The use and description of three kinds of implementation classes for controller interface in SPRINGMVC

Source: Internet
Author: User

Before using and explaining the implementation class of the Controller interface, let me briefly explain how SPRINGMVC works.

The ① client makes a request, and after the Web server accepts the request, will match the mapping path in the Dispatcherservlet configured in Web. XML, and if there is a corresponding matching path, the request will be given to Dispatcherservlet processing (this dispatcher Servlets are also at the heart of Springmvc);

②dispactherservlet accepts the request, the appropriate processor map is searched according to the URL to handle the request;

When the ③ processor is finished, a Modelandview object is returned to Dispatcherservlet, which encapsulates the model data information and the visual graph logical name (the JSP page where the view logic name often refers to a jump);

④ but the Modelandview object encapsulates only the view logical name, not a real view object, so Dispatcherservlet needs to parse the visual logic name through Viewresolver;

⑤ The View object is parsed, Dispatcherservlet gives the model data to the view object for rendering;

⑥ finally returns the result to the client, which can be either a page or a view form of Excel.

In other words, Dispatcherservlet is the core of SPRINGMVC, configuring Dispatcherservlet in Web. xml:

<servlet>      <servlet-name>spring</servlet-name><!--The name of the servlet here is spring, meaning that when parsing, The server will find a file called Spring-servlet.xml in the Web-inf directory--      <servlet-class> org.springframework.web.servlet.dispatcherservlet</servlet-class>      <load-on-startup>1 </load-on-startup>  </servlet>  <servlet-mapping>      <servlet-name>spring</ Servlet-name>      <url-pattern>*.  do </url-pattern>  </servlet-mapping>
Spring-servlet.xml files are used to configure processor mappings, controller definitions, view resolvers, and so on.
SPRINGMVC controller based on the Org.springframework.web.servlet.mvc.Controller interface, Spring provides controller implementation class has three: Abstractcommandcontroller , Simpleformcontroller and Multiactioncontroller.
Here are the differences in the use of these three controller classes:
One, Abstractcommandcontroller (Command Controller):
The Spring-servlet.xml configuration when using the command controller consists of 3 steps:
① processor mapping configuration, where org.springframework.web.servlet.SimpleUrlHandlerMapping is used;
② defines the controller (here is the configuration of the custom controller, because the custom controller has inherited the controller provided by the SPRINGMVC)
③ configuration View parser, which uses org.springframework.web.servlet.view.InternalResourceViewResolver
After customizing a controller class to inherit Abstractcommandcontroller, the following code is configured in Spring-servlet.xml,
<!--Configuring the processor map --
<beanclass= "Org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" > <property name= "Mappings" > &L t;props> <prop key= "login.do" >loginController</prop> <!--key value represents the value of the URL, and if both matches, the processing is handed to the ID login Controller class Handling-</props> </property> </bean><!--Configuring the controller --<bean id= "Logincontroller"class= "Com.controller.LoginController" > <!--Configure the custom controller class, where the ID value corresponds to the above ID value, in other words, the above processing operation is given here to the corresponding Logincontroller class-- <property name= "Commandclass" value= "Com.entity.Users" ></property> <!--Configuration Injection class--</bean>
   <!--configuration View Resolver --<beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name= "prefix" value= "/" > </property> <property name= "suffix" value= ". jsp" ></property> </bean>
 public  class  logincontroller extends   Abstractcommandcontroller {@Override  
   
    protected  
     Modelandview handle (HttpServletRequest arg0 , HttpServletResponse arg1, Object arg2, bindexception arg3)  
    throws     Span style= "COLOR: #000000" > Exception {users user  =
     Specifies the view logical name Modelandview Mav  = 
    new  modelandview ("Success" 
    );        Mav.addobject ( "user" 
     return   Mav; }}
   

When the Logincontroller execution is finished, the Modelandview object is returned to Internalresourceviewresolver resolution, and the view parser class is rendered according to the specified view logical name inside the MAV, as an example, is to find the success.jsp file, and then jump.


Second, Simpleformcontroller (Form Controller):
After the custom controller class inherits Simpleformcontroller, there are two steps in the Spring-servlet.xml configuration:
① Configuring Processor Mappings
② Configuring the Controller
  
<!--Configuring the processor map--
class= "Org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" > <property name= " Mappings "> <props> <prop key=" reg.do ">regController</prop> </props> </property> </bean> <!--configuration Controller-- class= "Com.controller.RegController" > <property name= "Commandclass" value= "com.entity.Users" ></property> <property name= " FormView "value=" register.jsp "></property> <!--specifies that after the form data is loaded, the page that jumps to <property name=" Successview "value=" show.jsp "></property> <!--specify page to jump after successful execution of the form-- </bean>
 Public classRegcontrollerextendsSimpleformcontroller {protectedModelandview OnSubmit (httpservletrequest request, httpservletresponse response, Object command, BINDEXCEP tion errors)throwsexception{Users User=(Users) command; Modelandview Mav=NewModelandview ("show.jsp"); Mav.addobject ("User", user); returnMav; }        protected voidInitbinder (httpservletrequest request, Servletrequestdatabinder Binder)throwsexception{binder.registercustomeditor (Date.class,NewCustomdateeditor (NewSimpleDateFormat ("Yyyy-mm-dd"),true)); }        protectedMap Referencedata (HttpServletRequest request)throwsexception{Map<string, object> model =NewHashmap<string, object>(); Model.put ("Hobbylist",Newstring[]{"Swimming", "Running"}); returnmodel; }}
From the above two pieces of code can be known, when the value of the URL is reg.do, Dispatcherservlet will process the request to Id=regcontroller class (that is, regcontroller) processing, at this time not execute onsubmit () method, but executes the Referencedata () method, because there is no click of the Submit button. Because this method
The view logical name is not specified, so when Referencedata () executes, it will find the configuration controller <property name= "FormView" value= "register.jsp" ></property > This statement, where the value=register.jsp, the next step will jump to the register.jsp page, the corresponding data will be loaded into
register.jsp on the page. The onsubmit () method is executed when the Submit button is clicked on the Register.jsp page and the request is handed to regcontroller for processing (because of the action=reg.do of the form tag).
Comparing the configuration of the Abstractcommandcontroller and Simpleformcontroller, you will find that the configuration view parser is less simpleformcontroller, and the specified view logical name is used in the custom controller class. JSP "suffix. In fact, this is a paired appearance, configure the view parser, you do not need a suffix, because the suffix is already in the view parser
Have the same configuration, the two methods of the same effect.
Three, Multisimplecontroller (multi-action Controller):
Multi-action controller is to reduce a large number of defined controller classes, you can implement multiple actions in the same class inside the operation, to achieve functional integration.
The configuration of the Multisimplecontroller is relatively simple:
① Configuring Processor Mappings
② Configuring a custom controller
③ Configuring the response policy of a custom controller
<!--Configuring the processor map--
class= "Org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" > <property name= " Mappings "> <props> <prop key=" user.do ">userController</prop> </props> </property> </bean>
<!--Configuring a custom controller-- class= "Com.controller.UserController" > <property name= " Methodnameresolver "ref=" Methodnameresolver "></property> </bean>
<!--Configure a custom controller response policy-- class= " Org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver "> <property name=" ParamName "value=" method "></property> </bean>

The view parser is also not configured above, so the view logical name of the custom control class is appended with the suffix (that is, using the path + file name).

In the configuration of the custom controller, there is no need to configure the encapsulation class "Commandclas" relative to Abstractcommandcontroller and Simpeformcontroller, Because the Parametermethodnamereslover policy is used, the function is to match the name of the method in the custom controller class based on the URL request, and

Perform action processing.

When you use a configuration-based action response to implement SPRINGMVC, you will find that Abstractcommandcontroller and Simpleformcontroller are shown as expired because the SPRINGMVC framework is the recommended form of annotations.

The use and description of three kinds of implementation classes for controller interface in SPRINGMVC

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.