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
  • Parameter conversion
  • Format
  • Source Codes

Was this helpful?

  1. MVC 1.0

Bean parameter conversion

Spring MVC provides ConversionService to convert data to bean target type from from data.

In MVC, we can use custom ParamConverter to convert form data to the target type.

Parameter conversion

As an example, add a dueDate to Task entity. We need to convert the form field value(it is a String) to LocalDate type.

  1. Create a custom ParamConverterProvider for LocalDate.

     @Provider
     public class CustomConverterProvider implements ParamConverterProvider {
    
         final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ISO_DATE;
    
         @Override
         public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) {
    
             if (rawType.getName().equals(LocalDate.class.getName())) {
    
                 return new ParamConverter<T>() {
                     @Override
                     public T fromString(String value) {
                         return value != null ? (T) LocalDate.parse(value, DATE_FORMAT) : null;
                     }
    
                     @Override
                     public String toString(T value) {
                         return value != null ? ((LocalDate) value).format(DATE_FORMAT) : "";
                     }
                 };
             } 
             ....
     }
  2. Add a new field dueDate to TaskForm, which type is LocalDate.

     @NotNull
     @FormParam("duedate")
     private LocalDate dueDate;
  3. In the view, add a new form field named duedate.

     <input type="text" id="duedate" name="duedate" class="form-control" placeholder="Due date"/>

When the form is submitted, the duedate will be converted to LocalDate type and bind to dueDate property of TaskForm.

Format

Spring provides Formatting service in the display view. eg. @DateFormat and custom Formatters.

Unfortunately, MVC does not support such features. And JSTL fmt:formatDate does not accept Java 8 DateTime APIs.

We can define a custom CDI beans for date formatting, and call it via expression language.

  1. Create RequestScoped bean Formatters.

     @RequestScoped
     @Named("formatters")
     public class Formatters {
    
         static DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ISO_DATE;
    
         public String formatDate(LocalDate data) {
             return DATE_FORMATTER.format(data);
         }
    
     }
  2. Apply it on the dueDate in the details.jspx page.

     <dt>Due date:</dt>
     <dd>${formatters.formatDate(details.dueDate)}</dd>

The result looks like:

Source Codes

  1. Clone the codes from my GitHub account.

  2. Open the mvc project in NetBeans IDE.

  3. Run it on Glassfish.

PreviousSecurityNextView engine

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
date formatting result