程序员社区

SpringMvc中常用的注解

文章目录

    • 1.@RequestParam
    • 2.@RequestBody
    • 3.@PathVaribale
    • 4.@RequestHeader
    • 5.@CookieValue注解
    • 6.@ModuleAttribute
      • 6.1 作用在方法上
      • 6.2 作用在参数上
    • 7@SessionAttribute

1.@RequestParam

请求参数

  1. 作用:把请求中的指定名称的参数传递给控制器的形参赋值
  2. 属性:
    value:请求参数中的名称
    required:请求参数中是否必须提供此参数
<html>
<head>
    <title>Title</title>
</head>
<body>
    <a href="anno/testRequestParam?name=哈哈">RequestParam</a>
</body>
</html>
@Controller
@RequestMapping("/anno")
public class AnnoController {
    @RequestMapping("/testRequestParam")
    public String testRequestParam(@RequestParam(value="name") String username){
        System.out.println(username);
        return "success";
    }
}

在这里插入图片描述

2.@RequestBody

@Data
public class User implements Serializable {
    private String username;
    private int age;
}
<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(body);
        return "success";
    }
}

请求体:只有post请求方法才有请求体

  1. 作用:获取请求体中的美容
  2. 属性:required:是否必须与请求体,默认值为true
    在这里插入图片描述

3.@PathVaribale

  1. 作用:拥有绑定url中的占位符的。例如:url中有/delete/{id},{id}就是占位符
  2. 属性:value:指定url中的占位符名称
    在这里插入图片描述

4.@RequestHeader

作用:获取指定请求头的值
属性value:请求头的名称
在这里插入图片描述

5.@CookieValue注解

  1. 作用:用于把指定Cookie名称的值传入控制器方法参数中
  2. 属性:
    value:指定cookie的名称
    required:是否必须有此cookie
    在这里插入图片描述

6.@ModuleAttribute

  1. 作用:
    出现在方法上:表示当前方法会在控制器方法之前执行
    出现在参数上:获取指定数据给参数赋值

6.1 作用在方法上

表示当前方法会在控制器方法之前执行
在这里插入图片描述

6.2 作用在参数上

@Data
public class User implements Serializable {
    private String username;
    private int age;
    private Date date;
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <form action="anno/testModuleAttribute" method="post">
        用户姓名:<input type="text" name="username"/><br>
        用户年龄:<input type="text" name="age"/><br>
        <input type="submit" value="提交"/>
    </form>
</body>
</html>
@Controller
@RequestMapping("/anno")
public class AnnoController {
@RequestMapping("/testModuleAttribute")
public String testModuleAttribute(@ModelAttribute("abc") User user){
    System.out.println("testModuleAttribute执行了....");
    System.out.println(user);
    return "success";
}
@ModelAttribute
public void showUser(String username, Map<String,User> map){
    System.out.println("showUser执行了....");
    User user  = new User();
    user.setUsername(username);
    user.setAge(20);
    user.setDate(new Date());
    map.put("abc",user);
	}
}

在这里插入图片描述

7@SessionAttribute

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <form action="anno/testModuleAttribute" method="post">
        用户姓名:<input type="text" name="username"/><br>
        用户年龄:<input type="text" name="age"/><br>
        <input type="submit" value="提交"/>
    </form>
    <a href="anno/testSessionAttributes">testSessionAttributes</a>
    <a href="anno/getSessionAttributes">getSessionAttributes</a>
    <a href="anno/deleteSessionAttributes">deleteSessionAttributes</a>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
   ${requestScope}
</body>
</html>
@Controller
@RequestMapping("/anno")
@SessionAttributes(value = {"msg"})//把msg=美美存储到session域中
public class AnnoController {
    @RequestMapping("/testSessionAttributes")
    public String testSessionAttributes(Model model){
        System.out.println("testSessionAttributes执行了....");
        model.addAttribute("msg","美美");
        return "success";
    }
    @RequestMapping("/getSessionAttributes")
    public String getSessionAttributes(ModelMap modelMap){
        System.out.println("getSessionAttributes执行了....");
        String msg = (String)modelMap.get("msg");
        System.out.println(msg);
        return "success";
    }
    @RequestMapping("/deleteSessionAttributes")
    public String deleteSessionAttributes(SessionStatus status){
        System.out.println("deleteSessionAttributes执行了....");
        status.setComplete();
        return "success";
    }
}

赞(0) 打赏
未经允许不得转载:IDEA激活码 » SpringMvc中常用的注解

相关推荐

  • 暂无文章

一个分享Java & Python知识的社区