spring boot template获取seesion
时间: 2023-09-13 11:10:34 浏览: 37
在Spring Boot中获取Session对象非常简单。您可以使用HttpServletRequest对象来获取Session。以下是一个示例:
```java
@GetMapping("/example")
public String example(HttpServletRequest request) {
HttpSession session = request.getSession();
// 在此处使用session对象
return "example";
}
```
您也可以使用@SessionAttributes注释来将模型属性存储在Session中,并通过@ModelAttribute注释访问它们。以下是一个示例:
```java
@Controller
@SessionAttributes("myAttribute")
public class MyController {
@ModelAttribute("myAttribute")
public String myAttribute() {
return "myValue";
}
@GetMapping("/example")
public String example() {
return "example";
}
@GetMapping("/otherExample")
public String otherExample(@ModelAttribute("myAttribute") String myAttribute) {
// 在此处使用myAttribute变量
return "other-example";
}
}
```
在上面的示例中,myAttribute属性被存储在Session中,并在otherExample方法中通过@ModelAttribute注释进行访问。