SSM-SpringMVC-09:SpringMVC中以继承MutiActionController类的方式实现处理器

时间:2022-04-15 03:37:53

------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------

MutiActionController类,多行动处理器,简单来说,就是可以一个处理器中有多个处理方法,分支

实现的原理可以解释一波:就是因为这个类中有InternalPathMethodNameResolver这个方法名字解析器,他的作用就是通过urlpath可以访问到这个类中的path指定的方法

小Demo案例:

  一,自定义一个类(我的叫MyMutiActionController),继承MutiActionController这个类,他里面没有必须要实现的方法,纯手写

package cn.dawn.day05multiActioncontroller;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Created by Dawn on 2018/3/23.
*/
public class MyMultiActionController extends MultiActionController{ public String doFirst(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
ModelAndView me=new ModelAndView();
me.setViewName("first");
return "first";
} public ModelAndView doSecond(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
ModelAndView me=new ModelAndView();
me.setViewName("second");
return me;
}
}

  此处要注意的是方法修饰符给public,我之前复制的AbstractController那个实现类的方法,它的访问修饰符是protected,结果出的错找了20分钟,据我的估计,它这儿有点low,按道理反射的写法应该连private也能读取到啊

  二,在自己的配置文件中:

<?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: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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--配置bean处理器-->
<bean id="myMultiActionController" class="cn.dawn.day05multiActioncontroller.MyMultiActionController"></bean>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<!--第一种方式-->
<property name="urlMap">
<map>
<!--这儿需要改成/*,而不是之前写死的那种-->
<entry key="/*">
<value>myMultiActionController</value>
</entry>
</map>
</property>
<!--第二种方式-->
<!--<property name="mappings">
<props>
<prop key="/*">myMultiActionController</prop>
</props>
</property>-->
</bean> </beans>

  三,之后需要改的有web.xml,将指向的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:ApplicationContext-day05MultiActionController.xml</param-value>
</init-param>
<load-on-startup></load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <!--第一种方式-->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping> </web-app>

  四,我在处理器的方法中不是给了一个first和second的字符串吗?

    那个经过视图解析器的处理其实就是/first.jsp和/second.jsp

    所以,我们写出这俩个页面供测试使用

  

    first.jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<body>
<h2>我是first</h2>
</body>
</html>

    second.jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<body>
<h2>我是second</h2>
</body>
</html>

---end---