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

本文详细介绍了Spring MVC框架中AbstractController的使用方法,包括如何通过继承AbstractController来实现自定义控制器,以及如何在XML配置文件中指定处理器的访问方式。通过示例代码展示了具体的实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

 

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

 

 

 

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俩种方式可以访问到这个处理器

转载于:https://www.cnblogs.com/DawnCHENXI/p/8624394.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值