SpringMvc---使用session将数据传输到页面上
1、先放上接收数据的jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
原生的:${type}
Hello ${requestScope.type}.
Hello ${sessionScope.type}.
</body>
</html>
2、通过servlet去读取session
2.1、普通方式传递数据
@RequestMapping("/servletApi/session")
public String session01(HttpSession session){
session.setAttribute("type","servletApi-session");
return "main";
}
2.2、使用Autowired的方式传递数据(推荐使用)
@Autowired
private HttpSession session;
@RequestMapping("/autowired/session")
public String session02(){
session.setAttribute("type","autowired-session");
return "main";
}
3、通过springMvc提供的注解去读取session
3.1、@SessionAttributes
@SessionAttributes("type")
public class DAMController {
@RequestMapping("/modelAndView")
public ModelAndView map(){
ModelAndView modelAndView = new ModelAndView("main");
modelAndView.addObject("type","modelAndView");
return modelAndView;
}
}
3.2、@SessionAttribute
@RequestMapping("/annotation/session")
public String session03(value = "type",required = false) String type){
System.out.println(type);
return "main";
}