MultiActionController这个在spring3.x中大行其道,spring4.x被遗弃,终于在spring5.x中被删除了.但是有些老的项目还是有在使用,这里介绍下其用法和新的替换方式.
MultiActionController方式
创建MultiActionController实现类(因为是一个简单的例子没有使用视图解析器)
package com.ultra.web;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Deprecated
public class HelloMultiActionController extends MultiActionController {
public ModelAndView hello(HttpServletRequest request, HttpServletResponse response) {
ModelAndView modelAndView = new ModelAndView("/WEB-INF/views/hello.jsp");
modelAndView.addObject("mess", "hello, I implemented the MultiActionController that was Deprecated!");
return modelAndView;
}
public ModelAndView hi(HttpServletRequest request, HttpServletResponse response) {
ModelAndView modelAndView = new ModelAndView("/WEB-INF/views/hello.jsp");
modelAndView.addObject("mess", "hi, I implemented the MultiActionController that was Deprecated!");
return modelAndView;
}
}
注册Controller和映射关系(dispatcherServlet-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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/multi/hello">helloMultiActionController</prop>
<prop key="/multi/hi">helloMultiActionController</prop>
</props>
</property>
</bean>
<bean id="helloMultiActionController" class="com.ultra.web.HelloMultiActionController">
<property name="methodNameResolver" ref="methodNameResolver"/>
</bean>
<bean id="methodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
<property name="mappings">
<props>
<prop key="/multi/hello">hello</prop>
<prop key="/multi/hi">hi</prop>
</props>
</property>
</bean>
</beans>
请求页面index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>index</title>
</head>
<body>
<a href="/simple/multi/hello">multi hello</a><br><br>
<a href="/simple/multi/hi">multi hi</a><br><br>
</body>
</html>
响应页面/WEB-INF/views/hello.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Spring MVC</title>
</head>
<body>
<h1>${mess}</h1>
</body>
</html>
applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
pom.xml(引入jar包配置)
<properties>
<spring.version>4.3.17.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.10.19</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 默认:/WEB-INF/applicationContext.xml -->
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 默认:/WEB-INF/{servlet-name}-servlet.xml -->
<param-value>/WEB-INF/dispatcherServlet-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
整体目录结构
测试
前台页面(这页面真吃藕…确实.)
响应页面
注解方式,让一个普通类当Controller并处理多个请求
创建普通类并添加处理请求的注解
package com.ultra.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/plain")
public class PlainOrdinaryController {
@GetMapping("/hello")
public ModelAndView hello() {
ModelAndView modelAndView = new ModelAndView("/WEB-INF/views/hello.jsp");
modelAndView.addObject("mess", "hello, I am a plain ordinary object!");
return modelAndView;
}
@GetMapping("/hi")
public ModelAndView hi() {
ModelAndView modelAndView = new ModelAndView("/WEB-INF/views/hello.jsp");
modelAndView.addObject("mess", "hi, I am a plain ordinary object!");
return modelAndView;
}
}
添加注解扫描
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello">helloController</prop>
<prop key="/multi/hello">helloMultiActionController</prop>
<prop key="/multi/hi">helloMultiActionController</prop>
</props>
</property>
</bean>
<bean id="helloController" class="com.ultra.web.HelloController"/>
<bean id="helloMultiActionController" class="com.ultra.web.HelloMultiActionController">
<property name="methodNameResolver" ref="methodNameResolver"/>
</bean>
<bean id="methodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
<property name="mappings">
<props>
<prop key="/multi/hello">hello</prop>
<prop key="/multi/hi">hi</prop>
</props>
</property>
</bean>
<!--使用@Controller注解需要的配置-->
<context:component-scan base-package="com.ultra.web"/>
<!--使用@RequestMapping等注解-->
<mvc:annotation-driven/>
</beans>
请求页面index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>index</title>
</head>
<body>
<a href="/simple/plain/hello">plain ordinary hello</a><br><br>
<a href="/simple/plain/hi">plain ordinary hi</a><br><br>
<a href="/simple/multi/hello">multi hello</a><br><br>
<a href="/simple/multi/hi">multi hi</a><br><br>
</body>
</html>
测试
前台页面
响应页面