Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Java EE MVC: Handling Form Validation

DZone's Guide to

Java EE MVC: Handling Form Validation

Here's a lesson in form validation in Java EE MVC environments that uses bean validation annotations and other handy tools to make it easy.

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

In this post, we will have a look at form validation in Java EE MVC.

Java EE MVC integrates with the Java Bean Validation API (JSR 303), which makes adding validation constraints pretty easy.

Validation, the JAX-RS Way

Assume we have a small HTML form that can be used to send contact messages. To represent the form data, we create a small ContactMessage bean containing the form fields and validation constraints:

public class ContactMessage {
    @FormParam("message")
    @NotBlank
    @Size(max = 1000)
    private String message;

    // getters / setters
}


In our MVC controller, we use the @BeanParam annotation to convert the form data to a ContactMessage object:

@Controller
@Path("/")
public class ContactController {

    @POST
    public String formSubmit(@Valid @BeanParam ContactMessage message) {
        ...
    }
}


(For more details about the @BeanParam annotation, have a look at the blog post Working with bean parameters).

By adding the @Valid annotation to the ContactMessage parameter, we enable validation. If a form is submitted and the validation of the ContactMessage object fails, a ConstraintViolationException will be thrown. The controller method will not be called in this case. Instead, the exception can be handled using a generic JAX-RS ExceptionMapper, as shown in another post: Global Exception handling in Java EE MVC.

This approach is typically fine for standard JAX-RS REST endpoints. Here, we often want to return a generic HTTP 400 (Bad request) status code if invalid data has been passed to the server.

In an MVC environment, we can use this behavior to render a standard error page to the user whenever invalid data has been passed. However, this is often not flexible enough. Often, we want to return a more specific page that shows an error message to the user.

Validation Using @MvcBinding and BindingResult

Java EE MVC provides an @MvcBinding annotation that enables an alternative exception-handling mechanism. @MvcBinding can be placed on fields and method parameters together with JAX-RS binding annotations (like @FormParam):

public class ContactMessage {
    @MvcBinding
    @FormParam("message")
    @NotBlank
    @Size(max = 1000)
    private String message;

    // getters / setters
}


This tells Java EE MVC to call the controller method instead of the generic exception handler if binding of the annotated field fails. To access binding information, we can inject a BindingResult object into our controller class:

@Controller
@Path("/")
public class ContactController {
    @Inject
    private Models models;

    @Inject
    private BindingResult bindingResult;

    @POST
    public String formSubmit(@Valid @BeanParam ContactMessage message) {
        if (bindingResult.isFailed()) {
            models.put("bindingResult", bindingResult);
            return "/WEB-INF/jsp/contact.jsp";
        }
        return "/WEB-INF/jsp/success.jsp";
    }
}


As the name suggests, we can use the injected BindingResult object to access binding information within a controller method. In this example, we simply check if there was a binding problem by calling isFailed(). By adding the bindingResult to the model, we can access it later in the view to show an error message to the user.

A simple JSP view that displays all validation errors below the submit button looks like this:

<form action="${mvc.contextPath}/contact" method="post">
    <label>Message:</label>
    <textarea name="message"></textarea>
    <br/>

    <input type="submit"/>

    <c:if test="${bindingResult.isFailed()}">
        <p>Form validation failed. Reasons:</p>
        <ul>
            <c:forEach items="${bindingResult.allValidationErrors}" var="validationError">
                <li>
                    <c:out value="${validationError.paramName}: ${validationError.message}"/>
                </li>
            </c:forEach>
        </ul>
    </c:if>
</form>


Conclusion

Form validation is pretty easy with Java EE MVC. Validation constraints can be added to beans using JSR 303 Bean validation annotations. @MvcBinding allows us to handle validation errors within controller methods instead of using generic ExceptionMappers. BindingResult gives us access to validation information.

As always, you can find the example code on GitHub.

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. 

Topics:
java ,java ee mvc ,form validation ,java beans ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}