0
這裏是我的控制器,Thymeleaf +春季啓動:webcontext對象「會話」工作不正常
@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";
}
}
我存儲對象到會話,並會顯示它與下面的代碼的用戶界面,
<!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]
有任何意見表示感謝,提前致謝!
使用'日:文本= 「$ {} session.filter.name」' –
這就是'日:field'一樣。它指的是指定對象的字段(通常是命令對象)。你可能想用'th:text'代替。 –
謝謝Deinum。我想將「過濾器」作爲查詢示例的命令對象,並且我希望搜索條件位於Session中,以便搜索條件在不同的請求之間持續。 th:文字無法保持用戶輸入。我對嗎?再次感謝。 –