spring mvc Hello World
2013-07-26 13:21
375 查看
index.jsp
Execution Flow
Run the application, then index.jsp file will be executed > click on the link given (I have given <a href=”java4s.html”>Click here to check the output</a>)
Once you click on that link, container will check the URL pattern at web.xml and passes the request to the DispatcherServlet
DispatcherServlet then passes that request to our controller class
Actually we are passing java4s.html from index.jsp right ? so DispatcherServlet verifies this ‘java4s’ name with the string in @RequestMapping(“-”) in our controller class if same it will executes the following method, which gives ModelAndView object as return
type
In our controller class we are returning…
return new ModelAndView("welcomePage", "welcomeMessage", message);
Means first argument is ‘View’ page name [ Where we are sending our result ], second, third arguments are key,values
So DispatcherServlet search for the name welcomePage in /jsp folder with extension .jsp [ you can change the 'view page' folder name/location and its extension in welcome-servlet.xml at line numbers 14,15], once the file was opened you can access the data
by using the key welcomeMessage [2nd parameter in ModelAndView object]
Check welcomePage.jsp > i am printing the result by calling the key ${welcomeMessage}
Note
In web.xml we have given servlet name as welcome, so spring configuration file name must be welcome-servlet.xml [ {servletName-in-web.xml}-servlet.xml ]
原文:http://www.java4s.com/spring-mvc/spring-mvc-hello-world-spring-mvc-3-2-hello-world-example-in-eclipse/
源代码:http://pan.baidu.com/share/link?shareid=3998828960&uk=3878681452
<body> <font size="2px" face="verdana"> Welcome... <a href="java4s.html"><br> Click here to check the output :-)</a> </font> </body>
package java4s; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller public class Java4sController { @RequestMapping("/java4s") public ModelAndView helloWorld() { String message = "Welcome to Java4s.com Spring MVC 3.2.x Sessions"; message += "<br>You Did it....!"; return new ModelAndView("welcomePage", "welcomeMessage", message); } }welcome-servlet.xml
<context:component-scan base-package="java4s" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/jsp/" /> <property name="suffix" value=".jsp" /> </bean>welcomePage.jsp
<html> <body> <font face="verdana" size="2">In Jsp ${welcomeMessage} </font> </body> </html>
Execution Flow
Run the application, then index.jsp file will be executed > click on the link given (I have given <a href=”java4s.html”>Click here to check the output</a>)
Once you click on that link, container will check the URL pattern at web.xml and passes the request to the DispatcherServlet
DispatcherServlet then passes that request to our controller class
Actually we are passing java4s.html from index.jsp right ? so DispatcherServlet verifies this ‘java4s’ name with the string in @RequestMapping(“-”) in our controller class if same it will executes the following method, which gives ModelAndView object as return
type
In our controller class we are returning…
return new ModelAndView("welcomePage", "welcomeMessage", message);
Means first argument is ‘View’ page name [ Where we are sending our result ], second, third arguments are key,values
So DispatcherServlet search for the name welcomePage in /jsp folder with extension .jsp [ you can change the 'view page' folder name/location and its extension in welcome-servlet.xml at line numbers 14,15], once the file was opened you can access the data
by using the key welcomeMessage [2nd parameter in ModelAndView object]
Check welcomePage.jsp > i am printing the result by calling the key ${welcomeMessage}
Note
In web.xml we have given servlet name as welcome, so spring configuration file name must be welcome-servlet.xml [ {servletName-in-web.xml}-servlet.xml ]
原文:http://www.java4s.com/spring-mvc/spring-mvc-hello-world-spring-mvc-3-2-hello-world-example-in-eclipse/
源代码:http://pan.baidu.com/share/link?shareid=3998828960&uk=3878681452
相关文章推荐
- 基于xml配置的Spring MVC HelloWorld
- Spring 3 MVC hello world example
- Gradle – Spring 4 MVC Hello World Example
- Spring MVC helloWorld中遇到的问题及解决办法
- IDEA建立Spring MVC Hello World 详细入门教程
- spring-3-mvc-hello-world-example
- spring mvc helloworld
- spring 3 mvc hello world + mavern +jetty
- Spring 4 MVC HelloWorld 纯注解方式(带源码)
- Spring学习笔记-1:SpringMVC hello world
- Spring MVC hello world annotation example
- Spring 3 MVC Framework Based Hello World Web Application Example Using Maven, Eclipse IDE And Tomcat
- Gradle – Spring 4 MVC Hello World Example – Annotation
- Maven+SpringMVC整合之helloworld
- Spring MVC hello world example
- Spring 4 MVC HelloWorld 纯注解方式(带源码)【超赞】
- Spring4 MVC HelloWorld 注解和JavaConfig实例
- Spring MVC hello world annotation example
- springMVC helloworld