This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Struts and the fly likes access the properties of an object inside actionform Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "access the properties of an object inside actionform" Watch "access the properties of an object inside actionform" New topic

access the properties of an object inside actionform

Suma Rangaraj
Ranch Hand

Joined: Feb 02, 2005
Posts: 50
Hello all,

I have a dataobject User[just one object, not a collection] and an actionForm MyForm. I have declared a User object inside the acitonform.

I am prepopulating the user.jsp with User data. But when I am submitting the form, the User data object is not beign populated. It gives an error...
I will be glad if anyone can give me pointers. I have been getting this work for more than a day now..

javax.servlet.ServletException: BeanUtils.populate
at org.apache.struts.util.RequestUtils.populate(Ljava.lang.Object;Ljava.lang.String;Ljava.lang.String;Ljavax.servlet.http.HttpServletRequest V(
at org.apache.struts.action.ActionServlet.processPopulate(Lorg.apache.struts.action.ActionForm;Lorg.apache.struts.action.ActionMapping;Ljavax.servlet.http.HttpServletRequest V(

Thanks for your help.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I suspect that the BeanUtils that Struts uses are having a hard time dealing with an inner class. I'd suggest making User a separately defined class and see if that fixes it.

Consultant, Sima Solutions
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
Is User its own class located in the file I have tried using inner classes before and they did not seem to work. If that is not the case, then nothing else jumps out at me. What does the HTML created look like? Are you pretty sure it is one of these hidden fields that is causing the error? I take it that your form has a method named getUser()
and that your User class has the methods setUserName() and setUserId();

- Brent
Suma Rangaraj
Ranch Hand

Joined: Feb 02, 2005
Posts: 50
Thanks Merrill and Brent for your response.
Actually, as Brent pointed out, the problem was not with populating the properties of the user object rather the other properties of MyForm. So now I am able to access the User object upon submit.

It work now but another question -
1. If the User object object extends say DefaultUser.

2. The otehr things is if teh User data object has a collection say arrayList, I get a null value on submittign the form. I doublechecked if the size of the list before populating the form.

I have been trying various ways ..but it's very the user object that I have is a complex one.
Also, I ended up settign the User data object in session[I was tryign to avoid settign it in the session and hence have it in my form]

Any ideas?
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Regarding your question about using inherited properties, I use them all the time, and Struts handles them just fine. Just make sure the getters and setters are declared as public, not protected or private.

Regarding your second question: Struts will not directly populate an ArrayList in a form. For example, if you have a series of checkboxes all named the same using property="myBoxes", you must declare the property "myBoxes" as type String[]. It cannot be an ArrayList.

If your user object is a complex one, I'd suggest leaving it in the form and then specifying scope="session" in your action mapping. This JavaWorld article on Struts Best Practices recommends using session scope as a best practice as long as you clean up after yourself. I've found that particularly with complex objects, session scope is better than request scope because it saves you having to rebuild the object after each form submission.
Suma Rangaraj
Ranch Hand

Joined: Feb 02, 2005
Posts: 50
Thanks for your response.

May be I should give another shot regarding inherited properties.

Also, as you have pointed out, I have ended up using teh User in session instead of setting it in the form.

Thanks again for your immediate response.
I agree. Here's the link:
subject: access the properties of an object inside actionform
Similar Threads
ActionForm question (struts 1)
Forwarding to external URL
Problem in accessing the Action class
How to use logic:iterate tag
RequestProcessor problem on a cluster