Spring provides a multi-Action controller, which allows you to merge several actions into a single controller. This allows you to combine functions.
The multi-Action controller exists in a separate package -- org. springframework. web. mvc. multiaction -- it can map requests to method names,
Then call the correct method. For example, if you have many common functions in a controller, but want to use different behaviors from multiple portals to the Controller,
It is especially convenient to use multiple action controllers.
MultiActionController class implementation
Class Definition: public class MultiActionController extends AbstractController implements LastModified,
Inherits AbstractController and implements the LastModified interface. By default,-1 is returned;
Core Attributes:
Delegate: The delegate object for processing the function, that is, the object where the request processing method is to be called. The default value is this;
MethodNameResolver: name of the function processing method parser, that is, the name of the method for parsing the delegate function to be executed based on the request information.
Next, let's look at an example of using ParameterMethodNameResolver.
import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.multiaction.MultiActionController;public class UserController extends MultiActionController {public ModelAndView add(HttpServletRequest request,HttpServletResponse response) {ModelAndView mv = new ModelAndView(); mv.addObject("message","add"); mv.setViewName("add"); return mv; }public ModelAndView delete(HttpServletRequest request,HttpServletResponse response) {ModelAndView mv = new ModelAndView(); mv.addObject("message","delete"); mv.setViewName("delete"); return mv; }}
SpringMVC. xml configuration
<Bean class = "org. springframework. web. servlet. handler. simpleUrlHandlerMapping "> <property name =" mappings "> <props> <prop key =" userAction. do "> userAction </prop> </props> </property> </bean> <bean id =" userAction "class =" com. itmyhome. userController "> <property name =" methodNameResolver "> <bean class =" org. springframework. web. servlet. mvc. multiaction. parameterMethodNameResolver "> <! -- Specify the parameter named action --> <property name = "paramName" value = "action"/> </bean> </property> </bean>
Test URL:
Http: // localhost: 8080/spring_MultiActionController/userAction. do? Action = add
Http: // localhost: 8080/spring_MultiActionController/userAction. do? Action = delete
Source code download: http://download.csdn.net/detail/itmyhome/7363665
Welcome to the JAVA technology exchange group: 74955800