SPRINGMVC implement a controller to write multiple methods

Source: Internet
Author: User

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

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.