【问题标题】:Thymeleaf error when validating form inputs before posting to Api在发布到 Api 之前验证表单输入时出现 Thymeleaf 错误
【发布时间】:2017-04-11 09:50:07
【问题描述】:

这是我遇到的错误(注意:错误正在被捕获):

浏览器Error during execution of processor 'org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor' (forms/frmEntry:75)

STS IDEjava.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'e' available as request attribute at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE] .... ....

GET 方法(显示表格):

@RequestMapping(value="/create", method=RequestMethod.GET)
public String create(Model model){

    model.addAttribute("entry", new EntryForm());

    return "forms/frmEntry";
}

由于我原来的POST 方法不起作用,我尝试创建另一个更薄的POST 方法,只是为了排除与postToEntity 调用Api 相关的其他代码。

@PostMapping("/temp")
public String temp(@Valid @ModelAttribute EntryForm e, BindingResult bindingResult){

// returns 1 in debug mode (error for the only field (name) in the form, so OK
//      int ct = bindingResult.getErrorCount();

    if(bindingResult.hasErrors()){

        // this Thymeleaf view/template is reached
        return "forms/frmEntry";
    }

    // dummy Thymeleaf view (doesn't exist)
    return "proslodokraja";
}

我的表格:

<form action="/consumer/temp" th:object="${e}" method="post" class="col-md-6">
    // in my code, this hidden field is commented out (for now)
    <input type="hidden" th:field="*{id}" />
    <p>
        Name: <input type="text" th:field="*{name}" class="form-control" />
        <br/>
        <span th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Name error</span>
        </p>

        <p>
            <input type="submit" value="Submit" class="btn btn-primary" /> 
            <input type="reset" value="Reset" class="btn btn-default" />
        </p>
</form>

表格Bean:

public class EntryForm {

private Long id;

@NotNull
@Length(min=2) // hibernate import!
private String name;


public EntryForm(){ }

public EntryForm(String name) { this.name = 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;
}
}

【问题讨论】:

    标签: java spring validation thymeleaf spring-validator


    【解决方案1】:

    问题在于您的模型属性名称。您将其设置为“条目”,然后尝试在 frmEntry 表单中访问“e”模型属性(甚至没有设置)。设置时将模型属性重命名为“e”,或在frmEntry 页面上将其重命名为“entry”,并在@ModelAttribute 中明确提供名称:

    <form action="/consumer/temp" th:object="${entry}" method="post" class="col-md-6">
    
    
    @ModelAttribute("entry") EntryForm e
    

    【讨论】:

    • 不完全。在发布问题之前,我已将 entry 替换为 e,因为我试图根据 Spring 的一些代码 sn-p 对其进行调整。但为了确保,我只是在模板和发布方法中恢复到entry,并且正如预期的那样,错误仍然存​​在。
    • 我猜你是对的。我也忘了在我的GET 方法中改回entry。最后,我决定将表单bean 引用为entryForm,它终于开始工作了。我接受你的回答。感谢您的帮助!
    猜你喜欢
    • 2015-10-12
    • 2019-07-12
    • 2020-04-24
    • 1970-01-01
    • 2021-06-26
    • 2023-03-23
    • 1970-01-01
    • 2021-05-10
    • 1970-01-01
    相关资源
    最近更新 更多