SpringMVC之Controller中多个方法

1、web.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>	
  
  <servlet>
  	<servlet-name>test</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:config/spring-servlet.xml</param-value>
	</init-param>
	<load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>test</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
     此时spingMVC的配置文件使用的是随意的名字,且不在默认目录中,具体配置见上面。

2、spring-servlet.xml配置文件      

<?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:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	
	
	<bean name="/test/helloWorld" class="cn.com.yy.controller.HelloWorldController"/>
	
	<!-- 配置MultiController -->
	<bean name="/multiController" class="cn.com.yy.controller.MultiController">
		<property name="methodNameResolver">
			<ref bean="parameterMethodNameResolver"/>
		</property>
	</bean>
	<!-- 配置参数名称解析器  其中paramName和action是固定配置 -->
	<bean id="parameterMethodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
		<property name="paramName" value="action"></property>
	</bean>
	
	
	<!-- 配置视图解析器  -->
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/"/>
		<property name="suffix" value=".jsp"></property>
	</bean>
</beans>

3、MultiController类

package cn.com.yy.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 yy
 *
 */
public class MultiController extends MultiActionController {
	
	public ModelAndView addFunc(HttpServletRequest request,HttpServletResponse repsonse){
		System.out.println("------add functon----");
		return new ModelAndView("/func","msg","this is add function");
	}
	
	public ModelAndView updateFunc(HttpServletRequest request,HttpServletResponse repsonse){
		System.out.println("------update functon----");
		return new ModelAndView("/func","msg","this is update function");
	}
}

4、func.jsp页面

  <body>
   		${msg}
  </body>
5、结果

     当请求addFunc方法时:

                              

       当请求updateFunc方法时:     

                               


你可能感兴趣的:(springMVC,springMVC,多个方法)