SpringMVC - 非注解的处理器映射器和适配器

一、非注解的处理器映射器

  • 提供的处理器有两个属性。一个是id属性,一个是name属性。分别对应两种不同的映射器。
<bean id = "itemsController2" name="/queryItems.action" class="club.lemos.ssm.controller.ItemsController1"/>
  • BeanNameUrlHandlerMapping
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
  • SimpleUrlHandlerMapping
<!-- 简单url映射 处理器映射器 -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="/queryItems1.action">itemsController2</prop>
            <prop key="/queryItems2.action">itemsController2</prop>
        </props>
    </property>
</bean>

结论:一个 Handler,可以有多个映射器,一个映射器也可以有多个url.
注意:所有的映射器都实现了HandlerMapping接口。

二、非注解的处理器适配器

  • SimpleControllerHandlerAdapter
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

要求编写的Handler实现Controller接口

  • HttpRequestHandlerAdapter
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>

要求编写的Handler 实现 HttpRequestHandler接口。
Handler的实现如下:

public class ItemsController2 implements HttpRequestHandler{

    public void handleRequest(HttpServletRequest httpServletRequest,
                              HttpServletResponse httpServletResponse) throws ServletException, IOException {

        // 调用service 查找数据库,查询商品列表。这里使用静态数据模拟
        List<Items> itemsList = new ArrayList<Items>();

        Items items_1 = new Items();
        items_1.setName("联想笔记本");
        items_1.setPrice(6000f);
        items_1.setDetail("ThinkpPad T430联想笔记本电脑!");

        Items items_2 = new Items();
        items_2.setName("苹果手机");
        items_2.setPrice(5000f);
        items_2.setDetail("iphone6 苹果手机!");

        itemsList.add(items_1);
        itemsList.add(items_2);

        //设置模型数据
        httpServletRequest.setAttribute("itemsList",itemsList);

        //设置转发的视图
        httpServletRequest.getRequestDispatcher("WEB-INF/jsp/items/itemsList.jsp")
                .forward(httpServletRequest,httpServletResponse);

// 使用此方法可以通过修改 response,设置响应数据格式,比如响应json数据
// httpServletResponse.setCharacterEncoding("utf-8");
// httpServletResponse.setContentType("application/json;charset=utf-8");
// httpServletResponse.getWriter().write("json串");

    }
}

三、 DispatcherServlet.properties

DispatcherServlet.properties

前端控制器默认从DispatcherServlet.properties 文件中加载映射器、适配器、图形解析器等组件。也就是说如果你不在dispatcher-servlet.xml中额外配置,那么就加载这个文件中的默认配置。

转载于:https://my.oschina.net/lemos/blog/790122

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值