Spring MVC Formatter

Spring MVC Formatter

Spring MVC supports data binding and type conversion to help you bind the data sent from a form to a Java object and to convert the data from one type to another. However, when you have more complex use cases where you need to convert between a string and a complex object, then Spring MVC formatters come into play.

Formatters in Spring MVC are similar to PropertyEditors but they are thread-safe and can be used by new Spring type conversion system.

Let's create a formatter that converts a String to a LocalDate object and vice versa.

  1. Define the Formatter:

    Create a new class that implements the org.springframework.format.Formatter interface.

    import org.springframework.format.Formatter; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.Locale; public class LocalDateFormatter implements Formatter<LocalDate> { private DateTimeFormatter formatter; private String datePattern; public LocalDateFormatter(String datePattern) { this.datePattern = datePattern; this.formatter = DateTimeFormatter.ofPattern(datePattern); } @Override public LocalDate parse(String text, Locale locale) throws ParseException { return LocalDate.parse(text, formatter); } @Override public String print(LocalDate object, Locale locale) { return formatter.format(object); } } 

    In this class, the parse method converts a string to a LocalDate, and the print method converts a LocalDate to a string.

  2. Register the Formatter:

    Next, you need to register the formatter with Spring MVC. This can be done in the configuration class (Java-based configuration) where you extend WebMvcConfigurerAdapter and override the addFormatters method.

    import org.springframework.context.annotation.Configuration; import org.springframework.format.FormatterRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addFormatters(FormatterRegistry registry) { // Register LocalDateFormatter as a formatter for LocalDate objects registry.addFormatter(new LocalDateFormatter("yyyy-MM-dd")); } } 

    Now, whenever Spring sees a LocalDate object in a controller method argument or in a Thymeleaf view, it will use the LocalDateFormatter to convert the LocalDate to a string and vice versa.

  3. Use the Formatter in a Controller:

    Now you can use LocalDate objects in your controller methods and form objects. Spring will automatically use the LocalDateFormatter to convert between strings and LocalDate objects.

    import org.springframework.web.bind.annotation.PostMapping; @PostMapping("/submitForm") public String submitForm(@ModelAttribute("form") MyForm form) { LocalDate date = form.getDate(); // date is converted from String to LocalDate // ... } 

    That's it! Now, whenever you need to convert between a String and a LocalDate, Spring will use the LocalDateFormatter to perform the conversion.

This tutorial is a basic demonstration of how to create and use a custom Formatter in Spring MVC. More complex examples might involve converting between a String and a complex object.


More Tags

tradingview-api cat pushsharp postal-code grid-layout recorder.js tint date-parsing heap-memory websocket

More Programming Guides

Other Guides

More Programming Examples