Skip to content

3. Example 02 – Parameter Injection into the Action

3.1. The NetBeans project

  • in [1,2], the configuration files
  • in [3], the action
  • in [4], the view
  • in [5], the project libraries

3.2. The configuration files

The [web.xml] file is as follows:


<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
     version="3.0">
 
  <display-name>Struts Tutorial-001</display-name>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <session-config>
    <session-timeout>
            30
    </session-timeout>
  </session-config>
</web-app>

We have already commented on this file. We won’t do so again. Just remember that it ensures all URLs (line 15) are passed through the Struts filter (line 10).

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>
 <package name="default" namespace="/" extends="struts-default">
    <default-action-ref name="index" />
    <action name="index">
      <result type="redirectAction">
        <param name="actionName">Action1</param>
        <param name="namespace">/actions</param>
      </result>
    </action>
  </package>
  <package name="actions" namespace="/actions" extends="struts-default">
    <action name="Action1" class="actions.Action1">
      <result name="success">/views/Action1.jsp</result>
    </action>
  </package>
</struts>
  • Lines 7–15: define the [default] package, which is used when an action cannot be found in another package.
  • line 8: defines a default action for this package named index.
  • Lines 9–14: configure the action named index. Note that it is not associated with a class (the class attribute is missing on line 9).
  • line 10: the result is for the success key (the name attribute is missing). It is of type redirectAction (type attribute). This type allows an action to be redirected to another action. Here, when the client requests the /index action, it will be redirected to the /actions/Action1 action (lines 11-12).
  • Lines 16–20: define the actions package (name) associated with the actions at the URL /actions/Action (class).
  • Lines 17–19: configure the /actions/Action1 action (name). When this action is invoked, Struts will instantiate the actions.Action1 class (class), and then the execute method of that class will be executed. This method must return the "success" key, as it is the only key defined on line 18.
  • Line 18: For the success key, the view to be displayed will be the JSP page [vues/Action1.jsp].

Ultimately, we should note that the Struts project can only execute the action at the URL [actions/Action1].

3.3. The action

The Action1 action is represented by the following class:


package actions;

import com.opensymphony.xwork2.ActionSupport;

public class Action1 extends ActionSupport{
  
  // action model
  private String param1="value1";
  private String param2 = "value2";
  
  @Override
  public String execute(){
    return SUCCESS;
  }
  
  // getters and setters

  public String getParam1() {
    return param1;
  }

  public void setParam1(String param1) {
    this.param1 = param1;
  }

  public String getParam2() {
    return param2;
  }

  public void setParam2(String param2) {
    this.param2 = param2;
  }
  
}
  • lines 8-9: two fields accessible via get/set methods (lines 18-32)
  • lines 12–14: The execute method returns the key as expected. It does nothing else.

3.4. The JSP view

The Action1.jsp view 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>Action1</title>
  </head>
  <body>
    <h1>Action1</h1>
    param1=<s:property value="param1"/><br/>
    param2=<s:property value="param2"/><br/>
  </body>
</html>

This view is displayed after the [Action1].execute method is executed. It displays the values of the param1 (line 11) and param2 (line 19) fields of the [Action1] class.

3.5. Tests

Let’s run the [example-02] project:

  • in [1], the requested URL. Note that the initial requested URL was [/example-02] without an action. The [web.xml] and [struts.xml] files were then used. We saw that the [web.xml] file entrusted the processing of all requests to Struts 2. The [struts.xml] file was then used:

<struts>
 <package name="default" namespace="/" extends="struts-default">
    <default-action-ref name="index" />
    <action name="index">
      <result type="redirectAction">
        <param name="actionName">Action1</param>
        <param name="namespace">/actions</param>
      </result>
    </action>
  </package>
  <package name="actions" namespace="/actions" extends="struts-default">
    <action name="Action1" class="actions.Action1">
      <result name="success">/views/Action1.jsp</result>
    </action>
  </package>
</struts>

The URL [/example-02] without an action was handled by the default package in lines 2–10. Since there was no action in the URL, the action became "index" due to line 3 (default action). Lines 4–8 caused Struts to return a redirect URL [/example-02/actions/Action1] to the client, as shown in [1].

The [Action1] action was then executed as configured in lines 12–14. Its execute method was executed. We saw that it returned the success code. Line 13 of [struts.xml] caused the page [/views/Action1.jsp] to be returned to the browser:


...
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Action1</title>
  </head>
  <body>
    <h1>Action1</h1>
    param1=<s:property value="param1"/><br/>
    param2=<s:property value="param2"/><br/>
  </body>
</html>

Lines 9 and 10 displayed the values of the param1 and param2 fields from [Action1]. This is shown in [2].

Let’s request another URL:

In [1], the [Action1] action is requested with the parameters param1 and param2. Let’s return to the execution flow of a Struts action:

The [FilterDispatcher] controller executes the action’s execute method. The execution flow passes through interceptors. One of them processes the parameter string param1=something&param2=somethingelse . It then uses the setParam1 and setParam2 methods of the Action1 action:


package actions;

import com.opensymphony.xwork2.ActionSupport;

public class Action1 extends ActionSupport{
  
  // action model
  private String param1="value1";
  private String param2 = "value2";
  
  @Override
  public String execute(){
    return SUCCESS;
  }
  
  // getters and setters

  public String getParam1() {
    return param1;
  }

  public void setParam1(String param1) {
    this.param1 = param1;
  }

  public String getParam2() {
    return param2;
  }

  public void setParam2(String param2) {
    this.param2 = param2;
  }
  
}

The params interceptor executes the following methods:

[Action1].setParam1("something");
[Action1].setParam2("somethingelse");

Therefore, they must exist. Note the following: to retrieve the params from an HTTP request, the called Struts action must have fields with the same names as the parameters and associated get/set methods.

When the execute method of [Action1] runs, the param1 and param2 fields have been initialized by the params interceptor:

param1="something"
param2="somethingelse"

The execute method returns the success key, and the [Action1.jsp] page is displayed with the new values for param1 and param2 [2].