0

Struts 2 is giving a strange behavior that the commercial,productCategory objects is not set on on form submission.It happens in some cases like these.Other fields are getting set properly

Most of the cases struts 2 is setting the custom objects very well and even for complex objects like List inside a object but in this case it is giving strange behavior.

Can somebody help in this.Can i debug the struts 2 code to check why it is not setting the property.If possible let me know the steps

Hibernate domain object:

public class Brand {            
    private Long id;                
    private String brandName;               
    private String brandDescription;                
    private Brand productSubCategory;               
    private ProductCategory productCategory;                
    private CommercialType commercial;

    /* getters and setters */
}

JSP:

<s:select label = "Commercial Type" 
           name = "brand.commercial.id" 
           list = "#attr.masterData.commercialTypes" 
        listKey = "id" 
      listValue = "commercialType" 
          value = "%{brand.commercial.id}" 
          theme = "simple" 
      headerKey = "" 
    headerValue = "--Select--" 
/>
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
POJO
  • 11
  • 6
  • Do you get errors or it just doesn't set the value correctly? Are those fields pointing to instances of those classes or are they `null`? – Anthony Grist Feb 04 '14 at 14:43
  • It does n't just set the property commercial.No error. commercial is null."brand.commercial.id" value does not set. – POJO Feb 04 '14 at 14:56
  • Here is the exception with devmode true. Unexpected Exception caught setting 'brand.commercial.id' Error setting expression 'brand.commercial.id' with value ['1', ] – POJO Feb 04 '14 at 15:17
  • If `commercial` is null then you either need to store the object in the session scope, rather than request, or initialise the `commercial` field with an instance of the `CommercialType` class when you declare it. The former sounds like the correct route, to be honest, but it depends what you're doing with it. You can't set a field on a field that doesn't reference anything. – Anthony Grist Feb 04 '14 at 15:46
  • Thanks Anthony Grist.latter worked fine.I created the instance of child objects inside constructor of parent constructor.But I still wonder why this strange behavior.In most of the cases it works fine – POJO Feb 05 '14 at 08:53
  • For curiosity, can you post `CommercialType` complete code (constructors, getters and setters too) ? – Andrea Ligios Feb 05 '14 at 10:35

1 Answers1

0

Answer based on the first look

I didn't find any error in the code that you have posted. But according to the Error which its generated in the dev mode, My thoughts roam around this.

Error

Unexpected Exception caught setting 'brand.commercial.id' Error setting expression 'brand.commercial.id' with value ['1', ]

Possible reason

The brand.commercial.id is used as the name variable in two or more places in the same form . if the same variable is set twice in the same name, the struts will consider that as a list which makes this `['1', ] ,assuming the brand.commercial.id is not a list.

Other possibility is that, some list value is set to the integer value.

Dileep
  • 5,362
  • 3
  • 22
  • 38