Spring MVC学习笔记一

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)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值