SpringMVC 使用注解配置基本使用(2)
主要是针对配置(1)进行优化:
SpringMVC xlm可左如下配置,从Spring3.0开始,可以用<mvc:annotation-driven/>标签,代替AnnotationHandlerMapping和MethodHandlerAdapter两个bean,Spring3.0后对其进行了封装,自动注入,省得麻烦每次自己手写一遍,因为这两个类每次都需要用到,一个是用来映射请求对应的Controller,一个是映射Controller里对应的方法。
<?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:p="http://www.springframework.org/schema/p" 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.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 扫描该包下所有的类 --> <context:component-scan base-package="test"></context:component-scan> <!-- spring3.0开始,可用以下方式直接替代下面两个bean,因为每次都需要用到HandlderMapping和MethodHandlerAdapter这两个类,spring干脆把它们封装起来--> <mvc:annotation-driven/> <!-- handleMapping类,用于对发来的请求映射到对应的Controller --> <!-- <bean id="handleMapping" mapping="/imp/**"/> <bean value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property></bean></beans>
package test;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.AbstractController;@Controller@RequestMapping(value="/User")public class MyAnnotationController { //假如 RequestMapping不写value,默认输入的是value值,@RequestMapping("/addUser")// 注解方式不需要输入HttpServletRequest和HttpServletResponse这两个参数public String addUser() {System.out.println("addUser--------");String result = "addUser";// 跳转到annotation.jspreturn "annotation";}/* * @RequestMapping(value="/User/addUser",method=RequestMethod.GET) * //注解方式不需要输入HttpServletRequest和HttpServletResponse这两个参数 public * ModelAndView addUser(){ System.out.println("addUser--------"); String * result = "addUser"; //跳转到annotation.jsp return new * ModelAndView("annotation","result",result); } */}
package test;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.AbstractController;@Controller@RequestMapping(value="/User")public class MyAnnotationController { //假如 RequestMapping不写value,默认输入的是value值,@RequestMapping("/addUser")// 注解方式不需要输入HttpServletRequest和HttpServletResponse这两个参数public String addUser(HttpServletRequest request) {System.out.println("addUser--------");String result = "addUser";request.setAttribute("result", result);// 跳转到annotation.jspreturn "annotation";}/* * @RequestMapping(value="/User/addUser",method=RequestMethod.GET) * //注解方式不需要输入HttpServletRequest和HttpServletResponse这两个参数 public * ModelAndView addUser(){ System.out.println("addUser--------"); String * result = "addUser"; //跳转到annotation.jsp return new * ModelAndView("annotation","result",result); } */}