SpringMVC系列之(三)常用注解
常用注解
1. RequestMapping
RequestMapping可以放在类上和方法上,放在类上表示一级目录,或表示某一个具体的模块
属性
- path和value属性的作用相同
- method决定方法的请求方式
- params:请求必须包含的参数
- headers:请求必须包含的请求头
以上的属性出现多个,需要同时满足
2. RequestParam
2.1 应用场景
数据绑定时,前端传到后端的参数名与控制器中的方法参数名不同,通过RequestParam可建立映射关系,建立前端请求参数与Controller接收参数变量命名之间的映射关系
2.2 常用属性
属性 | 取值 | 默认值 | 功能 |
---|---|---|---|
name | 字符串 | "" | 前端传过来的参数名 |
required | 布尔值 | true | 如:@RequestParam("name"),表示请求中必须携带name参数 |
2.3 实例
JSP
<%--
Created by IntelliJ IDEA.
User: 商务小本本
Date: 2022/3/3
Time: 23:27
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h3>入门程序</h3>
<a href="hh/hello?name=lisi&age=12">入门程序</a>
</body>
</html>
Controller
package cn.itcast.controller;
import cn.itcast.domain.User;
import cn.itcast.utils.StringToDateConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author 商务小本本
*/
@Controller
@RequestMapping("/hh")
public class HelloController {
@RequestMapping(path = "/hello")
public String sayHello(@RequestParam("name") String username, @RequestParam("age") String password){
System.out.println(username + "\t" + password);
return "success";
}
}
3. RequestBody
3.1 应用场景
获取整个请求体,与请求参数绑定不同,获取的是key=value&key=value这种形式
注:get请求方式不适用,get方式没有请求体
3.2 常用属性
属性 | 取值 | 默认值 | 功能 |
---|---|---|---|
required | 布尔值 | true | 如:@RequestBody(required = true),表示请求中必须存在请求体 |
3.3 实例
JSP
<%--
Created by IntelliJ IDEA.
User: 商务小本本
Date: 2022/3/3
Time: 23:27
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h3>入门程序</h3>
<form action="hh/hello" method="post">
姓名:<input type="text" name="name">
年龄:<input type="text" name="age">
<input type="submit" value="提交">
</form>
</body>
</html>
Controller
package cn.itcast.controller;
import cn.itcast.domain.User;
import cn.itcast.utils.StringToDateConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author 商务小本本
*/
@Controller
@RequestMapping("/hh")
public class HelloController {
@RequestMapping(path = "/hello")
public String sayHello(@RequestBody(required = false) String body){
System.out.println(body);
return "success";
}
}
4. PathVariable
4.1 关于Restful编程风格
Controller中各个方法的请求地址都相同,通过请求方式区分执行哪个方法,当几个方法的地址和请求方式都相同时,通过类似testRequest/{id}的方式进行区分
利于缓存
4.2 应用场景
绑定url中的占位符
4.3 实例
JSP
<%--
Created by IntelliJ IDEA.
User: 商务小本本
Date: 2022/3/3
Time: 23:27
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h3>入门程序</h3>
<form action="hh/hello" method="post">
姓名:<input type="text" name="name">
年龄:<input type="text" name="age">
<input type="submit" value="提交">
</form>
</body>
</html>
Controller
package cn.itcast.controller;
import cn.itcast.domain.User;
import cn.itcast.utils.StringToDateConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author 商务小本本
*/
@Controller
@RequestMapping("/hh")
public class HelloController {
@RequestMapping(path = "/hello/{id}")
public String sayHello(@PathVariable(name = "id") String id){
System.out.println(id);
return "success";
}
}
5. RequestHeader
5.1 应用场景
获取请求头的内容
5.2 实现
Jsp
<%--
Created by IntelliJ IDEA.
User: 商务小本本
Date: 2022/3/3
Time: 23:27
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h3>入门程序</h3>
<a href="hh/hello">入门程序</a>
</body>
</html>
Controller
package cn.itcast.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author 商务小本本
*/
@Controller
@RequestMapping("/hh")
public class HelloController {
@RequestMapping(path = "/hello")
public String sayHello(@RequestHeader("Upgrade-Insecure-Requests") String header){
System.out.println(header);
return "success";
}
}
6. CookieValue
6.1 应用场景
直接获取指定Cookie的值
6.2 实现
JSP
<%--
Created by IntelliJ IDEA.
User: 商务小本本
Date: 2022/3/3
Time: 23:27
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h3>入门程序</h3>
<a href="hh/cookie">Cookie</a>
</body>
</html>
Controller
package cn.itcast.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author 商务小本本
*/
@Controller
@RequestMapping("/hh")
public class HelloController {
@RequestMapping(path = "/cookie")
public String testCookie(@CookieValue("JSESSIONID") String cookie){
System.out.println(cookie);
return "success";
}
}
6.3 关于JSESSIONID
7. ModelAttribute
7.1 应用场景
当表单提交数据不是完整的实体类数据时,保证没有提交数据的字段使用数据库对象原来的数据。
在控制器所有方法执行之前执行
7.2 实例
7.2.1 修饰有返回值的方法
实体类有四个属性,其中,name、gender、money从前端接收,date是根据name从数据库中取到的,不是来源于客户端。
JSP
<%--
Created by IntelliJ IDEA.
User: 商务小本本
Date: 2022/3/3
Time: 23:27
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h3>入门程序</h3>
<form action="hh/hello" method="post">
姓名:<input type="text" name="name">
性别:<input type="text" name="gender">
金额:<input type="text" name="money">
<input type="submit" value="提交">
</form>
</body>
</html>
7.2.2 修饰无返回值的方法
package cn.itcast.controller;
import cn.itcast.domain.User;
import cn.itcast.utils.StringToDateConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;
/**
* @author 商务小本本
*/
@Controller
@RequestMapping("/hh")
public class HelloController {
@RequestMapping(path = "/hello")
public String sayHello(@ModelAttribute("aaa") User user){
System.out.println(user);
return "success";
}
@ModelAttribute
public void addDate(String name, Map<String, User> map){
//模拟根据name查询数据库的过程,为了从数据库中取到date的值
User user = new User();
user.setDate(new Date());
user.setGender("南");
user.setMoney(100);
user.setName(name);
map.put("aaa", user);
}
}
8. SessionAttributes
8.1 应用场景
与Session域对象有关,实现控制器方法间的参数共享
8.2 实例
index.jsp
<%--
Created by IntelliJ IDEA.
User: 商务小本本
Date: 2022/3/3
Time: 23:27
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h3>入门程序</h3>
<a href="hh/saveMsg">存入msg</a>
<a href="hh/checkMsg">查看msg</a>
<a href="hh/delMsg">删除msg</a>
</body>
</html>
Controller
package cn.itcast.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;
/**
* @author 商务小本本
*/
@Controller
@RequestMapping("/hh")
//把msg=妹妹存入session域对象中
@SessionAttributes("msg")
public class HelloController {
@RequestMapping(path = "/saveMsg")
public String saveMsg(Model model){
//Model中存入的数据底层会被存到request域对象中
model.addAttribute("msg", "妹妹");
return "success";
}
@RequestMapping(path = "/checkMsg")
public String checkMsg(ModelMap modelMap){
String msg = (String) modelMap.get("msg");
System.out.println("获取到的Msg为:" + msg);
return "success";
}
@RequestMapping(path = "/delMsg")
public String delMsg(SessionStatus status){
status.setComplete();
return "success";
}
}
success.jsp
<%--
Created by IntelliJ IDEA.
User: 商务小本本
Date: 2022/3/3
Time: 23:37
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h3>入门成功!</h3>
${sessionScope}
</body>
</html>
9. @ResponseBody
这个注解标注在类上,表明这个类的所有方法返回的数据直接写给浏览器,如果返回的是对象就转为json数据。
10. @RestController
@RestController等同于@ResponseBody和@Controller的组合
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术