Spring MVC学习笔记一 MVCSpringServletBeanWeb
Sequence:
1)在SpringMVC中,DispatcherServlet作为前端控制器来负责接收用户通过浏览器发送的一个请求。
2)然后DispatcherServlet开始查询一个或多个HandlerMapping(记录了mapping between controller object and url
3)DispatcherServlet delegate the request to the correspondent Controller in which actually delegate to business Object.
4)After business method return, a ModelAndView return to DispatcherServlet by Controller.
5)If the ModelAndView take only a logic name for the view object, DispatcherServlet have to ask for help of ViewResolver to query the View Object which server as rendering the response.
6) View Object responsible for render the response return to client.
Configuration:
1)configurate DispatcherServlet in web.xml
- < servlet >
- < servlet-name > training </servlet-name >
- < servlet-class > org.springframwork.web.servlet.DispatcherServlet </servlet-class >
- < load-on-startup > 1 </load-on-startup >
- <servlet >
- < servlet-mapping >
- < servlet-name > training </servlet-name >
- < url-pattern > *.htm </url-pattern >
- <servlet-mapping >
-
NOTE: servlet-name define the name for Servlet, After DispatcherServlet was loaded, the class will try to load the spring context with the default name of "traning-servlet.xml".
-
In order to seperate the different tier context file, we must configurate listener and context-params in web.xml
- < listener >
- < listener-class > org.springframeowrk.web.context.ContextLoaderListener</ listener-class >
- </listener >
- < context-param >
- < param-name > contextConfigLocation </param-name >
- < param-value > /WEB-INF/traning-service.xml,/WEB-INF/training-data.xml </param-value >
- </context-param >
NOTE:contextConfigLocation is a comma sepeartion.
Creating Main Page.
Step
1. code Controller class.
2. configurate traning-servlet.xml (DispatcherServlet context configuration file)
3.Code ViewResolver to combine the Controller and JSP
4. Code JSP
1)Controller is analogous to action.
Code a Controller is much like code a action-extends Controller, override the handleRequest(httprequest,httpresponse),return a ModelAndView
2)configurate the Controller.
- < bean name = "/home.htm" class ="packname.HomeController >
- </bean > <!---->
The name property in bean element confuse the novice just like me.
It have two meaning, define the bean name and url pattern of this Controller.
3)define a ViewRevoler
- < bean id = "viewResolver" class = "org.springframework.web.servlet.view.InternaleResourceViewResolver" >
- < property name = "prefix" >
- < value > /WEB-INF/jsp/ </ value >
- </ property >
- < property name = "suffix" >
- < value > .jsp </ value >
- </ property >
- </ bean >
4)create JSP (omit the details)