码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMVC04controller中定义多个方法

时间:2016-12-02 11:47:19      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:method   ges   exce   with   schema   ns-3   映射   tar   action   

技术分享
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即可看到效果!

 

SpringMVC04controller中定义多个方法

标签:method   ges   exce   with   schema   ns-3   映射   tar   action   

原文地址:http://www.cnblogs.com/999-/p/6124946.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!