一、注意點: 版本問題
spring3.2以前的版本,注解的映射器和適配器使用以下兩個類.
org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping.class org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.class
在新版本的源碼中可以看到以下注釋:

在3.2 包含及以后的版本中使用如下類:
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.class org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.class
二、使用注解和非注解的差別
1. 使用非注解時,在controller中只能實現一個方法,方法名和參數固定,不能使用多個方法。
2. 使用注解時,允許自定義任意方法。
3. 使用注解時,映射器和適配器需要同時使用,不可注解與非注解混搭使用。
三、注解的簡寫方式
如下,提供了更簡便的注解配置方法,以后生產環境中可直接使用該注解配置。
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean> --> <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean> --> <!-- 簡化后的注解處理器映射器和適配器 :不僅包含上面映射器和適配器,還加載了很多參數綁定方法 --> <mvc:annotation-driven></mvc:annotation-driven>
四、開發過程
1. 修改springmvc.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd "> <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean> --> <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean> --> <!-- 簡化后的注解處理器映射器和適配器 :不僅包含上面映射器和適配器,還加載了很多參數綁定方法 --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 配置handler處理器 --> <bean class="com.king.controller.UserController"></bean> <!-- 配置視圖解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean> </beans>
2. 修改UserController.java類如下:
package com.king.controller; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.king.pojo.User; @Controller
public class UserController { @RequestMapping("/getList") public ModelAndView getList(){ System.out.println("start UserController"); List<User> list = new ArrayList<>(Arrays.asList( new User(1,"zhangsan",20), new User(1,"zhang2",22), new User(1,"zhang3",23), new User(1,"zhang4s",24) )); ModelAndView mv = new ModelAndView(); mv.addObject("list", list); mv.setViewName("page/list.jsp"); return mv; } }
