Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-17472

Call setter-method for array when binding model data

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Minor
    • Resolution: Works as Designed
    • Affects Version/s: 5.1.2
    • Fix Version/s: None
    • Component/s: Core
    • Labels:
    • Last commented by a User:
      true

      Description

      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.

        Attachments

          Activity

            People

            Assignee:
            juergen.hoeller Juergen Hoeller
            Reporter:
            patrikstarck Patrik Starck
            Last updater:
            Spring Issues Spring Issues
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Days since last comment:
              3 years, 29 weeks, 6 days ago