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

Spring MVC学习笔记二 SpringMVCBeanServletvelocity 

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

There are 3 ways for processing mapping from DispatcherServlet to Controller

BeanNameUrlHandlerMapping           map to url based on the name of Controller
SimpleUrlHandlerMapping                  map to url with the collection of properties which configurated in the context configuration files.
CommonsPathMapHandlerMapping   map to url with meta data.

they all implement the HandlerMapping interface.

Utilize multiple mapping processor

xml 代码
  1. < bean   id = "beanNameUrlMapping"   class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" >   
  2.    < property   name = "Order" > < value > 1 </value > </property >   
  3. </bean >   
  4.   
  5. < bean   id = "simpleUrlMapping"   class = "org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >   
  6.    < property   name = "Order" > < value > 0</ value > </property >   
  7. </bean >   

The smaller the order value is, the higher priority will be get.

 

Spring has a hierarchical controller.
Controller<-AbstractController<-MultiActionController
                                                      <-BaseCommandController<-AbstractFormController<-SimpleFormController
                                                                                                                                                       <-AbstarctWizardFormController
                                                                                                         <-AbstractCommandController

ThrowawayController

BaseCommandController has validator property which can provide the validation functionality.

the Command Object service as VO, which is provide as a property in BaseCommandController.

AbstractWizardFormController is well suited in Corss Page Form situation.
Attention will pay when decide which page should be nevigate. The specified string used(_target1,_finsih,_cancel) come to rescue. P266 Spring in action for details.

There is a little different to process validation in corss page form. 
In this case, there are lots of properties. You should invode validatePage() to validate Command object each time when you jump aross the pages.

  
View Resolver
A View is a bean responsible for reder the result to the user.
How the render happen is depend on your view type.
The ViewResolver is a bridge between logic view name and view bean. 
Spring will consult ViewResolver when decide which view bean should be applied.
There are four types of ViewResolver.
    InternalResourceViewResolver interpret the logic name to a template file(Jsp,Velocity)
    BeanNameViewResolver              to a View bean configurated in the DispatcherServlet context configuration file.
    ResourceBundleViewResolver  to a View Object of a ResourceBundle
    XmlViewResolver                           interpret View Bean from a another xml(not main config files)

Please refer P278 Spring in Action for details.

Mutiple View Resolver can be configurated in the file, the same as the mutiple controller ,the ordr property used for set priortiy to mapping the logic name to view bean . 

Handler Exception

  1. < bean   id = "exceptionResolver"   class = "org.springframeowrok.web.servlet.handler.SimpleMappingExceptionResolver" >   
  2.    < property   name = "exceptionMappings" >   
  3.      < props >   
  4.        < prop   key = "java.lang.Exception" > friendlyError </ prop >   
  5.      </ props >   
  6.    </ property >   
  7. </ bean >   

 

xml 代码