PropertyEditor is originally part of the JavaBeans specification. Spring also heavily uses the PropertyEditors to be able to represent properties in a different way than the object itself e.g. parsing human readable inputs from http request params or displaying human readable values of pure java objects in view layer.
Spring has a number of built-in PropertyEditors in the org.springframework.beans.propertyeditors
package e.g. for Boolean
, Currency
, and URL
. Some of these editors are registered by default, while some you need to when required.
You can also create custom PropertyEditor in case – default property editors do not serve the purpose. Let’s say we are creating an application for books management. Now people can search the books by ISBN as well. Also, you will need to display isbn details in webpage.
Create Custom PropertyEditor
To create custom property editor, you will need to extend java.beans.PropertyEditorSupport
class.
IsbnEditor.java
package com.howtodoinjava.app.editors; import java.beans.PropertyEditorSupport; import org.springframework.util.StringUtils; import com.howtodoinjava.app.model.Isbn; public class IsbnEditor extends PropertyEditorSupport { @Override public void setAsText(String text) throws IllegalArgumentException { if (StringUtils.hasText(text)) { setValue(new Isbn(text.trim())); } else { setValue(null); } } @Override public String getAsText() { Isbn isbn = (Isbn) getValue(); if (isbn != null) { return isbn.getIsbn(); } else { return ""; } } }
Where Isbn class is as below:
Isbn.java
package com.howtodoinjava.app.model; public class Isbn { private String isbn; public Isbn(String isbn) { this.isbn = isbn; } public String getIsbn() { return isbn; } public String getDisplayValue() { return isbn; } }
Register Custom PropertyEditor
Next step is to register custom property editor in spring application. To register, you will need to create a method with annotation – @InitBinder
. On application startup, this annotation is scanned and all the detected methods should have a signature of accepting WebDataBinder
as an argument.
HomeController.java
@Controller public class HomeController { //... @InitBinder public void initBinder(WebDataBinder binder) { binder.registerCustomEditor(Isbn.class, new IsbnEditor()); } }
Accept input and display value using custom property editor
Now when custom property editor is created and registered, let’s use it. You can use it in controller to accept input as follow:
HomeController.java
@Controller public class HomeController { private final Logger LOGGER = LoggerFactory.getLogger(this.getClass()); @RequestMapping(value = "/books/{isbn}", method = RequestMethod.GET) public String getBook(@PathVariable Isbn isbn, Map<String, Object> model) { LOGGER.info("You searched for book with ISBN :: " + isbn.getIsbn()); model.put("isbn", isbn); return "index"; } @InitBinder public void initBinder(WebDataBinder binder) { binder.registerCustomEditor(Isbn.class, new IsbnEditor()); } }
Just look at how we are not accepting ISBN value directly in @PathVariable Isbn isbn
variable. Our IsbnEditor
is pretty much simple, but you can have a full range of rules and validations there and it will work.
To display the supplied value, you do not need special way. Just plain old spring way.
index.jsp
<!DOCTYPE html> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <html lang="en"> <body> <h2>ISBN You searched is :: ${ isbn.displayValue }</h2> </body> </html>
Demo
Now test the application by running the spring boot application.
SpringBootWebApplication.java
package com.howtodoinjava.app.controller; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.support.SpringBootServletInitializer; @SpringBootApplication public class SpringBootWebApplication extends SpringBootServletInitializer { public static void main(String[] args) throws Exception { SpringApplication.run(SpringBootWebApplication.class, args); } }
Now hit the browser with URL: http://localhost:8080/books/978-3-16-148410-0
Verify the logs in server, and display in browser that isbn received as request input is correct as passed as path parameter.
2017-03-16 13:40:00 - You searched for book with ISBN :: 978-3-16-148410-0

Drop me your questions in comments section.
Happy Learning !!
Comments