【问题标题】:Thymeleaf + Spring Boot: webcontext object 'session' is not working properlyThymeleaf + Spring Boot:webcontext 对象“会话”无法正常工作
【发布时间】:2017-03-09 14:08:50
【问题描述】:

这是我的控制器,

@Controller
public class CategoryController {
    @GetMapping(value="/categories")
    public String searchCategory(Map<String, Object> model, HttpSession session) {
        Category filter = (Category) session.getAttribute("filter");
        if(filter == null) {
            filter = new Category();
            session.setAttribute("filter", filter);
        }

        ...

        return "category-list";
    }   
}

我将一个对象存储到会话中,并使用以下代码将其显示在 UI 上,

<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<body>
            ...
            <input type="text" class="form-control" id="name" th:field="${session.filter.name}" placeholder="name"/>
            ...
</body>

但我最终得到了以下错误消息,在我看来,Thymeleaf 将“会话”视为请求中的普通对象,而不是预定义的 Webcontext 对象

2016-10-27 10:34:53.655 ERROR 5844 --- [nio-8080-exec-7] o.a.c.c.C.[.[.[.[dispatcherServlet]      : Servlet.service() fo
r servlet [dispatcherServlet] in context with path [/smartact] threw exception [Request processing failed; nested except
ion is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path
 resource [templates/datamaster/category-list.html]")] with root cause

java.lang.IllegalStateException: **Neither BindingResult nor plain target object for bean name 'session' available as requ
est attribute**
        at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144) ~[spring-webmvc-5.0.0.BUILD-SN
APSHOT.jar:5.0.0.BUILD-SNAPSHOT]
        at org.thymeleaf.spring4.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:307) ~[thymeleaf-spri
ng4-3.0.0.RELEASE.jar:3.0.0.RELEASE]
        at org.thymeleaf.spring4.util.FieldUtils.getBindStatus(FieldUtils.java:258) ~[thymeleaf-spring4-3.0.0.RELEASE.ja
r:3.0.0.RELEASE]
        at org.thymeleaf.spring4.util.FieldUtils.getBindStatus(FieldUtils.java:227) ~[thymeleaf-spring4-3.0.0.RELEASE.ja
r:3.0.0.RELEASE]
        at org.thymeleaf.spring4.processor.AbstractSpringFieldTagProcessor.doProcess(AbstractSpringFieldTagProcessor.jav
a:173) ~[thymeleaf-spring4-3.0.0.RELEASE.jar:3.0.0.RELEASE]
        at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74
) ~[thymeleaf-3.0.0.RELEASE.jar:3.0.0.RELEASE]

感谢任何 cmets,提前致谢!

【问题讨论】:

  • 使用th:text="${session.filter.name}"
  • th:field 就是这样做的。它指的是指定对象(通常是命令对象)的字段。您可能想改用th:text
  • 感谢 Deinum。我想将 'filter' 作为 Query-by-Example 的命令对象,并且我希望搜索条件位于 Session 中,以便搜索条件在不同请求之间持续存在。 th:text 无法保留用户输入。我对吗?再次感谢。

标签: spring-mvc spring-boot thymeleaf


【解决方案1】:

正如 Prasanna Kumar 所说,使用 th:text="${filter.name}"(注意 filter 是属性名称)。然而,还有一件更重要的事情:不要让你的会话变得不必要!在最坏的情况下,将其设置为 Spring 的会话属性。通常,您将使用请求属性:

@Controller
public class CategoryController {
    @GetMapping(value="/categories")
    public String searchCategory(@ModelAttribute("filter") Category filter, Model model) {
        filter = new Category();
        model.addAttribute("filter", filter);

        ...

        return "category-list";
    }   
}

【讨论】:

  • 非常感谢 Branislav,请参考我上面的回复。
猜你喜欢
  • 2017-08-13
  • 2022-01-22
  • 2015-05-10
  • 2018-11-22
  • 1970-01-01
  • 1970-01-01
  • 2018-02-24
相关资源
最近更新 更多