`
goodsense
  • 浏览: 30641 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Spring mvc 注解介绍

    博客分类:
  • Java
阅读更多

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路径.

 

上面的两个注解也可以用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.     <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的注解进行介绍.

 

首先用@Controller("userController")标志一个类为控制器,@Controller负责一个bean注册到spring上下文中,bean的ID默认是类名首字母小写,也可以自己定义,下面我显示的把名字定义为userController.

Java代码  收藏代码
  1. package gd.hz.springmvc.controller;  
  2.   
  3. import org.springframework.stereotype.Controller;  
  4.   
  5. @Controller("userController")  
  6. public class UserController {  
  7.    
  8. }  

 

@RequestMapping,用来定义访问的URL,可以定义在类上也可以定义在方法上.把它定义在类方法上,类下面的所有方法访问路径都要它之下.看下面的例子:

Java代码  收藏代码
  1. package gd.hz.springmvc.controller;  
  2.   
  3. import org.springframework.stereotype.Controller;  
  4.   
  5. import org.springframework.web.servlet.ModelAndView;  
  6. import org.springframework.web.bind.annotation.RequestMapping;  
  7.   
  8. @Controller("userController")  
  9. @RequestMapping("user")  
  10. public class UserController {  
  11.     //当有两个以上的配置时value不可以省略,url路径可以省去"/"  
  12.     @RequestMapping("addUser")  
  13.     public ModelAndView addUser()  
  14.     {  
  15.         String str = "Springmvc 您好啊!!--->addUser" ;  
  16.         return new ModelAndView("hello");  
  17.     }  
  18. }  

 我们在UserController 上使用了@RequestMapping("user"),在方法上使用了@RequestMapping("addUser")这样的话,我们访问这个方法的URL为:http://localhost/项目名称/user/addUser,并返回一个名为hello的视图

 

 下面是注解@RequestMapping的一些常用用法,其中当有配置有二个属性时,value不可省略.另外"result" , str ,是返回数据,我的下一章会介绍.

Java代码  收藏代码
  1. package gd.hz.springmvc.controller;  
  2.   
  3. import javax.servlet.http.HttpServletRequest;  
  4.   
  5. import org.springframework.stereotype.Controller;  
  6. import org.springframework.web.bind.annotation.PathVariable;  
  7. import org.springframework.web.bind.annotation.RequestMapping;  
  8. import org.springframework.web.bind.annotation.RequestMethod;  
  9. import org.springframework.web.bind.annotation.RequestParam;  
  10. import org.springframework.web.servlet.ModelAndView;  
  11.   
  12. @Controller("userController")  
  13. @RequestMapping("user")  
  14. public class UserController {  
  15.       
  16.     //method=RequestMethod.POST  
  17.     @RequestMapping(value="addUser/tow" , method=RequestMethod.GET)  
  18.     public ModelAndView addUser2()  
  19.     {  
  20.         String str = "Springmvc 您好啊!!--->addUser2" ;  
  21.         return new ModelAndView("hello" , "result" , str);  
  22.     }  
  23.       
  24.     //URL为user/addUser/thread  
  25.     @RequestMapping("addUser/thread")  
  26.     public ModelAndView addUser3()  
  27.     {  
  28.         String str = "Springmvc 您好啊!!--->addUser3" ;  
  29.         return new ModelAndView("hello" , "result" , str);  
  30.     }  
  31.       
  32.     //访问文件二级路径 URL为user/addUser/thread 返回定义目录下的test目录的Hello.jsp文件  
  33.     @RequestMapping("addUser/six")  
  34.     public String addUser6(HttpServletRequest request)  
  35.     {  
  36.         String str = "Springmvc 您好啊!!--->addUser6" ;  
  37.         request.setAttribute("result", str);  
  38.         return "test/hello" ;  
  39.     }  
  40. }  

 

 

 @RequestParam:

 required :参数是否必须,boolean类型,可选项,默认为true

 value: 传递的参数名称,String类型,可选项,如果有值,对应到设置方法的参数 
 defaultValue:String类型,参数没有传递时为参数默认指定的值

默认情况下,当从表单或者以GET传来的参数跟Controller中的方法中的参数名不一样时就要使用此注解:

Java代码  收藏代码
  1. @RequestMapping("/addUser/eight")    
  2.     public void addUser8(int id, @RequestParam("name") String username) {    
  3.         //这样做进行URL请求访问这个方法的时候,就会先从request中获取参数id的值赋给参数变量id,  
  4.         //从request中获取参数name的值赋给参数变量username    
  5.     }    

 

 

@PathVariable:可以利用请求路径传值,当指定名称跟变量名不一样时需要指定名字,如下.

Java代码  收藏代码
  1. @RequestMapping("addUser/{seven}")  
  2.     public String addUser7(@PathVariable("seven") String name , HttpServletRequest request)  
  3.     {  
  4.         request.setAttribute("result", name);  
  5.         return "test/hello" ;  
  6.     }  

  {seven}当url传来什么他就是什么,但是当接收它的变量名不一样时需要用@PathVariable指定.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics