spring mvc一个Controller响应多个请求
原创
©著作权归作者所有:来自51CTO博客作者morris131的原创作品,请联系作者获取转载授权,否则将追究法律责任
1.1. 控制器的实现
package com.morris.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;
public class Users extends MultiActionController
public ModelAndView addUser(HttpServletRequest request,
HttpServletResponse response){
ModelAndView mav = new ModelAndView("hello");
mav.addObject("message", "do addUser method......");
return mav;
}
public ModelAndView updateUser(HttpServletRequest request,
HttpServletResponse response) {
ModelAndView mav = new ModelAndView("hello");
mav.addObject("message", "do updateUser method......");
return mav;
}
public ModelAndView deleteUser(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView mav = new ModelAndView("hello");
mav.addObject("message", "do deleteUser method......");
return mav;
}
}
|
1.2. Spring-mvc.xml文件的配置
<!-- 解析请求参数类 -->
<bean id="parameterMethodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="paramName" value="action"></property>
</bean>
<!-- 视图解析器 当返回逻辑视图时,InternalResourceViewResolver会加上前缀和后缀-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="suffix" value=".jsp"></property>
<property name="prefix" value="/"></property>
</bean>
<bean name="/users" class="com.morris.controller.Users">
<property name="methodNameResolver" ref="parameterMethodNameResolver"></property>
</bean>
|
1.3. 测试
在浏览器输入一下地址:
http://localhost:8080/HelloSpringMVC/users?action=addUser