————吾亦无他,唯手熟尔,谦卑若愚,好学若饥————-
MutiActionController类,多行动处理器,简单来说,就是可以一个处理器中有多个处理方法,分支
实现的原理可以解释一波:就是因为这个类中有InternalPathMethodNameResolver这个方法名字解析器,他的作用就是通过urlpath可以访问到这个类中的path指定的方法
小Demo案例:
一,自定义一个类(我的叫MyMutiActionController),继承MutiActionController这个类,他里面没有必须要实现的方法,纯手写
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 1package cn.dawn.day05multiActioncontroller;
2
3import org.springframework.web.servlet.ModelAndView;
4import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
5
6import javax.servlet.http.HttpServletRequest;
7import javax.servlet.http.HttpServletResponse;
8
9/**
10 * Created by Dawn on 2018/3/23.
11 */
12public class MyMultiActionController extends MultiActionController{
13
14 public String doFirst(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
15 ModelAndView me=new ModelAndView();
16 me.setViewName("first");
17 return "first";
18 }
19
20 public ModelAndView doSecond(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
21 ModelAndView me=new ModelAndView();
22 me.setViewName("second");
23 return me;
24 }
25}
26
**
此处要注意的是方法修饰符给public,我之前复制的AbstractController那个实现类的方法,它的访问修饰符是protected,结果出的错找了20分钟,据我的估计,它这儿有点low,按道理反射的写法应该连private也能读取到啊**
二,在自己的配置文件中:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 1<?xml version="1.0" encoding="UTF-8"?>
2<beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
4 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">
5
6 <!--配置bean处理器-->
7 <bean id="myMultiActionController" class="cn.dawn.day05multiActioncontroller.MyMultiActionController"></bean>
8 <!--视图解析器-->
9 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
10 <property name="prefix" value="/"></property>
11 <property name="suffix" value=".jsp"></property>
12 </bean>
13
14 <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
15 <!--第一种方式-->
16 <property name="urlMap">
17 <map>
18 <!--这儿需要改成/*,而不是之前写死的那种-->
19 <entry key="/*">
20 <value>myMultiActionController</value>
21 </entry>
22 </map>
23 </property>
24 <!--第二种方式-->
25 <!--<property name="mappings">
26 <props>
27 <prop key="/*">myMultiActionController</prop>
28 </props>
29 </property>-->
30 </bean>
31
32</beans>
33
三,之后需要改的有web.xml,将指向的xml配置文件改成你现在用的那个
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 1<!DOCTYPE web-app PUBLIC
2 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
3 "http://java.sun.com/dtd/web-app_2_3.dtd" >
4
5<web-app>
6 <display-name>Archetype Created Web Application</display-name>
7 <!--中央调度器-->
8 <servlet>
9 <servlet-name>springmvc</servlet-name>
10 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
11 <init-param>
12 <param-name>contextConfigLocation</param-name>
13 <param-value>classpath:ApplicationContext-day05MultiActionController.xml</param-value>
14 </init-param>
15 <load-on-startup>1</load-on-startup>
16 </servlet>
17 <servlet-mapping>
18 <servlet-name>springmvc</servlet-name>
19 <url-pattern>/</url-pattern>
20 </servlet-mapping>
21
22
23 <!--第一种方式-->
24 <servlet-mapping>
25 <servlet-name>default</servlet-name>
26 <url-pattern>*.js</url-pattern>
27 </servlet-mapping>
28 <servlet-mapping>
29 <servlet-name>default</servlet-name>
30 <url-pattern>*.css</url-pattern>
31 </servlet-mapping>
32 <servlet-mapping>
33 <servlet-name>default</servlet-name>
34 <url-pattern>*.jpg</url-pattern>
35 </servlet-mapping>
36 <servlet-mapping>
37 <servlet-name>default</servlet-name>
38 <url-pattern>*.png</url-pattern>
39 </servlet-mapping>
40 <servlet-mapping>
41 <servlet-name>default</servlet-name>
42 <url-pattern>*.gif</url-pattern>
43 </servlet-mapping>
44
45</web-app>
46
四,我在处理器的方法中不是给了一个first和second的字符串吗?
那个经过视图解析器的处理其实就是/first.jsp和/second.jsp
所以,我们写出这俩个页面供测试使用
first.jsp页面
1
2
3
4
5
6
7 1<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
2<html>
3<body>
4<h2>我是first</h2>
5</body>
6</html>
7
second.jsp页面
1
2
3
4
5
6
7 1<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
2<html>
3<body>
4<h2>我是second</h2>
5</body>
6</html>
7
—end—