0

I am currently using hibernate-distribution-3.6.4.Final. New to Hibernate Validator.

Problem: How should I retrieve error messages I got from adding annotations to databean/formbean? I know in Spring that everyone seems to use messages.properties file from the classpath?

But how about pure hibernate 3, is there any file like that or what should i do instead? (Didn't find good answers online...)

Chenya Zhang
  • 463
  • 3
  • 11
  • 22

1 Answers1

1

Hope this will help. Source is : Mastering Spring MVC

You will need to add a few more things for validation to work. First, the controller needs to say that it wants a valid model on form submission. Adding the javax.validation.Valid as wel as

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

annotation to the parameter representing the form does just that:

@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";
}

Note that you do not redirect the user if the form contains any errors. This will allow you to display them on the same web page. Speaking of which, you need to add a place on the web page where those errors will be displayed. Add these lines in 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>

Yes Indeed, Spring Boot takes care of creating a message source bean for us. The default location for this message source is in

src/main/resources/messages.
properties.

Create such a bundle, and add the following text:

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

. enter image description here

AchillesVan
  • 4,156
  • 3
  • 35
  • 47
  • Thanks a lot!! How can we create the "bundle" you mentioned for pure hibernate? This is actually the part that confused me the most. – Chenya Zhang Jul 23 '16 at 14:17
  • Sorry, this is not pure Hibernate. These annotations come from the JSR-303 specification, which specifies bean validation. The most popular implementation of this specification is hibernate-validator, which is included in Spring Boot. For pure Hibernate, you must lookup for documentations – AchillesVan Jul 23 '16 at 14:22
  • Sorry, i v misunderstood your question. I thought that you asked for spring implementation. – AchillesVan Jul 23 '16 at 14:23
  • you create the bundle in the file messages.properties . – AchillesVan Jul 23 '16 at 14:37