SpringMVC3 Learning (5) -- MultiActionController

Source: Internet
Author: User

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

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.