springmvc常用注解

来源:互联网 发布:宇喜多秀家数据 编辑:程序博客网 时间:2024/06/12 01:05

@Controller

该注解标记在一个类上,标记的类就成了一个SpringMVC Controller对象。分发处理器将会扫描使用了该注解的类的方法,并检测该方法是否使用了@RequestMapping注解。@Controller仅仅是定义一个控制器类,而使用@RequestMapping注解的方法才是真正处理请求的处理器。

加上注解后需要把这个控制器类交给Spring来管理。有以下两种方式:

  • 在SpringMVC的配置文件中定义Controller的bean对象。

    <bean class="com.spring.controller.MyController"/>

  • 在SpringMVC的配置文件中告诉Spring到哪里去寻找标记@Controller的控制器。

    <context:component-scan base-package="com.spring"/>

@RestController

使用在类上,相当于@Controller+@ResponseBody。如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp页面,配置的视图解析器InternalResourceViewResolver不起作用,返回的内容就是return里的内容。

@RequestMapping

用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。

6个属性:

  • value、method

    value:指定请求的实际地址。

    method:请求的method类型,GET/POST/PUT/DELETE等。

  • consumes、produces

    consumes:指定处理请求的提交内容类型(Content-Type),text/html。

    produces:指定返回的内容类型,仅当request请求头中的类型中包含该指定类型才返回。

  • params、headers

    params:指定request中必须包含某些参数值,才让该方法处理。

    headers:指定request中必须包含某些指定的header值,才让该方法处理。

@Resource和@Autowired

@Resource不是Spring的注解,javax.annotation.Resource,需要导入,Spring支持@Resource。

  • 相同点

    二者都是用于bean的注入,都可以写在字段和setter方法上。如果都写在字段上,则不需要写setter方法。

  • 不同点

    装配的方式不同

    @Autowired为Spring提供的注解,org.springframework.beans.factory.annotation.Autowired,按照byType注入。默认情况下,要求依赖对象必须存在,如果允许null值,可以设置required属性为false。若想按照byName来装配,可以结合@Qualifier一起使用。

    @Resource默认按照byName自动注入。该注解有两个重要的属性:nametype,Spring将该注解的name属性解析为bean的名字,type属性解析为bean的类型。使用什么属性就按什么自动注入。

@Resource装配顺序

  1. 如果同时指定了nametype,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常。
  2. 如果指定了name,则从上下文查找名称(id)匹配的bean装配,找不到抛出异常。
  3. 如果指定了type,则从上下文中找到类似匹配的唯一bean进行装配,找不到或找到多个会抛异常。
  4. 若未指定属性,按照byName进行装配,如果没有匹配,则回退为一个原始类型进行匹配。

@ModelAttribute和@SessionAttributes

SpringMVC支持使用@ModelAttribute和@SessionAttributes在不同的模型(model)和控制器之间共享数据。

  • @ModelAttribute

    • 用于方法上:通常用来在处理@RequestMapping之前,为请求绑定需要从后台查询的model
    • 用于参数上:用来通过名称对应,把相应名称的值绑定到注解的参数bean上,要绑定的值来源于:
    • @SessionAttributes启用的attribute对象上
    • @ModelAttribute用于方法上时指定的model对象
    • 上述两种情况都没有时,new一个需要绑定的bean对象,然后把request中按名称对应的方式把值绑定到bean上。
  • @SessionAttributes

    写在class上面,将值存放到session域中,用来绑定HttpSession中的attribute对象的值,便于在方法中的参数里使用。该注解有两个属性:

    • value:通过名字指定要使用的attribute对象
    • types:通过类型指定要使用的attribute对象

@PathVariable

用于将请求URL中的模板变量映射到处理方法的参数上。

@Controller  public class TestController {       @RequestMapping(value="/user/{userId}/roles/{roleId}",method = RequestMethod.GET)       public String getLogin(@PathVariable("userId") String userId,           @PathVariable("roleId") String roleId){           System.out.println("User Id : " + userId);           System.out.println("Role Id : " + roleId);           return "hello";       }       @RequestMapping(value="/product/{productId}",method = RequestMethod.GET)       public String getProduct(@PathVariable("productId") String productId){             System.out.println("Product Id : " + productId);             return "hello";       }       @RequestMapping(value="/javabeat/{regexp1:[a-z-]+}",             method = RequestMethod.GET)       public String getRegExp(@PathVariable("regexp1") String regexp1){             System.out.println("URI Part 1 : " + regexp1);             return "hello";       }  }

@RequestParam

用于在SpringMVC后台控制层获取参数,类似request.getParameter("name"),有3个参数:

  • defaultValue:默认值
  • required:设置是否是必须要传入的参数
  • value:接收传入参数的类型

@RequestBody

用来处理Content-Type:不是application/x-www-form-urlencoded编码的内容,例如application/json,application/xml等。

通过使用HandlerAdapter配置的HttpMessageConverters来解析post data body,然后绑定到相应的bean上。

@ResponseBody

用于将Controller方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到response对象的body数据区。比如json格式。

@Component

交给Spring管理。不建议使用。

@Repository

用于注解DAO层。且在XML配置文件中启用Bean的自动扫描功能。

@Repository只能标注在DAO层,它能将所标注的类中抛出的数据访问异常封装为Spring的数据访问异常类型。

原创粉丝点击