Link to home
Start Free TrialLog in
Avatar of tcollogne
tcollogne

asked on

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

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?
Avatar of el_dios
el_dios

Hi tcollogne,
> So we want this jsp page to be generated with the values allready filled in.

> What is the best way of accomplishing this?
I dont think there is any Struts support for this. I think you could use a 2D array and iterate through it.

Cheers!
Avatar of tcollogne

ASKER

Is it not possible to create an instance of the form bean in the "goto" action. Which would then be automatically used for filling in the fields?
Or something like that?
ASKER CERTIFIED SOLUTION
Avatar of gksinghiet
gksinghiet

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is the strutcure of my app. Between brackets is the name of the Form bean

GoToMakeReservationAction( GoToMakeReservationForm)   --> makeReservation.jsp   --> MakeReservationAction (MakeReservationForm)   --> result.jsp

If I understand it correctly I should put this code in the GoToMakeReservationAction

MakeReservationForm f = new MakeReservationForm();
f.setName("me");
request.setAttribute("makeReservationForm",f);

So I create an instance of the MakeReservationForm (not GoToMakeReservationForm) in the GoToMakeReservationAction and put the values.

Then on my makeReservation.jsp page I have the code

               <html:form action="/makeReservation">
                  <html:text property="name"/>
                  <html:submit/><html:cancel/>
            </html:form>


Is this correct?
This is one way of doing the same but the ideal way for a struts application is:
In GoToMakeReservationAction::

MakeReservationForm f = (MakeReservationForm) form;
f.setName(<Your queried value>);
....
return (mapping.findForward("success"));

In struts-config.xml::
 
   <!--Your form bean-->
  <form-beans>
    <!-- name form bean -->
    <form-bean name="reservationForm" type="<MakeReservationForm>"/>
  </form-beans>

   <!--Your action mapping-->
    <action path="/setName"
            type="<GoToMakeReservationAction>"
            name="reservationForm"
            scope="request"
            input="/index.jsp">
      <forward name="success" path="/makeReservation.jsp "/>
    </action>
So what you say is using the same form bean for both actions?

Do you have only one such query or many such situations, then it would be better for a generic solution.
In the form have the names which you want to display as the headings in an array or arraylist.
Have the values to be displayed in a 2D array. Forrow the steps gksinghiet has provided. I think that would be enough.
No only one situation.