【问题标题】:Given error message is not displayed in Spring Form validation给定的错误消息未在 Spring Form 验证中显示
【发布时间】:2015-12-24 06:27:21
【问题描述】:

我想验证我的表单字段。我关注了this tutorial。我只想显示我给定的错误消息(无需使用 MessageResource,但在 pojo 中硬编码消息)。我使用 Hibernate 和 spring 验证,如上面使用的教程。这是我的 pojo,它被用作带有验证注释的模型属性。

import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;

public class User {

    @NotNull(message="username cannot be empty")
    private String username;
    @NotEmpty @Email(message="e mail cannot be empty")
    private String email;
    (... encapsulated getters and setter..)

这是我的 JSP。

<form:form action="register" method="post"  commandName="userForm">
            <table border="0">
                <tr>
                    <td colspan="2" align="center"><h2>Spring MVC Form Demo - Registration</h2></td>
                </tr>
                <tr>
                    <td>User Name:</td>
                    <td><form:input path="username" />
                     <form:errors path="username"></form:errors>
                    </td>

                </tr>
                <tr>
                    <td>E-mail:</td>
                    <td><form:input path="email" />
                     <form:errors path="email"></form:errors>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" align="center"><input type="submit" value="Register" /></td>
                </tr>
            </table>
        </form:form>

这是我的控制器:

    @RequestMapping(method = RequestMethod.POST)
    public String processRegistration( @ModelAttribute("userForm") @Valid   User user
                                        ,BindingResult result
                                        ,Map<String, Object> model) {

         if(result.hasErrors()){
                     return "registration";
         }
return "profile";

在我的 pom.xml 中,

<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>1.1.0.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>5.0.1.Final</version>
</dependency>

当出现字段错误时,页面仅显示默认错误may not be empty。如何设置给定的错误消息而不是默认错误?任何人都可以在我的实现中看到任何错误?

【问题讨论】:

    标签: java spring hibernate validation spring-mvc


    【解决方案1】:

    根据您当前的代码,只有当用户名为空时,您才会得到username cannot be empty。将其更改为@NotEmpty。只有当电子邮件格式不正确时,您才会收到e mail cannot be empty。如果电子邮件为空,则将打印默认消息,因为您尚未将任何消息附加到电子邮件属性的@NotEmpty。 @Sanjay 的建议会解决这个问题。

    【讨论】:

      【解决方案2】:

      你可以试试这个,看看它是否有效:

      @NotEmpty(message="username cannot be empty")
      private String username;
      
      @NotEmpty(message="email cannot be empty")
      @Email(message="email not well formed")
      private String email;
      

      【讨论】:

        猜你喜欢
        • 2022-01-09
        • 2021-10-20
        • 2016-07-23
        • 2020-05-22
        • 2015-09-07
        • 2020-06-20
        • 1970-01-01
        • 2022-06-20
        • 1970-01-01
        相关资源
        最近更新 更多