常用注解

  • @RequestParam

    把请求中指定名称的参数给控制器中的形参赋值

    package com.oylong.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    
    @Controller
    public class HelloController {
        @RequestMapping(path = "/hello")
        public String hello(@RequestParam(name = "uname",required = false) String username) {
            System.out.println(username);
            return "success";
        }
    }

    name = "uname"表示将参数为uname的值赋值给username,value属性与name属性相同required = false表示这个参数的名称不是必须为uname

  • @RequestBody

    用于获取请求体的内容,get请求不适用,得到的是key0=value0&key1=value1...这样结构的数据

  • @PathVariable

    用于绑定url中的占位符,适用于Restful的编程风格。

  • @RequestHeader

    用于获取请求消息头

  • @CookieValue

    用于把指定Cookie名称的值传入控制器方法参数

  • @ModelAttribute

    可用于修饰方法可参数

    • 出现在方法上表示当前方法会在控制器的方法之前执行
    • 出现在参数上,表示获取指定的数据给参数赋值
  • @SessionAttributes

    用于多次执行控制器方法间的参数共享

最后修改:2020 年 03 月 28 日
如果觉得我的文章对你有用,请随意赞赏