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)
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();
Joined: Feb 02, 2005
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]
Joined: Feb 15, 2005
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.
Joined: Feb 02, 2005
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.
subject: access the properties of an object inside actionform