spring mvc: 属性方法名称解析器(多动作控制器)MultiActionController/ControllerClassNameHandlerMapping/PropertiesMethodNameResolver

spring mvc: 属性方法名称解析器(多动作控制器)

加入控制器是StudentContrller.java,里面有3个方法 index,add,remove

那么访问地址是:

http://localhost:8080/项目名/student/index.html

http://localhost:8080/项目名/student/add.html

http://localhost:8080/项目名/student/remove.html

需要用到的配置类方法:

org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping
org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver

控制器用到的类:
org.springframework.web.servlet.mvc.multiaction.MultiActionController


配置文件web.xml, xxxx-servlet.xml,applicationContext.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>  

  

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">
					       
<!-- 默认:注解映射支持 -->	
<mvc:annotation-driven/>	
<!-- 静态资源配置 -->
<mvc:resources location="/pages/**" mapping="/pages/"/>
<!-- 自动扫描包名 -->
<context:component-scan base-package="springmvc"/>
					       
</beans>				

  

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">
					       

<!-- 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>

<!-- 属性 方法名解析 -->
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
	<property name="caseSensitive" value="true"/>
</bean>

<bean class="springmvc.StudentController">
	<property name="methodNameResolver">
	<bean class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
		<property name="mappings">
			<props>
				<prop key="/student/index.html">index</prop>
				<prop key="/student/add.html">add</prop>
				<prop key="/student/remove.html">remove</prop>
			</props>
		</property>
	</bean>
	</property>
</bean>



					       
</beans>

  

StudentController.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 StudentController extends MultiActionController {

	public ModelAndView index(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView model = new ModelAndView("student_index");
		model.addObject("message", "student-index(主页面)");
		return model;
		
	}
	
	
	public ModelAndView add(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView model = new ModelAndView("student_add");
		model.addObject("message", "student-add(添加页面)");
		return model;
		
	}
	
	
	public ModelAndView remove(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView model = new ModelAndView("student_remove");
		model.addObject("message", "student-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>index(student)</title>
</head>
<body>

hi,<br>
${message}

</body>
</html>

  



 
原文地址:https://www.cnblogs.com/achengmu/p/8981452.html