以下代码如想自己操作的话,我的环境是建立在这篇博客中。
IDEA大环境https://blog.youkuaiyun.com/monkey_wei/article/details/105788910
在开发控制器的时候,有时也需要保存对应的数据到这些数据中去,或者从中获取数据。而Spring MVC给予了支持,他的主要注解有三个:@RequestAttribute、@SessionAttribute和@SessionAttributes,它们的作用如下:
- @RequestAttribute获取HTTP的请求(request)对象属性值,用来传递给控制器的参数。
- @SessionAttribute在HTTP的会话(Session)对象属性值中,用来传递给控制器的参数。
- @SessionAttributes可以给它配置一个字符串数组,这个数组对应的是数据模型对应的键值对,然后将这些键值对保存到Session中。
注解@RequestAttribute
@RequestAttribute主要的作用是从HTTP的request对象中取出请求属性,只是它的范围周期是在一次请求中存在,首先建一个JSP文件。代码如下:
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
//设置请求属性
request.setAttribute("id",1L);
//转发给控制器
request.getRequestDispatcher("./attribute/requestAttribute.do").forward(request,response);
%>
</body>
</html>
上述代码首先设置了id为1L的请求属性,然后进行了转发控制器,这样将由对应的控制器去处理业务逻辑,下面用AttributeController去处理它,并且使用@RequestAttribute获取对应的属性,代码如下:
package com.ssm.example1.controller;
import com.ssm.example1.pojo.Role;
import com.ssm.example1.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
@Controller
@RequestMapping("/attribute")
public class AttributeController {
@Autowired
RoleService roleService=null;
@RequestMapping("/requestAttribute")
public ModelAndView reqAttr(@RequestAttribute("id") Long id){
ModelAndView mv=new ModelAndView();
Role role=roleService.getRole(id);
mv.addObject("role",role);
mv.setView(new MappingJackson2JsonView());
return mv;
}
}
这样就能够或当请求的id属性。
注解@SessionAttribute和注解@SessionAttributes
这两个注解和HTTP的会话对象有关,在浏览器和服务器保持联系的时候HTTP会创建一个会话对象,这样可以让我们在和服务器会话期间通过它读写会话对象的属性,缓存一定数据信息。在控制器中可以使用@SessionAttributes来设置对应的键值对,不过这个注解只能对类进行标注,不能对方法或者参数注解。他可以配置属性名称或者属性类型。它的作用是当这个类被注解后,Spring MVC执行完控制器的逻辑后,将数据模型中对应的属性名称或者属性类型保存到HTTP的Session对象中。
下面我们添加注解方法,代码如下:
@Controller
@RequestMapping("/attribute")
//可以配置数据模型的名称和类型,两者取或关系
@SessionAttributes(names={"id"},types = {Role.class})
public class AttributeController {
@Autowired
RoleService roleService=null;
@RequestMapping("/sessionAttributes")
public ModelAndView sessionAttrs(Long id){
ModelAndView mv=new ModelAndView();
Role role=roleService.getRole(id);
//根据类型,Session将会保存角色信息
mv.addObject("role",role);
//根据名称,Session将会保存id
mv.addObject("id",id);
//视图名称,定义跳转到另一个jsp文件
mv.setViewName("sessionAttribute");
return mv;
}
}
这个时候请求/attribute/sessionAttributes.do?id=1,那么它就会进入到sessionAttrs方法中,然后数据模型保存了一个id和角色,由于它们都满足了@SessionAttributes的配置,所以最后会保存到Session对象中。下面我们来编写跳转到的jsp页面,代码如下:
sessionAttribute.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java"
import="com.ssm.example1.pojo.Role" pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Role role=(Role)session.getAttribute("role");
out.println("id="+role.getId()+"<p/>");
out.println("roleName="+role.getRoleName()+"<p/>");
out.println("note="+role.getNote()+"<p/>");
Long id=(Long)session.getAttribute("id");
out.println("id="+id+"<p/>");
%>
</body>
</html>
运行结果: