SSM-SpringMVC-08:SpringMVC中以继承AbstractController的方式实现处理器

时间:2022-08-09 08:32:56

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

AbstractController实现了一些特殊功能,如继承了WebContentGenerator缓存控制功能,并提供了可选的会话的串行化访问功能。而且提供了handleRequestInternal方法,因此我们应该在具体的控制器类中实现handleRequestInternal方法,而不再是handleRequest。

它可以指定请求的方式,什么请求可以访问到

简单使用:

package cn.dawn.day04abstractController;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.mvc.Controller; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Created by Dawn on 2018/3/19.
*/
//处理器
public class FirstController extends AbstractController { protected ModelAndView handleRequestInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
ModelAndView me=new ModelAndView();
me.setViewName("index");
return me;
}
}

在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: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="second" class="cn.dawn.day04abstractController.FirstController">
<!--可以指定访问方式-->
<property name="supportedMethods" value="GET,POST"></property>
</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="/hello">
<value>second</value>
</entry>
</map>
</property>-->
<!--第二种方式-->
<property name="mappings">
<props>
<prop key="/hello">second</prop>
</props>
</property>
</bean> </beans>

这种配置方法只支持,get,post俩种方式可以访问到这个处理器