• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

access the properties of an object inside actionform

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Think of how stupid the average person is. And how half of them are stupider than that. But who reads this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic