使用注解在SpringMVC中配置映射器和处理器

1、在springmvc.xml中配置注解的处理器适配器和映射器有两种方式,选其一:

(1)在springmvc.xml声明相关的bean及实现

<!-- 注解映射器 -->  
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> 
<!-- 注解适配器 -->  
 <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> 

(2)在springmvc.xml使用<mvc:annotaion-driven />标签来配置

<mvc:annotation-driven></mvc:annotation-driven>  

2、编写包含注释的Controller类:

package cn.com.mvc.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.com.mvc.model.Fruits;

//注解的Handler类  
//使用@Controller来标识它是一个控制器  
@Controller
public class FruitsControllerTest3{

    private FruitsService fruitsService = new FruitsService();
    
    //商品查询列表  
    //@RequestMapping实现 对queryFruitsList方法和url进行映射,一个方法对应一个url  
    //一般建议将url和方法写成一样  
    @RequestMapping(value="/queryFruitsList")
    public ModelAndView  queryFruitsList() throws Exception {
        //模拟Service获取水果商品列表  
        List<Fruits> fruitsList = fruitsService.queryFruitsList();  
        //返回ModelAndView  
        ModelAndView modelAndView =  new ModelAndView();  
        //相当 于request的setAttribut,在jsp页面中通过fruitsList取数据  
        modelAndView.addObject("fruitsList", fruitsList);  
        //指定视图  
        modelAndView.setViewName("/WEB-INF/jsp/fruitsList.jsp"); 
        return modelAndView;  
    }
    
    
  //内部类
      class FruitsService{
          public List<Fruits> queryFruitsList(){
              List<Fruits> fruitsList = new ArrayList<Fruits>();
              Fruits apple = new Fruits();
              apple.setName("苹果");
              apple.setPrice(5.0);
              apple.setProducing_area("杭州");
              
              Fruits banana = new Fruits();
              banana.setName("香蕉");
              banana.setPrice(9.0);
              banana.setProducing_area("河南");
              
              Fruits ma = new Fruits();
              ma.setName("马");
              ma.setPrice(15.0);
              ma.setProducing_area("广西州");
              
              fruitsList.add(apple);
              fruitsList.add(banana);
              fruitsList.add(ma);
              
              return fruitsList;
          }
      }
}

3、为了让注解的处理器映射器和适配器找到注解的Handler,需要在springmvc.xml中声明相关的bean信息,有两种方式:

(1)<!-- 直接声明该Handler所在的类 -->
        <bean name="/queryFruits_test.action" class="cn.com.mvc.controller.FruitsControllerTest" /> 

(2)使用扫描配置,会包下所有的类进行扫描,找出所有使用@Controller注解的Handler控制器类。

<context:component-scan base-package="cn.com.mvc.controller"></context:component-scan>  

4、最终springmvc.xml中的信息为:即不需要像前文那样为每个action请求单独配置bean了

<?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"
    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方法执行后返回的jsp具体位置来加载相应界面 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
        </bean>
        <mvc:annotation-driven></mvc:annotation-driven>
        <context:component-scan base-package="cn.com.mvc.controller"></context:component-scan>       

</beans>

猜你喜欢

转载自blog.csdn.net/figo8875/article/details/85162757
今日推荐