Renaming a nested property of a Spring MVC model attribute

777 views Asked by At

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.

1

There are 1 answers

0
Zeronex On

You can try following in your controller

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