# How Do I: Use Multiple Forms in a JSP?

In WebLogic Workshop you can easily use multiple forms in the same JSP. This topic describes how to set up a page flow with multiple forms in a single JSP, whose form fields are either empty or prefilled with data.

To Create a JSP with Multiple Empty Forms

When you use forms in a JSP, the action attribute on the netui:form tag determines which action method is called when the user submits the form. The action method in turns determines which form bean the form binds to through the actionForm data binding context. This is illustrated in the following example, in which two forms are used on a JSP:

    Form1:
    <netui:form action="submitForm1">
        <netui:textBox dataSource="{actionForm.userName}"/>
        <netui:button>search</netui:button>
    </netui:form>
    ...
    Form2:
    <netui:form action="submitForm2">
        <netui:textBox dataSource="{actionForm.administrativeID}"/>
        <netui:button>search</netui:button>
    </netui:form>

The first form calls the action method submitForm1 shown below:

    /**
     * @jpf:action
     * @jpf:forward name="formPage" path="nextPage1.jsp"
     */
    public Forward submitForm1(UserNameForm form)
    {
      ...
    }

This action method has an instance of the form bean UserNameForm as its argument. This means that when the user submits the first form, the value the user entered in the text box is posted to the userName property of the UserNameForm object. Notice that the text box binds to the userName property of the UserNameForm object using the actionForm data binding context.

Similarly, the second form on the JSP calls the action method submitForm2, which is shown below:

    /**
      * @jpf:action
      * @jpf:forward name="formPage" path="nextPage2.jsp"
    */
    public Forward submitForm2(AdministrativeForm form)
    {
       ...
    }

This action method uses the form bean class AdministrativeForm, which contains the property administrativeID. It is this property that the second form binds to and uses to store the data the user enters.

The Flow View of the controller file will look like this:

Because the netui:form tag's action attribute determines the action method, and indirectly the form bean, that will be used by the form and its form fields, you can easily include multiple forms on a single JSP that use different form beans and action methods.

To Create a JSP with Multiple Prefilled Forms

If you want to create a JSP with multiple forms, and the forms' fields need to be prepopulated with form bean data before the JSP is loaded and displayed to the user, you need to create the form bean objects and add these to the Forward object that loads the JSP. When you only have one form, you can add the form bean instance as an argument to the Forward object (for more information, see Using Data Binding in Page Flows). When you have multiple forms, you need to use the method addOutputForm.

The following example, which builds on the previous example, shows an action method that creates two form bean instances, adds values to the form beans' properties, and adds the form bean objects to the Forward object, which loads the JSP containing the multiple forms:

    /**
     * @jpf:action
     * @jpf:forward name="formPage" path="multipleFormPage.jsp"
    */
    public Forward goToMultipleFormPage()
    {
        Forward fwd = new Forward("formPage");

        UserNameForm form1 = new UserNameForm();
        form1.userName = "jbrown";
        fwd.addOutputForm(form1);

        AdministrativeForm form2 = new AdministrativeForm();
        form2.administrativeID = "18493AX";
        fwd.addOutputForm(form2);

        return fwd;
    }

The action method creates the UserNameForm form bean instance and sets its userName property to jbrown. As we saw above, this form bean is used by the first form (because its action attribute calls an action method that uses this form bean), so the textbox in the first form will contain the value jbrown when the page is loaded. Similarly, the textbox in the second form binds to the property administrativeID of the form bean AdministrativeForm. An instance of this form bean is created in the action method and its property administrativeID is set to 18493AX.

Related Topics

Using Data Binding in Page Flows

Designing User Interfaces in JSPs