• 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

Value not accessible from form?

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm having problems accessing the value that I stored in a form, using a session scope.

In my ActionForm struts.form.DataForm, I have defined a String field "date" with getters and setters.

In my JSP, I set the field and in the MappingDispatchAction class, I print out the value to make sure it was set to whatever the user wanted.

Then I forward to a page with code:

<html:form action="/Blah">
...
<jsp:useBean id="theForm" type="struts.form.DataForm/>
...
</html:form>

And in the struts-config.xml I have set the formbean for that page, also using scope=session

Within the <html:form> I also call <% theForm.getDate(); %> but it doesn't return the value that was assigned previously. Can anyone see what I am doing wrong (or trying to do wrong?)

Thanks
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take out:

<jsp:useBean id="theForm" type="struts.form.DataForm/>

Not only do you not need it, but it's messing you up. Struts has already put an instance of DataForm in Session scope. You don't have to do anything to make that happen. Since you didn't specify a scope in your <jsp:useBean> tag, it defaults to page scope. What's happening, then, is that you're creating a second empty instance of DataForm in page scope, and this is the instance you're finding.
 
Darien Cheung
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for replying.

If I take out the <useBean> tag, how do I refer to the form in my JSP code?
 
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
You can access it in any Struts tags. If you specified this form bean in your action mapping in struts-config.xml, any html:xxx tags that you use will automatically refer to this form bean.

So, for example:

<html:text property="date" /> will refer to the date property of this bean.

You can access it in a bean:write tag like this:

<bean:write name="dataForm" property="date" />

This is assuming that "dataForm" is the name you gave the ActionForm when you defined it in the struts-config.xml file.

If you want to use the bean in a scriptlet, you will have to retrieve it from session scope like this:

<% struts.form.DataForm dataForm = (struts.form.DataForm) session.getAttribute("dataForm"); %>

Again, you must use the name you used in the struts-config.xml file.
 
Darien Cheung
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again! The last part was what I needed. I should have realized the object is stored there.
 
reply
    Bookmark Topic Watch Topic
  • New Topic