Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
xml
xml
titlestruts.xml
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
  <include file="struts-default.xml" />

    <package name="tutorialactions" extends="struts-default">

      <action name="HelloWorld" class="tutorial.HelloWorld">
          <result name="success">/tutorial/HelloWorld.jsp</result>
      </action>

      <action name="HelloName" class="tutorial.HelloName">
            <result name="success">HelloName>/HelloName-success.jsp</result>
            <result name="error">HelloName-error.jsp<html</result>
        </action>

    </package>

</struts>

Create the success and error pages

...

Code Block
html
html
titleHelloName-error.html
<html>
<head>
    <title>Error Page</title>
</head>
<body>
    <p>
        Hmmm, you did not enter a name. Please try again!
    </p>
</body>
</html>

Try it!

...

Let's use the same HTML form, but change the form Action to HelloName2.action:

Code Block
html
html
titleHelloNameHelloName2.html
<html>
<head>
  <title>A simple form with data</title>
</head>
<body>
    <p>What is your name?</p>

    <form action="HelloName2.action" method="post">
        <p><input type="text" name="name"></p>
        <p><input type="submit" value="Submit your name." /></p>
    </form>
</body>
</html>

Create the Action class

Code Block
java
java
titleHelloName2.java
package tutorial;

package tutorial;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.ParameterAware;

import java.util.Map;

public class HelloName2 extends ActionSupport implements ParameterAware {

    public String execute() {
        String[] name = (String[]) parameters.get("name");
        if (name == null || name[0] == null || name[0].length() == 0)
            return ERROR;
        else
            return SUCCESS;
    }

    Map parameters;

    public Map getParameters() {
        return parameters;
    }

    public void setParameters(Map parameters) {
        this.parameters = parameters;
    }
}

...

Code Block
xml
xml
titlestruts.xml
<?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>
    <include file="struts-default.xml"/>

    <package name="tutorialactions" extends="struts-default">

        <action name="HelloWorldHelloName" class="tutorial.HelloWorldHelloName">
            <result name="success">/tutorial/HelloWorld.jsp</result>
        </action>

        <action name="HelloName" class="tutorial.HelloName">
            <result name="success">HelloName-success.jsp</result>
            <result name="error">HelloName-error.html</result>
        </action>

        <action name="HelloName2" class="tutorial.HelloName2">
            <result name="success">HelloName2-success.jsp</result>
            <result name="error">HelloName-error.html</result>
        </action>

    </package>

</struts>

Create the success and error pages

We can use the same error page, but we'll need a slightly different success page HelloName2-success.jsp. The only difference is the <s:property> tag.

Code Block
html
html
titleHelloName2-success.jspl
<%@ taglib uriprefix="/tagss" prefixuri="s/tags" %>
<html>
<head>
    <title>Success Page - Without Using Getters and Setters</title>
</head>

<body>
<p>
    Hello, <s:property value="parameters.name"/>!
</p>
</body>
</html>

...