【问题标题】:th:if="${#fields.hasErrors()}" causes Exception evaluating SpringEL expressionth:if="${#fields.hasErrors()}" 导致异常评估 SpringEL 表达式
【发布时间】:2018-02-20 12:28:14
【问题描述】:

正如你们所见,我正在尝试对表单的 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 标签,它会正常运行。

【问题讨论】:

    标签: spring hibernate thymeleaf


    【解决方案1】:

    问题在于您进行了重定向(您进行了“重定向:全部”)。 由于重定向,您不会传递对象 person1,因此会出现错误“java.lang.IllegalStateException:Bean name 'person1' 的 BindingResult 和普通目标对象都不能用作请求属性”。

    如果您想发布您拥有的 /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";
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-20
      • 2023-01-03
      • 2019-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多