Contact

Technology

Jan 22, 2009

Spring MVC Custom Property Editors

Credera Team

Credera Team

Default image background

From my experience, one of the initially frustrating things in Spring MVC was figuring out how to use a Property Editor correctly.

The idea is simple. You want to be able to convert back and forth between Java objects and their String representations. For example, let’s say you are making an internal website for a company and you want to display a list of employees. You want to be able to select an employee from the drop-down list, submit a form, and do some operation on the selected employee. Property Editors allow you to convert between your Employee objects and their String representations in the list. Spring has a number of built-in Property Editors, but for an Employee object, we will have to create a custom Property Editor to make this conversion. I found the official Spring documentation on this subject to be good, but a bit confusing on this subject.

Using Spring MVC with annotations, there are five concepts that will work together to achieve our desired goal.

1. There is an Employee object with this structure:

[code lang=”java”]public class Employee {

private Long employeeID;

private String name;

} [/code]

2. In the controller, we want to get a list of Employee objects and create a Model Attribute for use in the drop-down on the JSP:

[code lang=”java”]

@ModelAttribute(“employees”)

public List initEmployees() {

return employeeDAO.getEmployees();

}[/code]

3. In the JSP, we want to be able to populate a drop-down list with the Employee objects and perform an operation on one of them. The drop-down on the JSP looks like this:

[code lang=”html”]

[/code]

4. Next we create the custom Property Editor. The setAsText(String) method overrides the default implementation in the PropertyEditorSupport class and will read in the selected employeeID from the drop-down list and convert it back to the appropriate Employee object.

[code lang=”java”]

public class EmployeeNamePropertyEditor extends PropertyEditorSupport {

EmployeeDAO employeeDAO;

public EmployeeNamePropertyEditor(EmployeeDAO employeeDAO)   {

this.employeeDAO = employeeDAO;

}

public void setAsText(String text) {

Employee employee = new Employee();

employee = employeeDAO.getEmployee(Long.parseLong(text));

setValue(employee);

}

} [/code]

5. The last change is in the controller class. We need to register this Property Editor and associate it with the Employee object in every controller where we will need to make the conversion.

[code lang=”java”]

@InitBinder

public void initBinder(WebDataBinder binder)    {

binder.registerCustomEditor(Employee.class, new EmployeeNamePropertyEditor(employeeDAO));

} [/code]

After completing these steps, you should be able to properly configure a Property Editor for use with any Object that you like.

Conversation Icon

Contact Us

Ready to achieve your vision? We're here to help.

We'd love to start a conversation. Fill out the form and we'll connect you with the right person.

Searching for a new career?

View job openings