spring mvc: 参数方法名称解析器(用参数来解析控制器下的方法)MultiActionController/ParameterMethodNameResolver/ControllerClassNameHandlerMapping
根据地址栏上的url的参数action来解析相应的控制器下的方法名,例如:
http://localhost:8080/项目名/user/index.html?action=remove
http://localhost:8080/项目名/user/*(可以是任意字符).html?action=remove
xml配置中会用到的类:
org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver
java-controller中,会用到:
org.springframework.web.servlet.mvc.multiaction.MultiActionController;
本例中用到了3个配置文件:web.xml, applicationContext, xxxx-servlet.xml
web.xml:
<!--配置文件路径--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <!-- 字符过滤器 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 监听转发 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 默认:注解映射支持 --> <mvc:annotation-driven/> <!-- 静态资源配置 --> <mvc:resources location="/pages/**" mapping="/pages/"/> <!-- 自动扫描包名,controller --> <context:component-scan base-package="springmvc"/> </beans>
springmvc-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- ViewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <!-- controller名解析 --> <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"> <property name="caseSensitive" value="true"/> </bean> <bean class="springmvc.UserController"> <property name="methodNameResolver"> <bean class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver"> <property name="paramName" value="action"/> </bean> </property> </bean> </beans>
UserController.java代码
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 UserController extends MultiActionController { public ModelAndView home(HttpServletRequest request, HttpServletResponse response) { ModelAndView model = new ModelAndView("user_home"); model.addObject("message", "home"); return model; } public ModelAndView add(HttpServletRequest request, HttpServletResponse response) { ModelAndView model = new ModelAndView("user_add"); model.addObject("message", "add"); return model; } public ModelAndView remove(HttpServletRequest request, HttpServletResponse response) { ModelAndView model = new ModelAndView("user_remove"); model.addObject("message", "remove"); return model; } }
jsp代码,大同小异:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page isELIgnored="false" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>home</title> </head> <body> hi,${message} </body> </html>