SpringMVC实现一个controller有多个处理请求的方法

MultiActionController与ParameterMethodNameResolver
在一个Controller类中定义多个方法,并根据使用者的请求来执行当中的某个方法,相当于Struts的DispatcherAction。使用MultiActionController需要配个MethodNameResolver实例,默认使用ParameterMethodNameResolver,他会根据所给的网址中,最后请求的参数来决定执行Controller中的哪一个方法

 

1.控制器类继承MultiActionController

package com.ljq.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

/**
 * 实现一个controller写多个方法
 * 
 * @author Administrator
 * 
 */
public class MultiController extends MultiActionController {

    public ModelAndView add(HttpServletRequest request,
            HttpServletResponse response) {
        System.out.println("----add----");
        return new ModelAndView("/test", "message", "add");
    }

    public ModelAndView update(HttpServletRequest request,
            HttpServletResponse response) {
        System.out.println("----update----");
        return new ModelAndView("/test", "message", "update");
    }

    public ModelAndView query(HttpServletRequest request,
            HttpServletResponse response) {
        System.out.println("----query----");
        return new ModelAndView("/test", "message", "query");
    }

}

 

 

2.定义控制方法按如下格式

 public ModelAndView add(HttpServletRequest request,
            HttpServletResponse response) {
        System.out.println("----add----");
        return new ModelAndView("/test", "message", "add");
    }

 

 

3.在dispatcher-servlet.xml文件中引入多视图控制

<bean id="paramMethodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
        <property name="paramName" value="action"></property>    
    </bean>

然后注入自己的控制器类

 <bean id="multi" class="com.ljq.web.controller.MultiController">
        <property name="methodNameResolver">
            <ref bean="paramMethodResolver"/>
        </property>
    </bean>

完整的配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:tx="http://www.springframework.org/schema/tx"
     xsi:schemaLocation="
     http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx.xsd
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop.xsd">
           
    <bean id="urlMapping"  
        class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">  
        <property name="mappings">  
            <props>  
                <prop key="/multi.do">multiController</prop>  
            </props>  
        </property>  
    </bean>  
    <!--配置由参数action来决定调用MultiActionController中的哪个方法--> 
    <bean id="paramMethodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
        <property name="paramName" value="action"></property>    
    </bean>
    <!--配置MultiActionController-->
    <bean id="multiController" class="com.ljq.web.controller.MultiController">
        <property name="methodNameResolver">
            <ref bean="paramMethodResolver"/>
        </property>
    </bean>
    
    <bean id="viewResolver"  
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix" value="/"></property>  
        <property name="suffix" value=".jsp"></property>  
    </bean>  
  
</beans>  

 

 

 

4.完善或者添加一个视图(test.jsp),对应控制器类的跳转

<%@ 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>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>实现一个controller写多个方法</title>
    </head>
    <body>
        本次调用方法是<b>${message }</b>
    </body>
</html>

 

 

5.部署,启动

访问路径:
http://localhost:8083/springmvc/multi.do?action=add
http://localhost:8083/springmvc/multi.do?action=update
http://localhost:8083/springmvc/multi.do?action=query

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值