Multiactioncontroller and Parametermethodnameresolver
Defining multiple methods in a controller class, and executing one of the methods according to the user's request, is equivalent to the dispatcheraction of struts. Use Multiactioncontroller need to match a Methodnameresolver instance, default use Parametermethodnameresolver, he will according to the given URL, The last requested file name to determine which method in the controller is executed
1. Controller class inheritance Multiactioncontroller
PackageCom.ljq.web.controller;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.web.servlet.ModelAndView;ImportOrg.springframework.web.servlet.mvc.multiaction.MultiActionController;/*** Implement a controller to write multiple methods * *@authorAdministrator **/ Public classMulticontrollerextendsMultiactioncontroller { Publicmodelandview Add (httpservletrequest request, httpservletresponse response) {SYSTEM.OUT.PRINTLN (/ c11>"----Add----"); return NewModelandview ("/multi", "message", "Add"); } Publicmodelandview Update (httpservletrequest request, httpservletresponse response) {SYSTEM.OUT.PRINTLN ( "----Update----"); return NewModelandview ("/multi", "message", "Update"); } Publicmodelandview Query (httpservletrequest request, httpservletresponse response) {SYSTEM.OUT.PRINTLN ( /c0>"----Query----"); return NewModelandview ("/multi", "message", "Query"); }}
View Code
2. Define the control method in the following format
Public modelandview Add (httpservletrequest request, httpservletresponse response) { System.out.println (" ----Add----"); return New Modelandview ("/multi", "message", "Add");
3. Introduction of multi-view control in the Dispatcher-servlet.xml file
class= "Org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver" > < Property Name= "ParamName" value= "action" ></property> </bean>
Then inject your own controller class
class= "Com.ljq.web.controller.MultiController" > <property name= "Methodnameresolver" > <ref bean= "Parammethodresolver"/> </property> </bean>
4. Perfect or add a view (multi.jsp), corresponding to the controller class jump
<%@ Page Language="Java"ContentType="text/html; Charset=utf-8"pageencoding="UTF-8"%><%@taglib prefix="C"URI="Http://java.sun.com/jsp/jstl/core"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML> <Head> <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"> <title>Implement a controller to write multiple methods</title> </Head> <Body>This call method is<b>${message}</b> </Body></HTML>
View Code
5. Deploy, start
Access path: http: // localhost:8083/springmvc/multi?action=addhttp://localhost:8083/springmvc/multi?action= Updatehttp://localhost:8083/springmvc/multi?action=query
SPRINGMVC implement a controller to write multiple methods