File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes How to generate a jsp page with form in struts where input values are pre-generated? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to generate a jsp page with form in struts where input values are pre-generated?" Watch "How to generate a jsp page with form in struts where input values are pre-generated?" New topic
Author

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

Thierry Collogne
Greenhorn

Joined: Oct 19, 2005
Posts: 16
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?
sweta naidu
Greenhorn

Joined: Nov 02, 2004
Posts: 25
Set the values to bean and put the bean in request and use bean:write to display....
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
If you want input fields pre-populated, put your data in the ActionForm bean and use <html:xxx> tags instead of <input> tags.


Merrill
Consultant, Sima Solutions
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
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
Scott Updike
Ranch Hand

Joined: Feb 16, 2006
Posts: 92
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

Joined: Feb 15, 2005
Posts: 4864
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to generate a jsp page with form in struts where input values are pre-generated?