参数方法名称解析器
1.配置依赖包
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<artifactId>SpringMVC</artifactId>
<groupId>cn.happy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>SpringMVC03ViewResolver</artifactId>
<packaging>war</packaging>
<name>SpringMVC03ViewResolver Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--spring对应的版本号-->
<spring.version>4.2.1.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<scope>test</scope>
</dependency>
<!--引入需要的spring 核心jar-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<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-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--引入需要的jstl jar-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<finalName>SpringMVC03ViewResolver</finalName>
</build>
</project>
2.配置中央调度器(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>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-controller.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
还有一个视图解析器——BeanNameViewResolver
3.配置处理器映射器(Spring-controller.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">
<!--映射器-->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello.do">myPropertiesController</prop>
</props>
</property>
</bean>
<!--视图解析器:XmlViewResolver-->
<bean class="org.springframework.web.servlet.view.XmlViewResolver">
<property name="location" value="classpath:myView.xml"/>
</bean>
<!--参数方法名称解析器-->
<bean id="parameterMethodNameResolver"
class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="paramName" value="actionName"/>
</bean>
<!--控制器配置-->
<bean id="myPropertiesController" class="cn.happy.controller.MyMultiController">
<property name="methodNameResolver" ref="parameterMethodNameResolver"/>
</bean>
</beans>
4.配置视图解析器(MyView.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">
<!--外部资源视图对象-->
<bean id="jd" class="org.springframework.web.servlet.view.RedirectView">
<property name="url" value="https://www.jd.com"/>
</bean>
<!--内部资源视图对象-->
<bean id="internal" class="org.springframework.web.servlet.view.JstlView">
<property name="url" value="/second.jsp"/>
</bean>
</beans>
5.处理器(MyMultiController)
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyMultiController extends MultiActionController {
public String doFirst(HttpServletRequest request, HttpServletResponse response) {
System.out.println("doFirst");
return "jd";
}
public ModelAndView doSecond(HttpServletRequest request, HttpServletResponse response) {
System.out.println("doSecond");
return new ModelAndView("internal");
}
}
6.视图(second.jsp)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h2>doSecond</h2>
</body>
</html>
测试结果:
当输入这行路径时:就会跳到下个页面