SpringMVC04controller中定义多个方法

public class MyController extends MultiActionController {

    // 新增 方法修饰符要是public
    public ModelAndView add(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        return new ModelAndView("/WEB-INF/jsp/success.jsp", "result", "add()");
    }

    // 修改
    public ModelAndView update(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        return new ModelAndView("/WEB-INF/jsp/success.jsp", "result",
                "update()");
    }

}

创建对应的controller

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

  <!-- 配置一个处理器映射器 -->
  <bean  class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="urlMap">
       <map>
       <!--
         因为我们的controller继承了MultiActionController,
         在MultiActionController有个
     /** Delegate that knows how to determine method names from incoming requests */
         private MethodNameResolver methodNameResolver = new InternalPathMethodNameResolver();
           根据我们的请求中参数找到对应方法 的解析器
          *:代表的就是我们的请求参数 也是对应的方法名
       -->
          <entry key="/user/*"  value="myController"/>
       </map>
    </property>
  </bean>

    <!-- 处理器 -->
    <bean id="myController" class="cn.bdqn.controller.MyController">
      <property name="methodNameResolver" ref="myResolver"></property>
    </bean>
</beans>

mvc核心xml文件的配置

使用我们自身设置的解析器 来执行,不需要改变controller中的代码!只需要更改mvc核心xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

  <!-- 配置一个处理器映射器 -->
  <bean  class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="urlMap">
       <map>
       <!--
         因为我们的controller继承了MultiActionController,
         在MultiActionController有个
     /** Delegate that knows how to determine method names from incoming requests */
         private MethodNameResolver methodNameResolver = new InternalPathMethodNameResolver();
           根据我们的请求中参数找到对应方法 的解析器
          *:代表的就是我们的请求参数 也是对应的方法名
       -->
          <entry key="/user/*"  value="myController"/>
       </map>
    </property>
  </bean>

  <!-- 设置解析器 -->
  <bean  id="myResolver" class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
    <property name="mappings">
     <props>
     <!--
      底层代码
     Set explicit URL to method name mappings through a Properties object.
      @param mappings Properties with URL as key and method name as value
    public void setMappings(Properties mappings) {
        this.mappings = mappings;
    }
      -->
       <prop key="/user/adds">add</prop>
       <prop key="/user/updates">update</prop>
     </props>
    </property>
  </bean>

    <!-- 处理器 -->
    <bean id="myController" class="cn.bdqn.controller.MyController">
    <!--需要更改父类的解析器引用 变成我们自已定义的解析器  -->
      <property name="methodNameResolver" ref="myResolver"/>
    </bean>

</beans>

mvc核心xml文件的设置

一旦我们设置了解析器,那么默认的

会执行我们设置的解析器

PropertiesMethodNameResolver

在浏览器中输入对应的url即可看到效果!

时间: 2024-08-05 14:03:59

SpringMVC04controller中定义多个方法的相关文章

Vue-Router路由Vue-CLI脚手架和模块化开发 之 在单文件组件项目中定义数据、方法和组件之间的相互引用

定义数据 根据上一篇博文配置项目的结构的基础上继续进行优化: 在app.vue中的导出模块/组件部分设置其属性: export default{//导出模块/组件 data(){ return{ name:'perfect', count:0 } }, 在一个template标签中进行调用: <template> <div> <h2> 欢迎来到perfect*的博客园!!!</h2> <h3>{{name}}</h3> </te

【enum】如何在枚举中定义自定义的方法,并进行使用

1.定一个枚举类 package com.eud.t1; public enum Color { //定义枚举中的常量 RED(1,"hongse"), GREEN(2,"qingse"),BLACK(3,"heise"); private int code; private String name; public int getCode() { return code; } public void setCode(int code) { thi

JS中定义类的方法&lt;转&gt;

转载地址:http://blog.csdn.net/sdlfx/article/details/1842218 PS(个人理解): 1) 类通过prototype定义的成员(方法或属性),是每个类对象共有的,一般不用来定义成员属性,一个对象修改了属性值,所有对象均被修改: 2) 类拥有prototype属性,类对象没有: 3) 每次new类对象或直接调用类(以下工厂方法形式),都会把定义类(function)的语句执行一次(单例模式可以避免这个情况): 4) 类是function类型,类对象是o

JS中定义类的方法

S中定义类的方式有很多种: 1.工厂方式 function Car(){    var ocar = new Object;    ocar.color = "blue";    ocar.doors = 4;    ocar.showColor = function(){     document.write(this.color)    };    return ocar;   }   var car1 = Car();   var car2 = Car(); 调用此函数时将创建新

C++类中定义常量的方法

好久没用过C++,本来就不太熟悉,今天突然写到一个类,需要在类中定义一个常量,居然花了很长时间. 刚开始写了static const int num = 100: 这样是不行的,因为常量只能在初始化列表中初始化,如果去掉const的话,又不能有"常量"的效果, 在类外用宏定义看起来不科学,类外const也一样,后面就大概搜索了一下,可以选择用枚举类型来替代达到 差不多的效果.具体的代码看下面: class User{ public: enum { MaxNum = 20}; } 写下来

Js中的假值_ES5中定义的ToBoolean方法强制类型转换后值为false

你不知道的Javascript(中)--ToBoolean javascript中的值可以分为以下两类: 1.可以被强制类型转换为false的值 2.其他(被强制类型转换为true的值) 假值---以下是js的ES规范中定义了的假值: undefined null false +0.-0和NaN ""-空字符串 假值的不二强制类型转换结果为false.一般除假值以外的都是真值. 假值对象--封装了假值的对象 eg: var a=new Boolean(false); var b=new

SpringMVC03controller中定义多个方法

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getS

php中定义数组的方法

1.PHP定义数组的格式 数组名=array(); 如:$aa=array();//这样就定义了一个数组, 之后给元素赋值: $aa[0]="9016"; $aa[1]="9017"; $aa[2]="9018"; 2.PHP输出数组的方法: foreach($aa as $val) { echo$val; } 也可以在定义数组时直接赋值 $aa=array(0=>"9016",1=>"9017"

自定义类中定义两个方法,相互调用

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace Test06 7 { 8 //class Class1 9 //{ 10 // public string Country() 11 // { 12 // string strCountry = "方法的示例!"; 13 // return strCountry; 14 /