Spring MVC学习笔记7——SessionAttributes注解

SessionAttributes注解只能放到类上面,不能放在方法上面。

SessionAttributes注解除了可以通过属性名指定需要放到会话中的属性外(value属性值),还可以通过模型属性的对象类型指定哪些模型属性需要放到会话中(types属性值)。

testSessionAttributes类

@SessionAttributes(value={"user"},types={String.class})
@Controller
public class Hello{

    @RequestMapping("/testSessionAttributes")
    public String testSessionAttributes(Map<String, Object> map){
		User user = new User("Tom", "123456789", "[email protected]", 16);
		map.put("user", user);
		map.put("school", "WBU");
		return "success";
		
	}
}

success.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

	<h1>Success page</h1>
	
	request user:${requestScope.user }
	<br><br>
	
	
	sessiong user:${sessionScope.user }
	<br><br>
	
	request school:${requestScope.school }
	<br><br>
	
	
	sessiong school:${sessionScope.school }
	<br><br>
</body>
</html>
发布了65 篇原创文章 · 获赞 31 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_41286145/article/details/88802542