Spring Thyme Leaf 检查全局错误导致 NPE [英] Spring Thyme Leaf checking for global errors results in NPE

查看:36
本文介绍了Spring Thyme Leaf 检查全局错误导致 NPE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了显示使用 Thyme Leaf 的 Spring MVC 创建的全局错误,我尝试了 http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#global-errors:

也就是说:

    当我将它们添加到我的 HTML 时,页面甚至不会呈现,更不用说提交表单了.所有示例都导致:

    org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是 org.thymeleaf.exceptions.TemplateProcessingException:评估 SpringEL 表达式的异常:#fields.hasErrors('global')"

    我用 v2.1.4 和 v.2.1.3 试过这个,得到了同样的错误.错误还是我做错了什么?

    是的,所有标签都已关闭且格式正确.是的,此代码在表单中.是的,表单的所有其他方面都可以在没有全局错误检查的情况下工作.

    以下是损坏的 HTML 的简短版本:

    <p th:if="${#fields.hasErrors('global')}" th:errors="*{global}">日期不正确</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></选择></表单>

    还有控制器..

    @RequestMapping(value = "/auto/search", method = RequestMethod.POST)公共字符串搜索(@Validated@ModelAttribute("命令")自动搜索命令 autoSearchCommandBindingResult 结果,模型模型) {返回搜索";}

    解决方案

    已解决:

    在全局错误检查之前的标记中需要

    th:object.不幸的是,Spring Thyme Leaf 教程<中没有提到这一点/a>.据推测,我在控制器中覆盖了某个默认表单名称.

    在这个工作 html 结果中添加标签:

    <div th:object="${command}" th:remove="tag"><p th:if="${#fields.hasErrors('global')}" th:errors="*{global}">日期不正确</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></选择></表单>

    .. 其中command"是控制器中表单 bean 的名称.

    这个帖子帮我弄明白了.

    To display global errors created with Spring MVC with Thyme Leaf, I tried the example given at http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#global-errors:

    That, is:

    <div th:if="${#fields.hasGlobalErrors()}">

    and

    <ul th:if="${#fields.hasErrors('global')}">

    and

    <div th:if="${#fields.hasGlobalErrors()}">

    When I add them to my HTML, the page won't even render, nevermind about submitting the form. All the examples result in:

    org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "#fields.hasErrors('global')"

    I tried this with v2.1.4 and v.2.1.3 and got the same error. Bug or am I doing something wrong?

    Yes, the tags are all closed and properly formed. Yes, this code was within a form. Yes, all the other aspects of the form work without the global error check.

    Here is a short version of broken 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>
    

    And the controller..

    @RequestMapping(value = "/auto/search", method = RequestMethod.POST)
    public String search(@Validated
                         @ModelAttribute("command")
                         AutoSearchCommand autoSearchCommand
                         BindingResult result, Model model) {
        return "search";
    }
    

    解决方案

    Solved:

    th:object is needed in a tag preceding to the global error check. Unfortunately, this isn't mentioned in the Spring Thyme Leaf tutorial. Presumably, there's a default form name somewhere which I've overridden in my controller.

    Adding the tag in results in this working 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>
    

    .. Where "command" is the name of the form bean in the controller.

    This thread helped me figure it out.

    这篇关于Spring Thyme Leaf 检查全局错误导致 NPE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆