ITeye博客

Spring mvc 注解介绍

goodsense · 2013-06-19 15:59 · Java · 编程语言 · 注解 · spring mvc · Markdown

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. <bean
  14. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  15. <property name="prefix" value="/" />
  16. <property name="suffix" value=".jsp" />
  17. </bean>
  18. <!-- 声明一个Controller中使用多个方法 -->
  19. <bean id="parameterMethodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
  20. <!-- 传参数时用这个作为名称 -->
  21. <property name="paramName" value="action"></property>
  22. </bean>
  23. <!-- 声明DispatcherServlet不要拦截下面声明的目录 -->
  24. <mvc:resources location="/images/" mapping="/images/**"/>
  25. </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. <!-- 开启注解 DefaultAnnotationHandlerMapping:映射相应的类,DefaultAnnotationHandlerMapping相应的类方法 -->
  16. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
  17. <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
  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>

 上面我们开启了注解扫描,注入了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. <mvc:annotation-driven/>
  16. <bean
  17. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  18. <property name="prefix" value="/" />
  19. <property name="suffix" value=".jsp" />
  20. </bean>
  21. </beans>

 

 接下来新建一个控制器:

Xml代码  收藏代码

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

注解 @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. import org.springframework.stereotype.Controller;
  3. @Controller("userController")
  4. public class UserController {
  5. }

 

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

Java代码  收藏代码

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

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

 

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

Java代码  收藏代码

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

 

 

 @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指定.

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。