• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Possibility for (Custom type objects) & (object arrays) in Form Bean

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Friends,

I got a doubt whether it is possible to have attributes of custom type in the form bean. The custom type will be composed itself of two or more attributes. I am trying to understand whether the population of data into such kind of beans is possible in the existing Struts, without using custom request processor.

My other doubt is can we can have arrays of objects in the form bean. I have tried this and was successful, if I am submitting the data directly form multiple-selection allowed dropdown. I am trying to understand whether it is possible with the use of hidden fields by giving names such as "person[0]", "person[1]", and so.

Thanks in advance
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you definitely can have form beans that reference your own types or classes. For example, I can have an account attribute on my form which is a class I've coded containing name, id, etc.

I can then reference the account id in my struts tags as: <html:text property="account.id"/> I can go as many levels down as I need to as in:
account.partner.company.name

Here is a link to the portion of the documentation that explains this:

http://struts.apache.org/api/org/apache/struts/taglib/bean/package-summary.html#package_description

Pay particular attention to the secion entitled "property references"
 
Hemanth Pallavajula
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Thank you Merrill.

I was able to do that. I missed instantiating the inner bean inside the form-bean.

Thank You Again.
 
Hemanth Pallavajula
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Merill,

I was successful in implementing "Nested References" and "Indexed References". But for the "Combined References", the values are not getting populated.

My JSP is:-


The form bean is:-


The bean being used within the form bean is:-


Can you please help me with this.

The other issue which is that, for "indexed references", we need to instantiate the array in the form bean by giving the size at the compile time itself. Is there an approach so that, we can make that dynamic.

Thanks in advance.
[ March 14, 2005: Message edited by: Hemanth Pallavajula ]
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When trying to figure out what Struts will do with an <html:xxxx> tag, a helpful exercise is to write out the method calls that struts will make when the submit button is pressed. Based on the link I gave you in my previous post, here is a method call that will be made by struts when your form is submitted:

scriptDataBean.getComponentIds(0).setKey("14207);

If scriptDataBean is in request scope, when Struts tries to populate the bean, the componentIds array in scriptDataBean will contain 20 elements, each of them null. When struts calls getComponentIds(0), a null value will be returned. When it tries to execute setKey("14207") on a null value, a null pointer exception will be thrown.

One possible solution to this would be to add a reset() method to your form bean that places an instance of KeyValuePair in each of the 20 array elements. The reset() method is called by struts before trying to populate the form bean. See the documentation for the correct signature for this method.

Another possible solution would be to modify the getComponentIds() method so that it checks to see if the element is null, and if so instantiates a new KeyValuePair, places it in the array, and returns it.

In answer to your second question about how to make the size of your array dynamic, you could use one of the List classes (e. g. ArrayList) in the java.util package instead of an array.
[ March 14, 2005: Message edited by: Merrill Higginson ]
 
If you open the box, you will find Heisenberg strangling Shrodenger's cat. And waving this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic