注解的处理器映射器和适配器

2016-11-30  本文已影响135人  Stringer

springmvc.xml

<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"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
        
    <!-- 配置Handler -->
    <!-- <bean name="/queryItems.action" class="cn.ztc.ssm.controller.ItemsController1"/> -->
    
    <bean id="ItemsController1" name="/queryItems.action" class="cn.ztc.ssm.controller.ItemsController1"/>
    
    <bean id="ItemsController3" class="cn.ztc.ssm.controller.ItemsController2"/>
    
    <!-- 注解的handller
        实际开发中建议使用组件扫描
     -->
    <!-- <bean class="cn.ztc.ssm.controller.ItemsController3"/> -->
    <!-- 可以扫描controller service -->
    <context:component-scan base-package="cn.ztc.ssm.controller"></context:component-scan>

    <!-- 处理器映射器 
        将bean的那么作为url进行查找,需要在配置Handler是指定beanname
    --> 
    <bean   class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
    
    <!-- 简单url映射 -->
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <!-- 对ItemsController1进行url映射 -->
                <prop key="/queryItems1.action">ItemsController1</prop>
                <prop key="/queryItems2.action">ItemsController1</prop>
                <prop key="/queryItems3.action">ItemsController3</prop>
            </props>
        </property>
    </bean>
    
    <!--注解映射器 -->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
    
    
    <!-- 处理器适配器 -->
    <bean   class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
    
    <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>
    
    <!--注解适配器 -->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
    
    <!-- 使用 mvc:annotation-driven可以代替注解映射器、注解适配器的配置
        默认加载了很多参数绑定方法,例如json转换解析器
        实际开发中用这个配置
    -->
    <mvc:annotation-driven></mvc:annotation-driven>
    
    
    <!-- 视图解析器 
        解析jsp,默认使用jstl标签
    -->
    <bean   class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
        
</beans>```

ItemsController3.java

package cn.ztc.ssm.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.ztc.ssm.po.Items;

@Controller
public class ItemsController3 {
//商品查询列表
@RequestMapping("/queryItems4")
public ModelAndView queryItems() throws Exception{
//调用service查找数据库,查询商品列表,这里使用静态数据模拟
List<Items> itemsList = new ArrayList<Items>();
Items items_1 = new Items();
items_1.setName("联想笔记本");
items_1.setPrice(6000f);
items_1.setDetail("ThinkPad 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);
    
    ModelAndView modelAndView = new ModelAndView();
    //在jsp页面中通过itemsList取数据
    modelAndView.addObject("itemsList",itemsList);
    //指定视图
    modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");

    return modelAndView;
}

}

上一篇下一篇

猜你喜欢

热点阅读