Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call setter-method for array when binding model data [SPR-17472] #22004

Closed
spring-projects-issues opened this issue Nov 6, 2018 · 1 comment
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: declined A suggestion or change that we don't feel we should currently apply

Comments

@spring-projects-issues
Copy link
Collaborator

Patrik Starck opened SPR-17472 and commented

Problem

This is an issue concerning when you bind data for a model, for example when submitting a form with a modelAttribute bound to it. If the item to be set is of a multidimensional type, the setter of the property will never be called. 

Consider the following code:

 

public class Product {

    private String name;
    private String imageStr;
    private List<ProdutImage> productImageList;

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<ProductImage> getProductImageList() {
        return this.productImageList;
    }

    public void setProductImageList(List<ProductImage> productImageList) {
        this.productImageList = productImageList;
        if (productImageList != null) {
            this.imageStr = [...]// convert list to json string
        }
    }

}

And the corresponding JSP-page:

<form>
      <form:input type="text" path="product.name"/>
      <form:input type="text" path="product.productImageList[0]"/>
      <form:input type="text" path="product.productImageList[1]"/>
      <form:input type="text" path="product.productImageList[2]"/>
</form>

When submitting the form, the "setName" is be called, but not the "setProductImageList".

I don't think this is intened?

Proposed Solution

The problem occurs in the class AbstractNestablePropertyAccessor in function processKeyedProperty. When using an array/list/vector or other multi-dimensional container, the function doesn't call the write method for the current PropertyHandler.

A fix would be to add this line of code

ph.setValue(list);

after this line

list.add(convertedValue);

Then one would apply the same approach for "Map" and "Array" as well which is defined in the same function.


Affects: 5.1.2

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

This works as designed: Each such entry will be bound individually, dereferencing the list through its getter method and setting/adding the value at the corresponding index. A setter method for the entire list will only be called if a single value (e.g. comma-separated) is bound to the property itself, i.e. without any index, decomposing the value into a complete list of entries.

@spring-projects-issues spring-projects-issues added type: bug A general bug status: declined A suggestion or change that we don't feel we should currently apply in: core Issues in core modules (aop, beans, core, context, expression) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues removed the type: bug A general bug label Jan 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

No branches or pull requests

2 participants