多动作控制器
MultiActionController,用于在单个控制器中分别映射多个URL到对应的方法。
UserController.java
public class UserController extends MultiActionController {
public ModelAndView home(HttpServletRequest request,HttpServletResponse response){
ModelAndView model = new ModelAndView("home");
model.addObject("message","Home");
return model;
}
public ModelAndView add(HttpServletRequest request,HttpServletResponse response){
ModelAndView model = new ModelAndView("index");
model.addObject("message","Add");
return model;
}
public ModelAndView remove(HttpServletRequest request,HttpServletResponse response){
ModelAndView model = new ModelAndView("index");
model.addObject("message","Remove");
return model;
}
}
servlet.xml文件中配置:
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean name="/home.html" class="com.springapp.multiaction.UserController"/>
<bean name="/user/*.html" class="com.springapp.multiaction.UserController"/>
对于/home.html请求,DispatcherServlet将请求转发到UserController类的 home()方法。
对于user/add.html,DispatcherServlet将请求转发到UserController类的 add()方法。
对于user/remove.html,DispatcherServlet将请求转发到UserController类的 remove()方法。
home.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
<a href="user/add.html" >Add</a>
<a href="user/remove.html">Remove</a>
</body>
</html>
user.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
${message}
</body>
</html>
url访问:
http://localhost:8080/multiaction/home.html
属性方法名称解析器
UserController.java
public class UserController extends MultiActionController {
public ModelAndView home(HttpServletRequest request,HttpServletResponse response){
ModelAndView model = new ModelAndView("index");
model.addObject("message","Home(home方法)");
return model;
}
public ModelAndView add(HttpServletRequest request,HttpServletResponse response){
ModelAndView model = new ModelAndView("index");
model.addObject("message","Add(add方法)");
return model;
}
public ModelAndView remove(HttpServletRequest request,HttpServletResponse response){
ModelAndView model = new ModelAndView("index");
model.addObject("message","Remove(remove方法)");
return model;
}
}
servlet.xml配置:
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
<property name="caseSensitive" value="true"/>
</bean>
<bean class="com.springapp.multiactionpropertyname.UserController">
<property name="methodNameResolver">
<bean class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
<property name="mappings">
<props>
<prop key="/user/home.html">home</prop>
<prop key="/user/add.html">add</prop>
<prop key="/user/remove.html">remove</prop>
</props>
</property>
</bean>
</property>
</bean>
使用上面的配置,如果URI:
对于/user/home.html请求,DispatcherServlet将请求转发到UserController类的 home()方法。
对于user/add.html,DispatcherServlet将请求转发到UserController类的 add()方法。
对于user/remove.html,DispatcherServlet将请求转发到UserController类的 remove()方法。
user.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
${message}
</body>
</html>
url访问地址:
http://localhost:8080/multiactionpropertyname/user/home.html
http://localhost:8080/multiactionpropertyname/user/add.html
http://localhost:8080/multiactionpropertyname/user/remove.html
参数方法名称解析器
UserController.java:
public class UserController extends MultiActionController {
public ModelAndView home(HttpServletRequest request,HttpServletResponse response){
ModelAndView model = new ModelAndView("index");
model.addObject("message","Home(home方法)");
return model;
}
public ModelAndView add(HttpServletRequest request,HttpServletResponse response){
ModelAndView model = new ModelAndView("index");
model.addObject("message","Add(add方法)");
return model;
}
public ModelAndView remove(HttpServletRequest request,HttpServletResponse response){
ModelAndView model = new ModelAndView("index");
model.addObject("message","Remove(remove方法)");
return model;
}
}
servlet.xml配置:
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
<property name="caseSensitive" value="true"/>
</bean>
<bean class="com.springapp.parametermethodname.UserController">
<property name="methodNameResolver">
<bean class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="paramName" value="action">
</property>
</bean>
</property>
</bean>
user.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
${message}
</body>
</html>
url访问:
http://localhost:8080/parametermethodname/user/test.html?action=home
http://localhost:8080/parametermethodname/user/test.html?action=add
http://localhost:8080/parametermethodname/user/test.html?action=remove
可参数化视图控制器
UserController.java:
public class UserController extends MultiActionController {
public ModelAndView home(HttpServletRequest request,HttpServletResponse response){
ModelAndView model = new ModelAndView("user");
model.addObject("message","Home");
return model;
}
}
servlet.xml配置:
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
index.html=userController
</value>
</property>
</bean>
<bean id="userController" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="viewName" value="index"/>
</bean>
index.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
<h2>Hello World! This is a Index jsp page.</h2>
</body>
</html>
网友评论