springMVC的常用注解

一、常用注解

     1.RequestParam

       1)作用:把请求中指定名称的参数给控制器中的形参赋值。 

             属性:value:请求参数中的名称。 

                        required:请求参数中是否必须提供此参数。

                                       默认值:true。表示必须提供,如果不提供将报错。 

       2)使用示例

           

<a href="anno/testRequestParam?name=哈哈">RequestParam</a>
@Controller
@RequestMapping("/anno")
public class AnnoController {
    @RequestMapping("/testRequestParam")
    public String testRequestParam(@RequestParam(name="name") String username){
        System.out.println("执行了-----------");
        System.out.println(username);
        return "success";
    }

    2.RequestBody

        1) 使用说明

             作用:用于获取请求体内容。直接使用得到是 key=value&key=value...结构的数据。 

                             get 请求方式不适用。 

                  属性:required:是否必须有请求体。默认值是:true。当取值为 true 时,get 请求方式会报错。如果取值 为 false,get 请求得到是 null。 

          2)使用示例

                 

<form action="anno/testRequestBody" method="post">
    用户姓名:<input type="text" name="username"/><br/>
    用户年龄:<input type="text" name="age"/><br/>
    <input type="submit" value="提交"/>
</form>
@Controller
@RequestMapping("/anno")
public class AnnoController {
@RequestMapping("/testRequestBody")
        public String testRequestBody(@RequestBody String body){
            System.out.println("执行了-----------");
            System.out.println(body);
            return "success";
        }
}

    3.PathVaribale

        1)  使用说明

            作用:用于绑定 url 中的占位符。例如:请求 url 中 /delete/{id},这个{id}就是 url 占位符。 

            属性:value:用于指定 url 中占位符名称。 

                          required:是否必须提供占位符。

         2)使用示例:

               

<a href="anno/testPathVariable/10">RequestParam</a><br/>
@RequestMapping("/testPathVariable/{sid}")
    public String testPathVariable(@PathVariable(name="sid") String id){
        System.out.println("执行了-----------");
        System.out.println(id);
        return "success";
    }

       4.RequestHeader

           1) 使用说明

               作用:用于获取请求消息头

                属性:value:提供消息头名称  required:是否必须有此消息头 

         5.CookieValue

             1)使用说明

                 作用:用于把指定 cookie 名称的值传入控制器方法参数。 

                 属性:value:指定 cookie 的名称。 

                            required:是否必须有此 cookie。         

              2)使用示例                 

@RequestMapping("/testCookieValue")
    public String testCookieValue(@CookieValue(value="JSESSIONID") String cookieValue){
        System.out.println("执行了-----------");
        System.out.println(cookieValue);
        return "success";
    }

          6.ModelAttribute

            1) 使用说明

               作用:出现在方法上,表示当前方法会在控制器的方法执行之前,先执行。它可以修饰没有返回值的方法,也可 以修饰有具体返回值的方法。 

                         出现在参数上,获取指定的数据给参数赋值。

               属性:value:用于获取数据的 key。key 可以是 POJO 的属性名称,也可以是 map 结构的 key。

             

<form action="testModelAttribute" method="post">
    用户姓名:<input type="text" name="uname"/><br/>
    用户年龄:<input type="text" name="age"/><br/>
    <input type="submit" value="提交"/>
</form>
 @RequestMapping("/testModelAttribute")
    public String testModelAttribute(){
        System.out.println("执行了-----------");
        return "success";
    }
    @ModelAttribute
    public void showUser(String uname, Map<String,User> map) {
        System.out.println("showUser----------");
       
    }

//结果showUser会先执行
//后执行
 @RequestMapping("/testModelAttribute")
    public String testModelAttribute(@ModelAttribute("abc") User user){
        System.out.println("执行了-----------");
        System.out.println(user);
        return "success";
    }

//先执行
    @ModelAttribute
    public void showUser(String uname, Map<String,User> map) {
        System.out.println("showUser----------");
        User user = new User();
        user.setUname(uname);
        user.setAge(15);
        user.setDate(new Date());
         map.put("abc",user);
    }

      7.SessionAttribute

          1)使用说明

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

             属性:value:用于指定存入的属性名称

                        type:用于指定存入的数据类型。 

           2)使用示例

<a href="anno/testSessionAttribute">testSessionAttribute</a><br/>

<a href="anno/getSessionAttribute">getSessionAttribute</a><br/>

<a href="anno/delSessionAttribute">delSessionAttribute</a><br/>
@Controller
@RequestMapping("/anno")
@SessionAttributes(value = {"msg"})   //该注解用于类上把“msg=张三”存入到session域中
public class AnnoController {
   /**
     * SessionAttribute的注解
     */
    @RequestMapping("/testSessionAttribute")
    public String testSessionAttribute(Model model){
        System.out.println("执行了-----------");
        //底层会存储到request域对象中
        model.addAttribute("msg","张三");
        return "success";
    }

    /**
     * 从Session中取值
     * @param modelMap
     * @return
     */
    @RequestMapping("/getSessionAttribute")
    public String getSessionAttribute(ModelMap modelMap){
        System.out.println("执行了-----------");
        //底层会存储到request域对象中
        String msg = (String) modelMap.get("msg");
        System.out.println(msg);
        return "success";
    }
    /**
     * 从Session删除值
     * @param
     * @return
     */
    @RequestMapping("/delSessionAttribute")
    public String delSessionAttribute(SessionStatus status){
        System.out.println("执行了-----------");
        //底层会存储到request域对象中
        status.setComplete();
        return "success";
    }

}

        

             

原文地址:https://www.cnblogs.com/cqyp/p/12522429.html