Renaming a Spring MVC Model Attribute Attribute Property

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 with the name modelto something else, possibly advertisementwhen the binding happens, so that I can refer to it as advertisementin the view (thymeleaf, etc.).

Is this possible with Spring MVC?

edit 1 : here is my application ' AbstractFormclass:

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 that it uses Generics, which is very neat in the java part. However, I would like to customize the property name modelin the views, hence my question.

+4
source share
1 answer

You can try to follow in your controller

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

All Articles