SpringMVC一个Controller写多个方法

一.Controller

 1 public class MultiController extends MultiActionController {
 2 
 3     public ModelAndView add(HttpServletRequest request,HttpServletResponse response){
 4         System.out.println("add");
 5         String data = "add";
 6         return new ModelAndView("/index","data",data);
 7     }
 8         
 9     public ModelAndView update(HttpServletRequest request,HttpServletResponse response){
10         System.out.println("update");
11         String data = "update";
12         return new ModelAndView("/index","data",data);
13     }
14     
15 }

二.spring-servlet.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"  
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 4     xmlns:p="http://www.springframework.org/schema/p"  
 5     xmlns:mvc="http://www.springframework.org/schema/mvc"  
 6     xmlns:context="http://www.springframework.org/schema/context"  
 7     xmlns:util="http://www.springframework.org/schema/util"  
 8     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
 9             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd  
10             http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd  
11             http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
12          
13      <!-- 单个配置Controller -->   
14      <bean name="/test/hello" class="com.dwl.controller.HelloWorldController"></bean>
15      
16      <!-- 多个配置Controller -->
17      <bean id="paramResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
18          <property name="paramName" value="action"></property>
19      </bean>
20      
21      <bean name="/test/multi" class="com.dwl.controller.MultiController">
22          <property name="methodNameResolver" ref="paramResolver"></property>
23      </bean>
24      
25      
26             
27     <!-- 配置试图分解器 -->
28     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="itnternalResource" p:prefix="/" p:suffix=".jsp">
29     </bean>
30     
31 </beans>

三.web.xml

 1 <servlet>
 2         <servlet-name>spring</servlet-name>
 3         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 4         <init-param>
 5             <param-name>contextConfigLocation</param-name>
 6             <param-value>classpath:/springmvc/spring-servlet.xml</param-value>    
 7         </init-param>
 8         <load-on-startup>1</load-on-startup>
 9     </servlet>
10     <servlet-mapping>
11         <servlet-name>spring</servlet-name>
12         <url-pattern>/</url-pattern>    
13     </servlet-mapping>
14     

访问路径

http://localhost:8080/springMVC/test/multi?action=update

posted @ 2014-01-07 14:25  哎呦喂,我的小祖宗╰つ  阅读(418)  评论(0编辑  收藏  举报