要显示与Spring MVC与百里香叶创建全局错误,我试图在http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#global-errors给出的例子:春百里香叶在NPE
也就是说,是:
<div th:if="${#fields.hasGlobalErrors()}">
和
<ul th:if="${#fields.hasErrors('global')}">
和
<div th:if="${#fields.hasGlobalErrors()}">
当我将它们添加到我的HTML,网页甚至不会渲染,没关系关于提交表单。所有的例子都导致:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "#fields.hasErrors('global')"
我试图与v2.1.4和v.2.1.3,并得到了同样的错误。错误还是我做错了什么?
是的,这些标签都是封闭的和正确的形成。是的,这段代码是在一个表单中。是的,表单的所有其他方面都没有全局错误检查。
这里被打破HTML的一个短版:
<form action="search.html" th:action="@{/auto/search}">
<p th:if="${#fields.hasErrors('global')}" th:errors="*{global}">
Incorrect date
</p>
<input type="text" th:field="${command.stockNumber}" />
<select th:field="*{command.startYear}">
<option value="" th:each="year : ${modelYears}" th:value="${year}"
th:text="${year}"></option>
</select>
</form>
而且控制器..
@RequestMapping(value = "/auto/search", method = RequestMethod.POST)
public String search(@Validated
@ModelAttribute("command")
AutoSearchCommand autoSearchCommand
BindingResult result, Model model) {
return "search";
}
通过使用语法'th:errors =“$ {command.global}”',您应该可以在表单外部或者在没有'th:object =“$ {command}”''的情况下访问它。见[http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#displaying-errors-outside-forms] – phn