2015-01-10 194 views
0
全球错误结果检查

要显示与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"; 
} 

回答

3

解决:

th:object需要在标签之前全球错误检查。不幸的是,这在春季百里香叶tutorial中没有提及。据推测,有一个默认的表单名称在我的控制器中被覆盖。

添加在此工作的HTML结果的标签:

<form action="search.html" th:action="@{/auto/search}"> 
<div th:object="${command}" th:remove="tag"> 
    <p th:if="${#fields.hasErrors('global')}" th:errors="*{global}"> 
     Incorrect date 
    </p> 
</div> 
<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> 

..其中“命令”是控制器的形式bean的名字。

This thread帮我弄明白了。

+0

通过使用语法'th:errors =“$ {command.global}”',您应该可以在表单外部或者在没有'th:object =“$ {command}”''的情况下访问它。见[http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#displaying-errors-outside-forms] – phn