Spring mvc系列三之 开启注解

来源:互联网 发布:现货黄金源码 编辑:程序博客网 时间:2024/06/07 06:13

http://mylfd.iteye.com/blog/1887319

spring mvc 基于注解的使用,相当于配置文件的使用简单的多.下面讲一下spring mvc 注解的使用

先首确保已经把spring mvc的环境搭配好.这里可以看我的前一篇文章Spring mvc系列一之 Spring mvc简单配置.

先看一下再未使用注解前,spring mvc的配置文件

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.     xsi:schemaLocation="        
  7.            http://www.springframework.org/schema/beans        
  8.            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd        
  9.            http://www.springframework.org/schema/context        
  10.            http://www.springframework.org/schema/context/spring-context-3.0.xsd       
  11.            http://www.springframework.org/schema/mvc        
  12.            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  
  13.            
  14.     <bean  
  15.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  16.         <property name="prefix" value="/" />  
  17.         <property name="suffix" value=".jsp" />  
  18.     </bean>  
  19.       
  20.     <!-- 声明一个Controller中使用多个方法 -->  
  21.     <bean id="parameterMethodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">  
  22.         <!-- 传参数时用这个作为名称 -->  
  23.         <property name="paramName" value="action"></property>  
  24.     </bean>  
  25.       
  26.     <!-- 声明DispatcherServlet不要拦截下面声明的目录 -->  
  27.     <mvc:resources location="/images/" mapping="/images/**"/>  
  28.       
  29. </beans>  

 

上面我们声明在一个控制器中使用多个方法.

 

再看看spring mvc使用注解配置文件的配置:

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.     xsi:schemaLocation="        
  7.            http://www.springframework.org/schema/beans        
  8.            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd        
  9.            http://www.springframework.org/schema/context        
  10.            http://www.springframework.org/schema/context/spring-context-3.0.xsd       
  11.            http://www.springframework.org/schema/mvc        
  12.            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  
  13.     <!-- 开启注解扫描功能 -->  
  14.     <context:component-scan base-package="gd.hz.springmvc.controller"></context:component-scan>       
  15.       
  16.     <!-- 开启注解 DefaultAnnotationHandlerMapping:映射相应的类,DefaultAnnotationHandlerMapping相应的类方法 -->  
  17.     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>  
  18.     <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>  
  19.       
  20.     <bean  
  21.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  22.         <property name="prefix" value="/" />  
  23.         <property name="suffix" value=".jsp" />  
  24.     </bean>  
  25.   
  26. </beans>  

 上面我们开启了注解扫描,注入了AnnotationMethodHandlerAdapter作用是对有RequestMapping注解的控制器进行HTTP路径、HTTP方法和请求参数解析.DefaultAnnotationHandlerMapping作用是映射处理程序方法级别的HTTP路径.在spring 3.1之后由RequestMappingHandlerAdapter和RequestMappingHandlerMapping代替.

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.     xsi:schemaLocation="        
  7.            http://www.springframework.org/schema/beans        
  8.            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd        
  9.            http://www.springframework.org/schema/context        
  10.            http://www.springframework.org/schema/context/spring-context-3.0.xsd       
  11.            http://www.springframework.org/schema/mvc        
  12.            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  
  13.     <!-- 开启注解扫描功能 -->  
  14.     <context:component-scan base-package="gd.hz.springmvc.controller"></context:component-scan>       
  15.       
  16.     <!-- 开启注解 DefaultAnnotationHandlerMapping:映射相应的类,DefaultAnnotationHandlerMapping相应的类方法 -->  
  17.     <!--   
  18.     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>  
  19.     <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>   
  20.     -->  
  21.       
  22.     <!-- spring 3.1之后由RequestMappingHandlerAdapter和RequestMappingHandlerMapping代替 -->  
  23.     <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>  
  24.     <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>    
  25.   
  26.       
  27.     <bean  
  28.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  29.         <property name="prefix" value="/" />  
  30.         <property name="suffix" value=".jsp" />  
  31.     </bean>  
  32.       
  33.     <!-- 声明DispatcherServlet不要拦截下面声明的目录 -->  
  34.     <mvc:resources location="/images/" mapping="/images/**"/>  
  35. </beans>  

 

 

上面的两个注解也可以用mvc标签表示,spring 3.1之后默认注入的是RequestMappingHandlerAdapter和RequestMappingHandlerMapping:

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.     xsi:schemaLocation="        
  7.            http://www.springframework.org/schema/beans        
  8.            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd        
  9.            http://www.springframework.org/schema/context        
  10.            http://www.springframework.org/schema/context/spring-context-3.0.xsd       
  11.            http://www.springframework.org/schema/mvc        
  12.            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  
  13.     <!-- 开启注解扫描功能 -->  
  14.     <context:component-scan base-package="gd.hz.springmvc.controller"></context:component-scan>       
  15.   
  16.     <mvc:annotation-driven/>  
  17.       
  18.     <bean  
  19.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  20.         <property name="prefix" value="/" />  
  21.         <property name="suffix" value=".jsp" />  
  22.     </bean>  
  23. </beans>  

 

 接下来新建一个控制器:

Xml代码  收藏代码
  1. package gd.hz.springmvc.controller;  
  2.   
  3. import org.springframework.stereotype.Controller;  
  4. import org.springframework.web.bind.annotation.RequestMapping;  
  5. import org.springframework.web.servlet.ModelAndView;  
  6.   
  7. @Controller("userController")  
  8. public class UserController {     
  9.     @RequestMapping("addUser")  
  10.     public ModelAndView addUser()  
  11.     {  
  12.         return new ModelAndView("hello");  
  13.     }  
  14. }  

注解 @Controller标志该类为控制器.注解@RequestMapping,映射相应的路径.

我们可以这样访问:http://localhost/Springmvc/addUser , 其中 addUser 就是@RequestMapping映射的名称,名字随意.下一章我们将对spring mvc的注解进行介绍.