Previous Next vertical dots separating previous/next from contents/index/pdf

How to Submit User Data from a JSP

This topic explains how to set up user data submission using an HTML form, a form bean (a Java representation of the HTML form), and an action.

The purpose of these instructions is to make you familiar with some of the main dialogs and wizards to help you accomplish this coding goal. These instructions are not intended to apply to every case where a form is required for user submitted data. For example, these instruction may not apply directly if you already have a pre-existing form beans. In that case, the instruction below can modified to utilize your pre-existing resources: where the instructions tell you to create a form bean, simply select the pre-existing item from the dropdown list.

These instructions assume that you have a dynamic web project (New > Project > Other > Web > Dynamic Web Project) that contains a page flow.

To Create a Form Bean to Model Submitted Data

(If you already have a form bean, you can skip this step.)

  1. Open the Page Flow perspective (Window > Open Perspective > Page Flow).
  2. On the Page Flow Explorer tab, right-click the Form Bean node and select New Inner Class Form Bean.
  3. On the Page Flow Explorer tab, right-click the new form bean (named NewFormBean by default) and select Rename. Rename the form bean appropriately (e.g., Customer, Order, etc.).
  4. On the Page Flow Explorer tab, double-click the form bean to view its source. Add private fields to the form bean class, for example:
        @Jpf.FormBean
            public static class Customer implements java.io.Serializable {
      
            private String firstName;
            private String lastName;
      
        }
  5. Right-click within the body of the Controller class and select Source > Generate Getters and Setters. This will create public getter and setter methods for the form bean's private fields.

To Create an Action and a User Input Form Based on a Form Bean

  1. Open the JSP page where you want the form to appear.
  2. From the JSP Design Palette drag and drop the node Create Form onto the JSP page.
  3. In the Create Form wizard, in the Action section, click New. (If you already have an action you want to use, do not click New. Instead select that action from the dropdown list and skip the next step.)
  4. In the New Action wizard, in the Action Template field, select Basic Method Action.
    In the Action Name field, enter an appropriate name.
    In the Form Bean field, select the form bean created above.
    In the Forward To field, select an appropriate destination to forward the user to after data has been submitted.
    Click Finish.
  5. In the Create Form wizard, click Next.
    On the Select Properties page, select the form bean fields that will appear in the user input form.
    Click Next.
    On the Arrange Fields page, select the order that the fields should appear on the JSP page.
    Click Finish.

The code created should look like something like the following:

form.jsp page

<netui:form action="nameAction">
    <table>
         <tr valign="top">
             <td><label for="field1"> FirstName: </label></td>
             <td><netui:textBox dataSource="actionForm.firstName" tagId="field1"></netui:textBox></td>
         </tr>
         <tr valign="top">
             <td><label for="field2"> LastName: </label></td>
             <td><netui:textBox dataSource="actionForm.lastName" tagId="field2"></netui:textBox></td>
         </tr>
    </table>
    <netui:button value="nameAction" type="submit" />
</netui:form>

Controller.java

    @Jpf.Action(forwards = { @Jpf.Forward(name = "success", path = "confirm.jsp") })
    public Forward nameAction(Controller.NameForm form) {
        Forward forward = new Forward("success");
        return forward;
    }

        ...

    @Jpf.FormBean
    public static class NameForm implements java.io.Serializable {
        private static final long serialVersionUID = 1815159769L;
	  
        private String firstName;
        private String lastName;
  
        public String getFirstName() {
            return firstName;
        }
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
        public String getLastName() {
            return lastName;
        }
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    }

Related Topics

Create Form Wizard

JSP Design Palette View

JSF Tutorial: Step 2: Create a JSF Web Application

 

Skip navigation bar   Back to Top