简体   繁体   English

无法访问Thymeleaf中的会话属性

[英]Can't access session attributes in Thymeleaf

I'm using Thymeleaf with Springboot2.1.2 and I have problem accessing session attributes in the templates. 我在Springboot2.1.2中使用Thymeleaf,但在访问模板中的会话属性时遇到问题。
Here's the code: 这是代码:

This is one of the controllers: 这是控制器之一:

@GetMapping("/profile")
public String getProfile(HttpServletRequest request) {
    HttpSession session = request.getSession(false);
    String email = (String) session.getAttribute("userId");
    User user = userService.getProfile(email);
    session.setAttribute("user", user);
    return "user/profile";
}

And the corresponding view(html): 和相应的视图(html):

<body th:object="${session.user}">
    //some code using the user object here...
</body>

When I run the application, I got the exception: 运行应用程序时,出现异常:

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'session' available as request attribute

I've also tried for #session and something else, they didn't work. 我也尝试了#session和其他方法,但是它们没有用。 However, in another controller, I can access the object by using Model: 但是,在另一个控制器中,我可以使用Model来访问对象:

@GetMapping("/register/user")
public String registerUser(Model model) {
    model.addAttribute("user", new User());
    return "user/register";
}

And the view is like: 而且视图就像:

<form th:object="${user}" method="post" action="#" th:action="@{/register/user}">
    //some code using the user object...
</form>

It's driving me crazy since all the tutorials I can find tell me I can access session attributes by ${session.something} , in reality, it doesn't work. 这让我发疯,因为我可以找到的所有教程都告诉我可以通过${session.something}访问会话属性,实际上这是行不通的。
Could you help me? 你可以帮帮我吗?

You should be using Spring-Security with thymeleaf extensions to accomplish what you want. 您应该将Spring-Securitythymeleaf扩展结合使用来完成所需的工作。 Here is an example of what you want to do. 这是您想做的一个例子 If you follow the example you can access user information as follows: 如果遵循该示例,则可以按以下方式访问用户信息:

<div sec:authentication="name"><!-- Display UserName Here --></div>

Please note that for spring-boot 2.1.X you should use the following dependency: 请注意,对于spring-boot 2.1.X,您应该使用以下依赖项:

<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>

You are saving the information in the session, this is not visible by thymeleaf. 您正在会话中保存信息,百里香叶看不到该信息。 you need to create a model for your thymeleaf template and add the attribute (or the session) to the model and then return that. 您需要为您的百里香模板创建一个模型,并将属性(或会话)添加到该模型中,然后将其返回。

@GetMapping("/profile")
public ModelAndView getProfile(HttpServletRequest request) {
    User user = userService.getProfile(email);
    ModelAndView model = new ModelAndView(NAME_OF_THYMELEAF_PROFILE_FILE);
    model.addObject("user",user);
    return model;
}

Beware that for the thymeleaf to see the template it needs to be in a default path (resources/templates) or you need to define where your templates are stored. 请注意,要使百里香叶看到模板,它必须位于默认路径(资源/模板)中,或者您需要定义模板的存储位置。

If you want to use session again the solution is similar. 如果您想再次使用会话,则解决方案类似。

@GetMapping("/profile")
public ModelAndView getProfile(HttpServletRequest request) {
    HttpSession session = request.getSession(false);
    User user = userService.getProfile(email);
    session.setAttribute("user", user);
    ModelAndView model = new 
    ModelAndView(NAME_OF_THYMELEAF_PROFILE_FILE);
    model.addObject("session",session);
    return model;
}

UPDATE use model and return String: UPDATE使用模型并返回String:

@GetMapping("/profile")
public String getProfile(HttpServletRequest request, Model model) {
    HttpSession session = request.getSession(false);
    String email = (String) session.getAttribute("userId");
    User user = userService.getProfile(email);
    session.setAttribute("user", user);
    model.addAttribute("session", session);
    return "user/profile";
}

I used ModelAndView , you can do the same with Model , just instead of addObject() you must use addAttribute() . 我使用了ModelAndView ,您可以对Model进行同样的操作,只是必须使用addAttribute()代替addObject() addAttribute()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM