这是我的控制器,
@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将'session'视为请求中的正常对象,预定义的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]
感谢所有评论,提前感谢!
答案 0 :(得分:0)
正如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";
}
}