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

Posted on 2006-04-12
Last Modified: 2010-04-01
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?
Question by:tcollogne
    LVL 3

    Expert Comment

    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.


    Author Comment

    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?
    LVL 5

    Accepted Solution

    >>Is it not possible to create an instance of the form bean in the "goto" action...
    Yes it is possible. You need to create the goto action mapping to use the newly created form and populate that form with the queried values. Now in your JSP page use that form using <html:form> tag. You get the queried values there.
    Let me know if you want more explanation.

    Author Comment

    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();

    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"/>

    Is this correct?
    LVL 5

    Expert Comment

    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-->
        <!-- name form bean -->
        <form-bean name="reservationForm" type="<MakeReservationForm>"/>

       <!--Your action mapping-->
        <action path="/setName"
          <forward name="success" path="/makeReservation.jsp "/>

    Author Comment

    So what you say is using the same form bean for both actions?

    LVL 3

    Expert Comment

    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.

    Author Comment

    No only one situation.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Suggested Solutions

    Title # Comments Views Activity
    Help with LDAp login using jsp 6 101
    issue with pdf generation 2 74
    String format issue 13 86
    Java Exception example issues 11 75
    I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
    Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now