欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring MVC学习笔记一 MVCSpringServletBeanWeb 

程序员文章站 2022-07-12 11:11:08
...

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

xml 代码
  1. < servlet >   
  2.    < servlet-name > training </servlet-name >   
  3.    < servlet-class > org.springframwork.web.servlet.DispatcherServlet </servlet-class >   
  4.    < load-on-startup > 1 </load-on-startup >   
  5. <servlet >   
  6.   
  7. < servlet-mapping >   
  8.    < servlet-name > training </servlet-name >   
  9.    < url-pattern > *.htm </url-pattern >   
  10. <servlet-mapping >   
  11.   
  12. 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". 

  13.  

In order to seperate the different tier context file, we must configurate listener and context-params in web.xml

xml 代码
  1. < listener >   
  2.    < listener-class > org.springframeowrk.web.context.ContextLoaderListener</ listener-class >   
  3. </listener >   
  4.   
  5. < context-param >   
  6.    < param-name > contextConfigLocation </param-name >   
  7.    < param-value > /WEB-INF/traning-service.xml,/WEB-INF/training-data.xml </param-value >   
  8. </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.

xml 代码
  1. < bean   name = "/home.htm"   class ="packname.HomeController >   
  2. </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

xml 代码
  1. < bean   id = "viewResolver"   class = "org.springframework.web.servlet.view.InternaleResourceViewResolver" >   
  2.    < property   name = "prefix" >   
  3.      < value > /WEB-INF/jsp/ </ value >   
  4.    </ property >   
  5.    < property   name = "suffix" >   
  6.      < value > .jsp </ value >   
  7.    </ property >   
  8. </ bean >   

4)create JSP (omit the details)