Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

183
Views
Spring JSR-303 with form:errors tag

I'm trying to use JSR-303 validation and display it's results in jsp page. Here is jsp page code piece which shows error:

<c:set var="Error">
    <form:errors path="name"/>
</c:set>
...
<spring:transform value="${Error}"/>

In the controller I have the following:

@PostMapping(params = "next")
public String next(@Valid @ModelAttribute(COMMAND_NAME) final ProjectNewDetailsCommand cmd,
                   final Errors errors,
                   final Model model,
                   final HttpServletRequest request) {

First, the solution which worked for me is writing a custom Validator class:

@Override
public void validate(Object o, Errors errors) {
    errors.pushNestedPath("project");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "error.project.creation.name.required");
    errors.popNestedPath();
}

In this case the appropriate message from messages.properties was applied and show on UI:

error.project.creation.name.required=The name must not be blank.

Here is another case when I use JSR-303 annotation:

public class Project {

    ...
    @Size(max = 100, message = "error.project.creation.size")
    private String name;

In messages.properties there is the message under key error.project.creation.size, I've also tried to add the message with key Size.command.project.name as it's stated in some of the sources. Error object in controller is also not empty and contains validation errors. But when I use this approach with JSR-303 I got the following error:

Caused by: javax.servlet.jsp.JspTagException: No message found under code 'Size' for locale 'en_US'.
    at org.springframework.web.servlet.tags.MessageTag.doEndTag(MessageTag.java:200)

Which means that for some reason Spring tries to apply Size error code instead of error.project.creation.size or Size.command.project.name. Any ideas why this is happening and how to fix this?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Finally I was able to find the source of the problem. In the code for rendering alerts, there was a spring:message tag:

<spring:message code="${error.code}" arguments="${error.arguments}"/>

If works in the following way: it looks for last code from the codes array in Errors class (see DefaultMessageSourceResolvable.getCode method) and that's why it couldn't find Size message and didn't picked up defaultMessage created by hibernate validator or the custom one.

The simple fix in this case was to add text attribute:

<spring:message code="${error.code}" arguments="${error.arguments}" text="${error.defaultMessage}"/>
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!