Java EE 8 By Example
  • Introduction
  • Overview
    • Example Codes
  • JSF 2.3
    • Activating CDI in JSF 2.3
    • Run applications in JSF 2.2 compatible mode
    • CDI alignment
    • CDI compatible @ManagedProperty
    • Inject support in Converter, Validator and Behavor
    • Websocket support
    • UIData improvements
    • Class level bean validation with f:valdiateWholeBean
    • Java 8 DateTime support
    • PostRenderViewEvent: publising events after view is rendered
    • Search expression framework
  • CDI 2.0
    • Java SE support
    • Event Priority
    • Async Events
    • Register Beans dynamicially
    • Configurators and Intercept Producers
  • JPA 2.2
    • Java 8 Datetime support
    • Return Stream based result from Query
    • More CDI Alignments
  • JSON-B 1.0
  • JSON-P 1.1
  • Bean Validation 2.0
  • JAXRS 2.1
    • Async improvements
    • Server Sent Event
    • Reactive Client
  • Java EE Security API 1.0
    • HttpAuthenticationMechanism
    • IdentityStore
    • SecurityContext
  • Servlet 4.0
    • Server Push
    • Runtime Discovery of Servlet Mappings
    • Http Trailer
  • MVC 1.0
    • Getting started with MVC
    • Handling form submission
    • Exception handling and form validation
    • Processing PUT and DELETE methods
    • Page navigation
    • MVC and CDI
    • Security
    • Bean parameter conversion
    • View engine
Powered by GitBook
On this page
  • Form binding and validation
  • Handling form validation
  • Handling exception
  • Source codes

Was this helpful?

  1. MVC 1.0

Exception handling and form validation

When submitting a form, it should validate the form data before it is stored in the backend database.

Form binding and validation

Like Spring MVC, Struts, Stripes, JSF etc. MVC provides the similar progress to process form submission.

  1. Gather user input form data.

  2. Convert form data to the target form bean. If there are some conversion failure, it is possible to stop the progress and notify user.

  3. Bind the converted value to the form bean.

  4. Validate the form bean via Bean Validation. If there are some constraint violations, it is possible to stop the progress and notify user.

  5. Continue to process form.

MVC provides a BindingResult to gather all of the binding errors and constraint violations in the request.

Handling form validation

Inject it in the controller class.

@Inject
private BindingResult validationResult;

In the controller method, add @Valid annotation on the method parameters.

@ValidateOnExecution(type = ExecutableType.NONE)
public Response save(@Valid @BeanParam TaskForm form) {
    log.log(Level.INFO, "saving new task @{0}", form);

    if (validationResult.isFailed()) {
        AlertMessage alert = AlertMessage.danger("Validation voilations!");
        validationResult.getAllViolations()
                .stream()
                .forEach((ConstraintViolation t) -> {
                    alert.addError(t.getPropertyPath().toString(), "", t.getMessage());
                });
        models.put("errors", alert);
        return Response.status(BAD_REQUEST).entity("add.jspx").build();
    }
}

If the validation is failed, the isFailed method should return true.

You can iterate all violations(validationResult.getAllViolations()) and gather the violation details for each properties.

Then display the error messages in the JSP pages.

<c:if test="${not empty errors}">
     <c:forEach items="${errors.errors}" var="error">
    <div class="alert alert-danger alert-dismissible"
         role="alert">
        <button type="button" class="close" data-dismiss="alert"
                aria-label="Close">
            <span aria-hidden="true"><![CDATA[&times;]]></span>
        </button>
        <p>${error.field}: ${error.message}</p>
    </div>
    </c:forEach>
</c:if>

Handling exception

Like JAX-RS exception handling, you can handle exception via ExceptionMapper and display errors in the certain view.

Create a custom ExceptionMapper and add annotation @Provider.

@Provider
public class TaskNotFoundExceptionMapper implements ExceptionMapper<TaskNotFoundException>{

    @Inject Logger log;

    @Inject Models models;

    @Override
    public Response toResponse(TaskNotFoundException exception) {
        log.log(Level.INFO, "handling exception : TaskNotFoundException");
        models.put("error", exception.getMessage());
        return Response.status(Response.Status.NOT_FOUND).entity("error.jspx").build();
    }     
}

Different from JAX-RS, the entity value is the view that will be returned. In the error.jspx file, display the error model via EL directly.

<div class="container">
    <div class="page-header">
        <h1>Psst...something was wrong!</h1>
    </div>
    <div class="row">
        <div class="col-md-12">
            <p class="text-danger">${error}</p>
        </div>
    </div>
</div>

When the TaskNotFoundException is caught, it will display the erorr like the following.

Source codes

  1. Clone the codes from my GitHub account.

  2. Open the mvc project in NetBeans IDE.

  3. Run it on Glassfish.

PreviousHandling form submissionNextProcessing PUT and DELETE methods

Last updated 4 years ago

Was this helpful?

After it is deployed and running on Glassfish application server, navigate in browser.

https://github.com/hantsy/ee8-sandbox/
http://localhost:8080/ee8-mvc/mvc/tasks
mvc error