Search code examples
springspring-mvcspring-roo

How to change the display name of a model in Spring MVC


I am working with a Spring MVC project and I can't figure out how to change the String representation of a Model in the Views.

I have a Customer model that has a ONE_TO_MANY relationship with a WorkOrder model. On the workorders/show.jspx the Customer is displayed as a String that is the first and last name, email address, and phone number concatenated.

How do I change this? I thought I could just change the toString method on the Customer, but that didn't work.


Solution

  • We found a good solution. There are toString() methods for all the models in ApplicationConversionServiceFactoryBean_Roo_ConversionService.aj

    You can just push the method for the Model you want into ApplicationConversionServiceFactoryBean.java and modify it. In my case I added this:

    public Converter<Customer, String> getCustomerToStringConverter() {
        return new org.springframework.core.convert.converter.Converter<com.eg.egmedia.bizapp.model.Customer, java.lang.String>() {
            public String convert(Customer customer) {
                return new StringBuilder().append(customer.getId()).append(' ').append(customer.getFirstName()).append(' ').append(customer.getLastName()).toString();
            }
        };
    }
    

    Spring uses this for all the view pages so this will change the String representation of your model across your whole app!