wood burning stoves 2.0*
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 OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA 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
Author

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(RequestUtils.java:774)
at org.apache.struts.action.ActionServlet.processPopulate(Lorg.apache.struts.action.ActionForm;Lorg.apache.struts.action.ActionMapping;Ljavax.servlet.http.HttpServletRequest V(ActionServlet.java:2061)

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.


Merrill
Consultant, Sima Solutions
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
Is User its own class located in the file User.java? 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 frustrating...as 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: http://aspose.com/file-tools
 
subject: access the properties of an object inside actionform