【问题标题】:Can't access session attributes in Thymeleaf无法访问 Thymeleaf 中的会话属性
【发布时间】:2019-02-11 09:07:49
【问题描述】:

我在 Springboot2.1.2 中使用 Thymeleaf,但在访问模板中的会话属性时遇到问题。
代码如下:

这是控制器之一:

@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";
}

以及对应的view(html):

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

当我运行应用程序时,我得到了异常:

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

我也尝试了#session 和其他东西,但它们没有用。但是,在另一个控制器中,我可以使用 Model 访问该对象:

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

视图是这样的:

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

这让我发疯,因为我能找到的所有教程都告诉我,我可以通过 ${session.something} 访问会话属性,实际上,它不起作用。
你能帮帮我吗?

【问题讨论】:

    标签: java spring-boot thymeleaf


    【解决方案1】:

    您正在保存会话中的信息,这对 thymeleaf 不可见。您需要为您的 thymeleaf 模板创建一个模型并将属性(或会话)添加到模型中,然后返回它。

    @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;
    }
    

    请注意,要让 thymeleaf 查看模板,它需要位于默认路径(资源/模板)中,或者您需要定义模板的存储位置。

    如果你想再次使用会话,解决方法类似。

    @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;
    }
    

    更新使用模型并返回字符串:

    @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";
    }
    

    我用了ModelAndView,你可以用Model做同样的事情,只是你必须用addAttribute()代替addObject()

    【讨论】:

    • 是的,ModelAndView 确实有效,但我想知道为什么会话对 Thymeleaf 不可见?这是我在官方论坛中找到的另一个问题的回复:
    • 我有什么遗漏的秘密吗?
    • 我感觉返回 ModelAndView 不如返回字符串优雅
    • 实际上,我发现返回模型比返回字符串更优雅。但是在第二种情况下,您将用户插入模型中,这就是它起作用的原因。您需要将您需要的信息导入模型中,会话不直接与thymeleaf 连接。看看这个tutorial
    【解决方案2】:

    你应该使用 Spring-Securitythymeleaf 扩展 来完成你想要的。这是您想要做的事情的example。如果您遵循示例,您可以访问用户信息,如下所示:

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

    请注意,对于 spring-boot 2.1.X,您应该使用以下依赖项:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-05
      • 1970-01-01
      • 2014-07-31
      • 1970-01-01
      • 1970-01-01
      • 2019-05-18
      • 2013-07-03
      • 1970-01-01
      相关资源
      最近更新 更多