java - 使用 Spring 3.1 在 JSP 页面中不显示表单验证错误

标签 java spring-mvc validation

我无法在 JSP 页面中显示验证错误消息。这是我的 Controller :

@Controller
@RequestMapping("/secure")
@SuppressWarnings({"All"})
public class OperationController {

    @ModelAttribute("crawler/operation")
    public OperationForm operationForm() {
        return new OperationForm();
    }

    @RequestMapping(value = "crawler/operation/create", method = RequestMethod.POST)
    public String processCrawlerOperationForm(@Valid OperationForm operationForm, BindingResult result, Map model) {
        if (result.hasErrors()) {
            HashMap<String, String> errors = new HashMap<String, String>();
            for (FieldError error : result.getFieldErrors()) {
                errors.put(error.getField(), error.getDefaultMessage());
            }
            model.put("errors", errors);
            return "crawler/operation";
        }
        //Some logic
        return "crawler/operation";
    }

}

我 100% 确定存在错误。我一直在调试这段代码以确保存在错误。

我的表单类:

public class OperationForm {

    @NotEmpty
    private String operationName;

    @NotEmpty
    private String author;

    @NotEmpty
    private String configurationId;

    public String getOperationName() {
        return operationName;
    }
    //Getters and setters
}

我的 JSP Spring 表单:

<form:form action="${pageContext.servletContext.contextPath}/secure/crawler/operation/create.htm" commandName="crawler/operation">
    <table border="0" cellspacing="12">
        <tr>
             <td>
                 <spring:message code="application.operationForm.operationName"/>
             </td>
             <td>
                <form:input path="operationName"/>
             </td>
             <td class="error">
                <form:errors path="operationName"/>
             </td>
        </tr>
        <tr>
             <td>
                 <spring:message code="application.operationForm.author"/>
             </td>
             <td>
                <form:input path="author"/>
             </td>
             <td class="error">
                <form:errors path="author"/>
             </td>
        </tr>
        <tr>
             <td>
                 <spring:message code="application.operationForm.configuration"/>
             </td>
             <td>
                <form:input path="configurationId"/>
             </td>
             <td class="error">
                <form:errors path="configurationId"/>
             </td>
        </tr>
        <tr>
             <td>
                 <input class="button" type="submit" value="Vykdyti"/>
             </td>
        </tr>
    </table>
</form:form>

我做错了什么?

最佳答案

无需循环遍历错误列表并将其插入模型对象,只需检查验证并返回 View ,即

if(result.hasErrors())
  return "crawler/operation";
...

此外,在您的 Controller 方法中,添加 @ModelAttribute 注释,即

public String processCrawlerOperationForm(@ModelAttribute("form") @Valid OperationForm operationForm, BindingResult result, Map model)

确保 ModelAttribute 注释的值与您最初显示表单时放入模型对象中的值相匹配。如果您使用上面的 operationForm 方法来生成模型,请将其设置为该值。我会将其更改为更有用的值,Spring 可能会对/感到困惑,将其更改为简单的内容,例如“form”。

关于java - 使用 Spring 3.1 在 JSP 页面中不显示表单验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10836848/

相关文章:

spring - 在Spring Boot中使用@CrossOrigin

swift - 如何从 Outlet Collection 获取文本字段值以进行密码验证?

java - 从文件中读取数据并将其分解为标记

java - Log4j 2.5 PatternLayout Nano 时间戳不起作用

java - 这是否被视为 "Database as an IPC"反模式?

Java计算给定int数组的所有可能组合

java - Spring Data Rest 字段转换器

javax.el.PropertyNotFoundException : in the JSP page of a Spring MVC Application

Django 管理员 : Unique email verification fails against self

javascript - ASP.NET MVC 验证不适用于 Bootstrap 模式