@RequestMapping
源码:
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
}
作用:
用来建立请求 URL 与和处理方法之间的对应关系。
出现位置:
类上:
请求 URL 的第一级访问目录。此处不写的话,就相当于应用的根目录。写的话需要以/开头。它出现的目的是为了使我们的 URL 可以按照模块化管理:
例如:
用户模块:
/user/add
/user/update
/user/delete
加粗的部分就要使用@RequestMapping
注解来配置,使我们的 URL 更加精细。
方法上:
请求 URL 的第二级访问目录。
属性:
value:用于指定请求的 URL。它和 path 属性的作用是一样的。
method:用于指定请求的方法。
类型:RequestMethod[ ](RequestMethod是一个枚举类)
取值:RequestMethod + . + GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE任意一种请求方法。
params:用于指定限制请求参数的条件。它支持简单的表达式。要求请求参数的 key 和 value 必须满足配置的要求。
类型:String[ ]
例如:
params = {“username”},表示请求参数中必须要有 username 。
params = {"!sex"},表示请求参数中不能有 sex 。
params = {“age=22”},表示请求参数中必须要有 age 的值必须为 22 。
params = {“money!=100”},表示请求参数中必须要有 money 的值不能为 100 。
headers:用于指定限制请求消息头的条件。它的限制规则与 params 属性一样。
@RequestParam
源码:
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestParam {
}
作用:
把请求中指定名称的参数赋值给控制器中方法的形参。
属性:
value:用于将请求参数和形参绑定。和 name 属性作用一样。
required:表示请求中是否必须提供 value 中所指定的请求参数。默认值:true 。表示必须提供,如果不提供将报错。
@RequestBody
源码:
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestBody {
}
作用:
用于获取请求体内容。get 方式没有请求体,所以不适用。
属性:
required:表示必须有请求体。默认值是:true 。当取值是 true 时,get 请求方式会报错。如果取值为 false,get 请求得到的是 null 。
@PathVaribale
源码:
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PathVariable {
}
作用:
用于绑定url中的占位符。例如:请求 URL 中 /delete/{id}
,这个{id}
就是 URL 占位符。
URL 支持占位符是 spring3.0 之后加入的。是 springmvc 支持 rest 风格 URL 的一个重要标志。关于 rest 风格,点击这里了解。
属性:
value:用于指定 URL 中占位符名称。
required:是否必须提供占位符。
示例:
@RequestMapping("/pathVariable/{id}")
public String testPathVariable(@PathVariable("id") Integer uid) {
System.out.println(uid);
}
@RequestHeader
源码:
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestHeader {
}
作用:
用于获取请求消息头。
属性:
value:提供消息头名称。和 name 属性作用一样。
required:是否必须有此消息头。
@CookieValue
源码:
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CookieValue {
}
作用:
用于把指定 cookie 名称的值传入控制器方法参数。
属性:
value:value:指定 cookie 的名称。和 name 属性作用一样。
required:是否必须有此 cookie 。
@ModelAttribute
源码:
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ModelAttribute {
}
作用:
该注解是 SpringMVC4.3 版本以后新加入的。它可以用于修饰方法和参数。
出现位置:
方法上:
出现在方法上,表示当前方法会在控制器的方法执行之前,先执行。它可以修饰没有返回值的方法,也可以修饰有具体返回值的方法。
参数上:
出现在参数上,获取指定的数据给参数赋值。
属性:
value:用于获取数据的 key。key 可以是 POJO 的属性名称,也可以是 map 结构的 key 。
应用场景:
当表单提交数据不是完整的实体类数据时,保证没有提交数据的字段使用数据库对象原来的数据。
例如:
我们在编辑一个用户时,用户有一个创建信息字段,该字段的值是不允许被修改的。在提交表单数据是肯定没有此字段的内容,一旦更新会把该字段内容置为 null ,此时就可以使用此注解解决问题。
@SessionAttributes
源码:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface SessionAttributes {
}
作用:
用于多次执行控制器方法间的参数共享。
属性:
value:用于指定存入的属性名称。
type:用于指定存入的数据类型。
@ModelAttribute使用示例
作用一: 作用在方法上,则此方法在所有控制器方法之前执行。
Jsp代码:
<a href="${pageContext.request.contextPath}/testModelAttribute1">测试</a>
Controller代码:
@ModelAttribute
public void before() {
System.out.println("before方法执行了");
}
@RequestMapping("/testModelAttribute1")
public String testModelAttribute1() {
System.out.println("testModelAttribute1方法执行了");
return null;
}
输出:
before方法执行了
testModelAttribute1方法执行了
作用二: 案例准备:用户注册后信息(username,password,birthday)存于数据库中,当执行更新操作时,因为某一字段(birthday)我们设置不允许用户修改,没有将数据库中 birthday 字段发送给前端,导致用户更新提交后,前端传过来的birthday字段内容为 null ,如果直接将请求参数封装到实体类中,然后持久化,肯定会造成 birthday 字段的数据丢失。@ModelAttribute 注解可以解决这个问题。
Jsp代码:
<form method="post" action="${pageContext.request.contextPath}/testModelAttribute2">
<input type="text" name="username"/>
<input type="text" name="password"/>
<input type="submit" value="提交"/>
</form>
实现方式一:@ModelAttribute修饰的方法带有返回值。
Controller代码:
@ModelAttribute
public User before(String username) {
//首先根据请求参数username查询数据库,将数据库中的信息封装到user中
User user = userService.findByUsername(username);
return user;//返回user,供testModelAttribute2方法使用
}
@RequestMapping("/testModelAttribute1")
//定义一个User类型变量接收before方法返回的对象,同时
//spring mvc会根据请求中某些参数的有无来决定覆盖掉User对象中的哪些属性。
public String testModelAttribute2(User user) {
System.out.println("testModelAttribute2方法执行了" + user);
return null;
}
实现方式二:@ModelAttribute修饰的方法不带返回值。
Controller代码:
@ModelAttribute
//需要定义Map类型的形参,用于存放从数据库中查询得到的user对象
public void before(String username, Map<String,User> map) {
User user = userService.findByUsername(username);
map.put("u", user);
}
@RequestMapping("/testModelAttribute1")
//@ModelAttribute注解作用于参数上,指定 map 中的 key 来接收 User对象
public String testModelAttribute2(@ModelAttribute("u") User user) {
System.out.println("testModelAttribute2方法执行了" + user);
return null;
}
@SessionAttributes使用示例
作用: 多个控制方法间数据共享。案例准备:三个功能,用户点击登录,将登录信息存入session。用户进行更新操作,首先要判断用户是否登录,根据判断结果跳转到更新页面或者登录页面。用户登出。
Jsp代码:
<form method="post" action="${pageContext.request.contextPath}/login">
<input type="text" name="username"/>
<input type="text" name="password"/>
<input type="submit" value="提交"/>
</form>
<a href="${pageContext.request.contextPath}/update">更新</a>
<a href="${pageContext.request.contextPath}/loginOut">登出</a>
Controller代码:
@Controller
//把request域对象中key为"u"的对象存入session
@SessionAttributes("u")
public class FirstController {
/**
* 登录
* @param user
* @param model
* @return
* Model 是 spring 提供的一个接口,该接口有一个实现类 ExtendedModelMap
* 该类继承了 ModelMap ,而 ModelMap 是 LinkedHashMap 子类。
*/
@RequestMapping("/login")
public String login(User user, Model model) {
//底层会把user存入request域对象中
model.addAttribute("u", user);
return "index";
}
/**
* 更新
* @param modelMap
* @return
*/
@RequestMapping("/update")
public String update(ModelMap modelMap) {
//从session中取出user对象
User u = (User) modelMap.get("u");
System.out.println(u);
return u != null ? "update" : "login";
}
/**
* 登出
* @param sessionStatus
*/
@RequestMapping("/loginOut")
public void loginOut(SessionStatus sessionStatus) {
//销毁session
sessionStatus.setComplete();
System.out.println("用户登出,session被销毁");
}
}