4

I have the following form:

public class ChildcareWorkerAdvertisementForm extends AbstractForm<ChildcareWorkerAdvertisement> {

    @Valid
    @Override
    //Rename the property from "model" to "advertisement"?
    public ChildcareWorkerAdvertisement getModel() {
        return super.getModel();
    }

}

I would like to rename the property named model to something else, perhaps advertisement when binding occurs so that I can refer to it as advertisement in the view (thymeleaf, etc...).

Is this possible using Spring MVC?

edit 1: Here is my application' AbstractForm class:

package com.bignibou.web.controller;

public class AbstractForm<T> {

    private T model;

    public T getModel() {
        return this.model;
    }

    public final void setModel(T model) {
        this.model = model;
    }
   ...

You can see it uses Generics which is very neat on the java part. However I would like to customize the name of the model property in the views hence my question.

balteo
  • 23,602
  • 63
  • 219
  • 412
  • How about this answer? http://stackoverflow.com/questions/8986593/how-to-customize-parameter-names-when-binding-spring-mvc-command-objects – Neil McGuigan Nov 13 '14 at 01:14
  • Can you post more details? Maybe sample code on github? What class is AbstractForm? Is it this one - http://spring-rich-c.sourceforge.net/1.1.0/apidocs/org/springframework/richclient/form/AbstractForm.html? – Jigish Nov 13 '14 at 02:04
  • @Jigish: the AbstractForm is a custom class I have included in my edit: see edit above. – balteo Nov 13 '14 at 10:23
  • @NeilMcGuigan: I did see this post but I was hoping Spring 4.1 provided a simpler way to achieve the property renaming on a controller-method basis... – balteo Nov 13 '14 at 10:26
  • 1
    have you tried just "add new" `advertisement` property to model e.g.:public ChildcareWorkerAdvertisement getAdvertismenet() { return super.getModel(); } and similarly for setter. – sodik Jan 04 '15 at 12:46

1 Answers1

0

You can try following in your controller

@Valid
@Override
@ModelAttribute("advertisement")
//Rename the property from "model" to "advertisement"?
public ChildcareWorkerAdvertisement getModel() {
    return super.getModel();
}
Zeronex
  • 434
  • 1
  • 3
  • 8