【问题标题】:Hibernate Validator retrieve error messages without using Spring FrameworkHibernate Validator 在不使用 Spring Framework 的情况下检索错误消息
【发布时间】:2016-11-27 06:41:39
【问题描述】:

我目前正在使用 hibernate-distribution-3.6.4.Final。 Hibernate Validator 的新手。

问题: 我应该如何检索从向 databean/formbean 添加注释时得到的错误消息?我知道在 Spring 中似乎每个人都使用类路径中的 messages.properties 文件?

但是纯hibernate 3怎么样,有没有这样的文件或者我应该怎么做? (网上没找到好的答案……)

【问题讨论】:

    标签: spring hibernate hibernate-validator hibernate-annotations hibernate3


    【解决方案1】:

    希望这会有所帮助。来源是:Mastering Spring MVC

    您需要添加更多内容才能使验证生效。首先,控制器需要说它想要一个有效的表单提交模型。添加javax.validation.Valid 以及

    import org.hibernate.validator.constraints.Email;
        import org.hibernate.validator.constraints.NotEmpty;
    

    对代表表单的参数的注释就是这样做的:

    @RequestMapping(value = "/profile", method = RequestMethod.POST)
    public String saveProfile(@Valid ProfileForm profileForm,
    BindingResult bindingResult) {
    if (bindingResult.hasErrors()) {
    return "profile/profilePage";
    }
    System.out.println("save ok" + profileForm);
    return "redirect:/profile";
    }
    

    请注意,如果表单包含任何错误,您不会重定向用户。这将允许您在同一网页上显示它们。说到这一点,您需要在网页上添加一个显示这些错误的位置。 在profilePage.html 中添加这些行:

    <form th:action="@{/profile}" th:object="${profileForm}"
    ....">
    <div class="row">
    <div class="input-field col s6">
    <input th:field="${profileForm.twitterHandle}"
    id="twitterHandle" type="text" th:errorclass="invalid"/>
    <label for="twitterHandle">Twitter handle</label>
    <div th:errors="*{twitterHandle}" class="redtext">
    Error</div>
    </div>
    <div class="input-field col s6">
    <input th:field="${profileForm.email}" id="email"
    type="text" th:errorclass="invalid"/>
    <label for="email">Email</label>
    <div th:errors="*{email}" class="red-text">Error</div>
    </div>
    </div>
    <div class="row">
    <div class="input-field col s6">
    <input th:field="${profileForm.birthDate}"
    id="birthDate" type="text" th:errorclass="invalid" th:placeholder="${
    dateFormat}"/>
    <label for="birthDate">Birth Date</label>
    <div th:errors="*{birthDate}" class="red-text">Error</
    div>
    </div>
    </div>
    <div class="row s12">
    <button class="btn indigo waves-effect waves-light"
    type="submit" name="save">Submit
    <i class="mdi-content-send right"></i>
    
    </button>
    </div>
    </form>
    

    是的,确实, Spring Boot 负责为我们创建一个消息源 bean。 此消息源的默认位置在

    src/main/resources/messages.
    properties.
    

    创建这样一个包,并添加以下文本:

    Size.profileForm.twitterHandle=Please type in your twitter user name
    Email.profileForm.email=Please specify a valid email address
    NotEmpty.profileForm.email=Please specify your email address
    PastLocalDate.profileForm.birthDate=Please specify a real birth date
    NotNull.profileForm.birthDate=Please specify your birth date
    typeMismatch.birthDate = Invalid birth date format
    

    【讨论】:

    • 非常感谢!!我们如何为纯休眠创建您提到的“捆绑”?这其实是最让我困惑的部分。
    • 对不起,这不是纯 Hibernate。这些注释来自 JSR-303 规范,该规范指定了 bean 验证。该规范最流行的实现是 hibernate-validator,它包含在 Spring Boot 中。对于纯 Hibernate,您必须查找文档
    • 对不起,我误解了你的问题。我还以为你要求 Spring 实现。
    • 您在文件 messages.properties 中创建捆绑包。
    猜你喜欢
    • 2013-08-12
    • 1970-01-01
    • 2014-10-21
    • 1970-01-01
    • 2020-03-24
    • 1970-01-01
    • 2012-04-28
    • 2012-07-10
    • 1970-01-01
    相关资源
    最近更新 更多