• 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

How to generate a jsp page with form in struts where input values are pre-generated?

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

In our web application (struts), we have an action that executes a query in the database. The results of this query are the values of the fields on the next page (the page that is being forwarded to).
So we want this jsp page to be generated with the values allready filled in.

What is the best way of accomplishing this?
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Set the values to bean and put the bean in request and use bean:write to display....
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want input fields pre-populated, put your data in the ActionForm bean and use <html:xxx> tags instead of <input> tags.
 
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure how much you have worked with Struts. One thing that confused me when getting started is the idea of "input form" versus "output form". Very often it seems like actions are given one form as input and need to create another form as output, but the execute method is only passed one form parameter.

I am not sure exactly what type of page you are developing. If you gave more details I might be able to give better advice. If I was developing a DisplayUserDetails action that took an id as a parameter, then I would configure the action to use the UserForm. Struts would instantiate a UserForm, save it on the request, and pass a reference to the form into the execute method. I would then extract the id from the form, execute a query, and populate the remaining fields on the form. In this case I was able to use the form parameter as both input and output from my action.

In other cases, you will have treat the form passed to the execute method as just input and then you need to instantiate an instance of your output form and save it to the request using request.setAttribute.

BTW, as much as possible I try to use the request rather than the session and to use ActionForms rather than placing individual objects on the request.

- Brent
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a similar situation where I have an input form that upon submission, the form data is saved to the database and is forwarded to an update screen with the same fields. The fields on both screens are mapped to the same form bean. However, the update screen pre-populates the fields from the property values in the form bean which I saved to the request object in the action servlet (via setAttribute). The fields were populated using the usual <html:text...value="${beanname.property}"> tags.

This presents a problem when you perform validation on the update screen. If the form validation fails and the action is forwarded to the INPUT paramter in the action element (i.e., the same screen) all my fields are blank. I assume because the action form now has a new request and ${beanname.property} is now unkown. Is there a better way to do this?

Thanks,
Scott
 
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

Originally posted by Scott Updike:
The fields were populated using the usual <html:text...value="${beanname.property}"> tags.



This "usual" method is not the correct way to use the <htm:text> tag. The correct way to use the tag is:

<html:form action="/myAction" />
<html:text property="myProperty" />

Where myProperty is a property of the ActionForm bean that is assigned to the Action called by the <html:form> tag in this JSP. (<action name="myForm" path="/myAction" /> The value is populated automatically, and there is no need to specify a "value" attribute in the tag. The value attribute is to be used only when you need to override what's in the ActionForm bean.

If you do this, the values will still be populated even if the page is redisplayed because of validation errors.
 
reply
    Bookmark Topic Watch Topic
  • New Topic