韩源-末日生存手册

SpringMVC的常用注解

@Controller

处理由DispatcherServlet 分发的请求,用于类上把用户请求的数据经过业务处理层处理之后封装成一个Model再把该Model 返回给对应的View 进行展示

@RequestMapping

处理请求地址映射的注解,可用于类或方法属性

value: 指定请求的实际地址,指定的地址可以是URI Template 模式
method: 指定请求的method类型, GET、POST、PUT、DELETE...
consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html
produces: 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回
params: 指定request中必须包含某些参数值是,才让该方法处理
headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求

@Resource、@Autowired

在bean的注入时使用写在字段和setter方法上。两者如果都写在字段上,那么就不需要再写setter方法

@PathVariable

用于将请求URL中的模板变量映射到功能处理方法的参数上,即取出uri模板中的变量作为参数

@requestParam

主要用于在SpringMVC后台控制层获取参数,类似一种是request.getParameter("name")

@ResponseBody

将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式

@Component

通用注解(可代替@Repository @Service @Controller)

@Repository

用于注解dao层,在daoImpl类上面注解

@ModelAttribute、@SessionAttributes

传递和保存数据,在不同的模型(model)和控制器之间共享数据

@PathVariable、@RequestParam

@PathVariable

获取请求路径上的变量值

@RequestParam

用来获得静态的URL请求入参 spring注解时action里用到

@RequestHeader、@CookieValue

@RequestHeader

可以把Request请求header部分的值绑定到方法的参数上。

@CookieValue

可以把Request header中关于cookie的值绑定到方法的参数上

@RequestParam, @RequestBody

@RequestParam

用来处理Content-Type: 为 application/x-www-form-urlencoded编码的内容,提交方式GET、POST

@RequestBody

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

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »