SpringMVC is mainly used for process control. This blog summarizes how to add an Interceptor (Interceptor) in Process Control, how to resolve the ing of the process, and how to compile the Controller ).
First, let's take a look at the ing parser in the framework to bind the uri to the Controller:
1. The default parser of SpringMVC framework uses the bean name as the URI, Which is mapped to the Controller and BeanNameUrlHandlerMapping. For example:
In this case, the uri and class are directly bound. However, the coupling is higher. We generally want to bind the uri to the class id, so that we can easily modify it later. See the object provided by the framework below.
2. SimpleUrlHandlerMapping: binds the Request Path and the Controller through the ing relationship to make the settings more flexible. Here is an example:
In this way, the uri and the class id are bound to lower coupling and higher independence. In this way, our ing relationships and classes are separated. Of course, you can select either of them based on the actual situation. This is our two most commonly used ing parser.
2. Interceptor: Interceptor can be used to intercept the controller and extend the functions of the framework. Let's take a look at how to integrate the self-written Interceptor into the framework.
1. The custom interceptor must inherit the HandlerInterceptorAdapter parent class or implement the HandlerInterceptor interface. Here, let's take a look at a method to print a small interceptor of the system time before and after:
Public class TimeInterceptor extends HandlerInterceptorAdapter {// the operation to be executed after the entire process request is completed. @ Overridepublic void afterCompletion (HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {System. out. println ("invoke afterCompletion... "+ new Date ();} // The operation performed after the request controller ends @ Overridepublic void postHandle (HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {System. out. println ("invoke postHandle... "+ new Date ();} // The operation previously performed by the request controller @ Overridepublic boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {System. out. println ("invoke preHandle... "+ new Date (); return true ;}}
2, of course, after writing the interceptor, you need to configure our configuration file SpringMVC-servlet.xml to declare the Configuration:
3, here you also need to configure in the SpringMVC-servlet.xml file, the interceptor we set takes effect on those controllers, here declare the relationship between the interceptor and the Controller, using SimpleUrlHandlerMapping ing:
In this way, the function expansion of the Framework is relatively convenient.
3. Finally, let's take a look at the preparation of our Controller. The actions in Controller and Struts2 are the same, but they are invasive. In general, you need to implement the Controller interface. The previous blog has demonstrated it, and you can also inherit the parent class. here we can see several inherited parent classes to implement different functions.
1. inherit AbstractController. This is similar to the interface. We need to rewrite the method in it. The main thing is that, in general, this method needs to be set to the public type of the parent class protected, for the use of other classes, the method name is fixed. We can specify the path and the method that can be called by the framework. I will not give an example here.
2. inherit from MultiActionController (Multi-Action Controller). In this way, we can add multiple methods to process requests from multiple customers, for example:
Public class MultiController extends MultiActionController {// custom request processing method. for naming rules, see handleRequestInternalpublic ModelAndView insert (HttpServletRequest request, HttpServletResponse response) of the parent class) throws Exception {return new ModelAndView ("insertSuccess");} public ModelAndView delete (HttpServletRequest request, HttpServletResponse response) throws Exception {return new ModelAndView ("deleteSuccess ");}}
Let's take a look at the compilation of the configuration file ing:
Insert
Delete
3. inherits AbstractCommandController and command controller, which is used to obtain page parameters and encapsulate the parameters in the specified object model. Similar to the model driver of the Struts2 framework. For example:
Public class DemoController extends AbstractCommandController {public DemoController () {this. setCommandClass (User. class); // this is the data model class used to receive client request parameters .} // The command parameter is the data model object specified in the constructor. // The SpringMVC framework encapsulates Request Parameters in the specified data model object and passes them to the handle method for use. @ Overrideprotected ModelAndView handle (HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {User user = (User) command; System. out. println (user. getUsercode (); System. out. println (user. getUserpswd (); return new ModelAndView ("test ");}}
The ing of configuration files is the ing of classes and paths. In this way, the data transmitted from the page is directly encapsulated into the specified object, which can be used directly and is very convenient.
To sum up, some content about SpringMVC process control is provided. In fact, no MVC Framework cares about it. Core controllers, ing files, corresponding action classes, extensions can be implemented through interceptors, filters, and so on. After understanding the general direction and some minor problems, we can view the API documentation, which is a powerful assistant for our development !!!