Spring MVC component-handlemapping Processor mapping __java

Source: Internet
Author: User

With the processor mapping, you can map the Web request to the correct processor controller. When the request is received, dispatcherservlet the request to the Handlermapping processor map, allowing it to check the request and find an appropriate handlerexecutionchain. This handlerexecutionchain contains a processor controller that can handle the request. Dispatcherservlet then executes the processor controller defined in Handlerexecutionchain.

Two most commonly used processor mappings They're all abstracthandlemapping subclasses.

1) simpleurlhandlermapping

Simpleurlhandlermapping can be configured in the application context and have ant-style path-matching capabilities. For example, we configure a simpleurlhandlermapping mapping processor in Applicationcontext.xml:

<beans>
   <bean class= "org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >        <property name= "Mappings" >           <props>            <prop key= "/**/help.do" >helpAction</prop>            <prop key= "/ex/ View*.do ">helpAction</prop>            <prop key= "/*/account.do" >accountAction</prop>             <prop key= "/*/editaccount.do" >accountAction</prop>           </props>        </property>    </ bean>    <bean id= "helpaction"  class= "Org.springframework.web.servlet.mvc.UrlFilenameV"Iewcontroller "/>       <bean id=" accountaction " class=" Org.springframework.web.servlet.mvc.SimpleFormController ">       <property Name= "FormView"  value= "account"/>       <property name= "SuccessView"  value= "account-created"/>       <property name= "CommandName"   value= "Account"/>       <property name= "Commandclass" samples. Account "/>    </bean> <beans>

This processor mapping will first pass to helpaction the request for file name Help.do in all directories, which is a urlfilenameviewcontroller. All requests starting with view in the ex directory, with a. Do end, are passed to helpaction. Similarly, we have defined two mappings for accountaction, and all account.do and editaccount.do requests will be addressed to Accountaction.

2) beannameurlhandlermapping

The

Beannameurlhandlermapping is a simple but powerful processor map that maps incoming HTTP requests to the bean's name. For example, to implement a user's ability to create a new account, we provide a JSP view (or velocity template) that Formcontroller and displays the form. When using beannameurlhandlermapping, we map the access requests containing http://samples.com/editAccount.do to the specified Formcontroller as follows: <beans >   <bean id= "handlermapping"       class= "Org.springframework.web. Servlet.handler.BeanNameUrlHandlerMapping "/>   <bean name="/editaccount.do "       class= "Org.springframework.web. Servlet.mvc.SimpleFormController ">            <property Name= "FormView"  value= "account"/>     <property name= "Successview" Account-created "/>     <property name=" commandName " value=" account "/>      <property name= "Commandclass"  value= "samples." Account "/>   </bean> <beans>

All requests for/editaccount.do will be handled by the formcontroller above. To use beannameurlhandlermapping, you do not need to define it in the Web application context (as shown above). By default, if the processor mapping is not found in the context, Dispatcherservlet creates a beannameurlhandlermapping for you.

3) Interceptor (Handlerinterceptor)

It is more powerful in processor mappings by configuring interceptors, including before the processor executes, after execution, or before and after the execution of the Interceptor. You can also support more features by customizing the handlermapping. For example, a custom processor map can be selected not only according to the URL of the request, but also according to the session state associated with the request.

Spring's processor mappings support interceptors. This is useful when you want to provide special functionality for certain requests, such as authenticating a user.

The interceptor in the processor map must implement the Handlerinterceptor interface in the Org.springframework.web.servlet package, which defines 3 methods, one called before the processor executes, one called after the processor executes, and the other is invoked after the entire request has been processed 。 These 3 methods provide you with enough flexibility to do any processing before and after the operation.

The following example provides an interceptor that intercepts account.do and editaccount.do requests:

  <beans>    <bean id= "handlermapping"       class= " Org.springframework.web.servlet.handler.SimpleUrlHandlerMapping ">      <property Name= "Interceptors" >         <list>            <ref bean= "Officehoursinterceptor"/>          </list>      </property>       <property name= "Mappings" >         <props>            <prop key= "/*/account.do" >accountAction</prop>            <prop key= "/*/editaccount.do" >accountAction</ Prop>         </props>      </ Property>    </bean>    <bean id= "Officehoursinterceptor"  class= "samples. Timebasedaccessinterceptor ">       <property name=" openingTime " value=" 9 "/>       <property name=" closingtime " value="/>     </bean> <beans>

The

Interceptor Timebasedaccessinterceptor must inherit Handlerinterceptoradapter, which is the role of redirecting users to a page if the current time is not 9 o'clock in the morning to 6 o'clock in the afternoon:

  package samples;    public class TimeBasedAccessInterceptor extends  Handlerinterceptoradapter        {             private int openingTime;             private int closingTime;             public void setopeningtime ( Int openingtime)               {                   this.openingtime = openingtime;               }             public void setclosingtime (int closingTime)                {                   this.closingTime = closingTime;               }             public boolean prehandle (                   Httpservletrequest request,                    HttpServletResponse response,                    object handler)  throws exception {               calendar cal = calendar.getinstance ();      &nbsP;        int hour = cal.get (HOUR_OF_DAY);               if  (openingtime  <= hour < closingtime)  {                   return true;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.