th:if =“$ {#fields.hasErrors()}”导致Exception评估SpringEL表达式

时间:2017-09-12 06:45:19

标签: spring hibernate thymeleaf

我正在尝试对表单的1个字段进行验证,你们可以看到:

<div id ="EditModal" class="modal fade" role="dialog">
    <form class="modal-dialog" th:action="@{/Edit}" th:object="${person1}" method="POST">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>
            <div class="modal-body">
                <input type="hidden" id="edit-modal-person-id" name="id" value=""/>
                <input type="text" placeholder="Name" id="edit-modal-person-name" name="name"/>
                <p th:if="${#fields.hasErrors('name')}" th:errors="*{name}" th:class="'error'">something</p>
                <div>
                    <a class="btn btn-default pull-right" id="PhoneNumberEdit">Edit Phone Number</a>
                </div>
            </div>
            <div class="modal-footer">
                <button type="submit" id="SubmitEdit" class="btn btn-default" >Submit</button>
                <button type="button" class = "btn btn-default" data-dismiss="modal">Cancel</button>
            </div>
        </div>
    </form>
</div>

属性th:if="${#fields.hasErrors('name')}"总是会导致500错误。

我的控制员:

@RequestMapping(value = "/Edit", method = RequestMethod.POST)
public String editPerson(@Valid @ModelAttribute(value="person1") PersonRequest person, BindingResult result) {
    if(result.hasErrors()) {
        return "redirect:All";
    }
    else {
        personService.update(person.getId(), person.getName());
        return "redirect:All";
    }
}

我的实体:

public class PersonRequest {

    @NotNull
    private long id;

    @NotEmpty
    @Name
    private String name;

    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public PersonRequest() {
        super();
    }
}

控制台返回以下错误:

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'person1' available as request attribute.

但我认为这与此无关,因为如果删除p标签,它会正常运行。

1 个答案:

答案 0 :(得分:3)

问题是你做了一个重定向,(你做了一个&#34;重定向:All&#34;)。 由于重定向,你不会传递对象person1因此错误&#34; java.lang.IllegalStateException:既不是BindingResult也不是bean名称的普通目标对象&#39; person1&#39;可用作请求属性&#34;。

如果你想发布你所拥有的/ all requestMapping的代码

可能你想要这样的东西

@RequestMapping(value = "/Edit", method = RequestMethod.POST)
    public String editPerson(@Valid @ModelAttribute(value="person1") PersonRequest person, BindingResult result) {
        if(result.hasErrors()) {

            return "edit";//change it to the name of the html page that you want to go
        }
        else {
            //probably here you return in the page with all the persons 
            personService.update(person.getId(), person.getName());
            return "redirect:All";
        }
    }