Skip to content

6. Example 05 – The Input Form

We’ll introduce the concept of an input form using a simple example.

6.1. The NetBeans project

In [1], the project:

  • the views [Saisie.jsp], [Confirmation.jsp]
  • the configuration file [struts.xml]
  • the message file [messages.properties]
  • the action [Confirm.java]

In [2], the entry page

6.2. Configuration

The [struts.xml] file is as follows:


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
  <!-- internationalization -->
  <constant name="struts.custom.i18n.resources" value="messages" />
  <!-- default package -->
  <package name="default" namespace="/" extends="struts-default">
    <default-action-ref name="index" />
    <action name="index">
      <result type="redirectAction">
        <param name="actionName">Enter</param>
        <param name="namespace">/actions</param>
      </result>
    </action>
  </package>
  <!-- actions package -->
  <package name="actions" namespace="/actions" extends="struts-default">
    <action name="Enter">
      <result name="success">/views/Entry.jsp</result>
    </action>
    <action name="Confirm" class="actions.Confirm">
      <result name="success">/views/Confirmation.jsp</result>
    </action>  
  </package>
</struts>
  • Line 20: the action package for the URL /actions/Action.
  • lines 21–23: define the [Enter] action. No class is associated with it. The response is always the view [/views/Enter.jsp]
  • Lines 24–26: define the [Confirm] action. The [actions.Confirm] class is associated with it. The response is always the view [/views/Confirmation.jsp]

6.3. The [Confirm] action

Its code is as follows:


package actions;

import com.opensymphony.xwork2.ActionSupport;

public class Confirm extends ActionSupport{
  
  // model
  private String name;
  
  // getters and setters

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
  
}

The class has only one field, the one on line 8 with its get/set methods.

6.4. The messages file

The contents of [messages.properties] are as follows:


input.text=Input form
input.title1=Input
input.title2=Input
input.label=Enter your name
entry.submit=Submit
confirm.title1=Confirmation
confirm.title2=Confirmation
confirm.text=Hello
confirm.return=Return to the entry form

These message keys are used in both the [Entry.jsp] and [Confirmation.jsp] views.

6.5. The views

6.5.1. The [Entry.jsp] view

Visually, it looks like this:

Its source code is as follows:


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title><s:text name="saisie.titre1"/></title>
  </head>
  <body>
    <h1><s:text name="saisie.titre2"/></h1>
    <s:form action="Confirm">
      <s:textfield key="saisie.libelle" name="name"/>
      <s:submit key="submit" action="Confirm"/>
    </s:form>
  </body>
</html>
  • Line 7: Displays the message for key "saisie.titre1" (Input) [1].
  • line 10: displays the message for key "saisie.titre2" (Entry) [2].
  • Lines 11, 14: The <s:form> tag introduces an HTML form. All entries within this form will be sent to the web server. We say they are posted because the HTTP operation involved in sending this data to the server is called POST. The data is sent in the form of a string: param1=value1&param2=value2&... We have already encountered this string in Section 3.5 of Example 02. In both cases, the parameter string is processed in the same way by Struts 2. The values of the parameters are injected into the executed action, into fields bearing the same names as the parameters. To whom is the posted data sent? By default, to the action that displayed the form, here the Saisir action [5]. You can also use the action attribute to specify another action, here the [Confirmer] action.
  • Line 12: The <s:textfield ...> tag displays the input field [3]. Its key attribute specifies the key for the label to be displayed to the left of the input field. The label is therefore looked up in the [messages.properties] file. The name attribute is the name of the parameter that will be posted. The value associated with this parameter will be the text entered in the input field.
  • Line 13: The <s:submit ...> tag displays the button [4]. Its key attribute specifies the key for the label to be displayed on the button. The label is therefore retrieved from the [messages.properties] file. A submit button triggers the form to be posted to an action. We mentioned earlier that this was the [Confirm] action due to the action attribute of the <s:form> tag. The action attribute of the <s:submit> tag allows you to specify a different action if desired. Here, we have set the action to [Confirm]. The button has a default parameter name: action:action_name, here action:Confirm. The value associated with this parameter is the button label. Ultimately, the parameter string posted to the [Confirm] action when the user clicks the [Validate] button will be:

?name=xx&action:Confirm=Validate.

where xx is the text entered by the user in the input field.

6.5.2. The [Confirmation.jsp] view

Visually, it looks like this:

We enter a name and submit the input page. We then receive the response [1], which is the [Confirmation.jsp] view. The URL displayed in [2] is that of the [Confirm] action. In the example above, the following string was posted:

name=bernard&action:Confirm=Validate

The action:Confirm parameter allows Struts 2 to route the request to the correct action, in this case the [Confirm] action. The value of the name parameter will then be injected into the name field of the [Confirm] action.

The source code for the [Confirmation.jsp] action is as follows:


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title><s:text name="confirm.title1"/></title>
  </head>
  <body>
    <h1><s:text name="confirm.title2"/></h1>
    <s:text name="confirm.text"/>&nbsp;
    <s:property value="name"/> !
    <br/><br/>
    <a href="<s:url action="Submit"/>"><s:text name="confirm.return"/></a>
  </body>
</html>
  • line 7: displays the key label confirm.title1 found in the message file [1]
  • line 10: displays the key label confirm.title2 found in the message file [3]
  • line 12: displays the name property of the [Confirm] action that was executed.
  • line 14: creates a link to the [Enter] action. The <s:url> tag, which we’ve seen before, will be generated from the browser’s URL [2]. The path will be that of [2] http://localhost:8084/exemple-05/actions and the action will be that of the action attribute, in this case Enter. The link's URL will therefore be http://localhost:8084/exemple-05/actions/Saisir.action. The link text will be the label associated with the confirm.retour key [4].

6.6. The tests

Here are two requests:

In [1], enter a name and submit. In [2], the confirmation page.

In [3], we follow the link back to the form. In [4], the result. Everything has been explained, except the transition from [3] to [4]. Why don’t we see the name we entered in [4]?

The link in [3] is a link to the URL [http://localhost:8084/exemple-05/actions/Saisir.action]. The [Submit] action is therefore executed. Let’s look at its configuration in [struts.xml]:


<package name="actions" namespace="/actions" extends="struts-default">
    <action name="Enter">
      <result name="success">/vues/Saisie.jsp</result>
    </action>
    <action name="Confirm" class="actions.Confirm">
      <result name="success">/views/Confirmation.jsp</result>
    </action>  
</package>

The [Enter] action is not associated with any action. Therefore, the [Entry.jsp] view is displayed immediately. Let's look at the code for this view:


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title><s:text name="saisie.titre1"/></title>
  </head>
  <body>
    <h1><s:text name="saisie.titre2"/></h1>
    <s:form>
      <s:textfield key="saisie.libelle" name="name"/>
      <s:submit key="saisie.valider" action="Confirm"/>
    </s:form>
  </body>
</html>

On line 12, the input field is displayed. Its content is initialized with the name property. Recall what was mentioned earlier regarding how the <s:property> tag works:

The <s:property name="property"> tag allows you to write the value of a property of an object called ActionContext. This object contains:

  1. the properties of the class associated with the action that was executed.
  2. the attributes of the current request denoted as <s:property name="#request['key']">
  3. the attributes of the user’s session, denoted as <s:property name="#session['key']">
  4. attributes of the application itself, denoted as <s:property name="#application['key']">
  5. the parameters sent by the client browser, denoted as <s:property name="#parameters['key']">
  6. The notation <s:property name="#attr['key']"> displays the value of an object searched for in the page, the request, the session, and the application, in that order.

We are in case 1 here. The [Enter] action has no associated class. The name property therefore does not exist. An empty string is then displayed in the input field.

To display in [4] the name that was entered in [1], we will use the user’s session.