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-issuemaster opened this issue Nov 6, 2018 · 1 comment
Assignees

Comments

@spring-issuemaster
Copy link
Collaborator

@spring-issuemaster spring-issuemaster commented Nov 6, 2018

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-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Nov 6, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.