【发布时间】: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