Two Laptop Bag*
The moose likes Struts and the fly likes Possibility  for (Custom type objects) & (object arrays) in Form Bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Possibility  for (Custom type objects) & (object arrays) in Form Bean" Watch "Possibility  for (Custom type objects) & (object arrays) in Form Bean" New topic
Author

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

Hemanth Pallavajula
Ranch Hand

Joined: Oct 07, 2004
Posts: 38
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


Cheers,<br />Hemanth...<br />(When opportunity doesn't knock, build a door.)
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
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"


Merrill
Consultant, Sima Solutions
Hemanth Pallavajula
Ranch Hand

Joined: Oct 07, 2004
Posts: 38


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

Joined: Oct 07, 2004
Posts: 38
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

Joined: Feb 15, 2005
Posts: 4864
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Possibility for (Custom type objects) & (object arrays) in Form Bean
 
Similar Threads
collections doubt
useBean doubt
Doubt in Form Bean population
A JSTL doubt while assigning a variable using <c:set/>
Doubt on doubt