首页 > 代码库 > Springmvc系列02 简单url处理器映射和另一个适配器

Springmvc系列02 简单url处理器映射和另一个适配器

技术分享

1.springmvc配置

技术分享
<?xml version="1.0" encoding="UTF-8"?>


<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-4.0.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
     http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd">
<!-- 处理器映射器 将bean的name作为url进行查找 ,需要在配置handler是自定beanname(就是url)-->
    <bean  class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    <!-- 简单url映射 -->
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="queryItems1.action">itemsController1</prop>
                <prop key="queryItems2.action">itemsController1</prop>
                <prop key="queryItems3.action">itemsController2</prop>
            </props>
        </property>
    </bean>
<!-- 处理器适配器 -->
    <!-- 这个适配器能解析的Handler需要实现Controller接口 -->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
    <!-- 另一个非注解的适配器 -->
    <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>
<!-- 视图解析器 (解析jsp,默认使用jstl标签,所以jstl下需要有jstl的包)-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
<!-- 配置Handler -->
<bean id="itemsController1" name="/queryItems.action" class="com.meicai.ssm.controller.ItemsController1"/>
<bean id="itemsController2"  class="com.meicai.ssm.controller.ItemsController2"/>
</beans>
另一个适配器和简单url映射

2.实现了HttpRequestHandler接口的类

技术分享
package com.meicai.ssm.controller;

import java.io.IOException;
import java.util.List;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.HttpRequestHandler;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.meicai.ssm.po.Items;

/**
 * 
 * 
 * @author Administrator
 *
 */
public class ItemsController2 implements HttpRequestHandler {

    @Override
    public void handleRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        List<Items> itemsList=new ArrayList<Items>();
        Items items1=new Items();
        items1.setName("联想笔记本chm2");
        items1.setPrice(6000f);

        items1.setDetail("tinkpadT430");
        
        Items items2=new Items();
        items2.setName("苹果手机");
        items2.setPrice(5000f);
        items2.setDetail("ipone7");
        itemsList.add(items1);
        itemsList.add(items2);
        request.setAttribute("itemsList", itemsList);
        request.getRequestDispatcher("/WEB-INF/jsp/itemsList.jsp").forward(request, response);
        
    }

}
实现了HttpRequestHandler接口的类

3.部署调试:http://localhost:8080/springmvc04-01/queryItems3.action

4.技术分享

Springmvc系列02 简单url处理器映射和另一个适配器