Versions Compared

Key

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

Update parent page

...

Code Block
html
html
titlehelloName.html
<html>
<head>
	<title>A&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;A simple form with data</title>
</head>
<body>
	<p>Whatdata&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;p&gt;What is your name?</p>&lt;/p&gt;

	<form&lt;form action="&quot;helloName.action"&quot; method="post">&quot;post&quot;&gt;
		<p><input&lt;p&gt;&lt;input type="text"&quot;text&quot; name="name"></p>
		<p><input type="submit" value="&quot;name&quot;&gt;&lt;/p&gt;
		&lt;p&gt;&lt;input type=&quot;submit&quot; value=&quot;Submit your name."&quot; /></p>
	</form>

</body>
</html>&gt;&lt;/p&gt;
	&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;

Create the Action class

The HTML form submits an attribute called "name", and the Action class provides a corresponding JavaBean property.

...

Code Block
xml
xml
titleaction.xml
<&lt;!DOCTYPE xwork PUBLIC "&quot;-//OpenSymphony Group//XWork 1.0//EN" "&quot; &quot;http://www.opensymphony.com/xwork/xwork-1.0.dtd">

<xwork>&quot;&gt;

&lt;xwork&gt;
  <include&lt;include file="&quot;action-default.xml"&quot; />&gt;

  <package&lt;package name="default"&quot;default&quot; extends="&quot;action-default">&quot;&gt;

    <action&lt;action name="helloWorld"&quot;helloWorld&quot; class="&quot;tutorial.HelloWorld">&quot;&gt;
      <result&lt;result name="success">helloWorld.jsp</result>&quot;success&quot;&gt;helloWorld.jsp&lt;/result&gt;
    </action>&lt;/action&gt;

    <action&lt;action name="helloName"&quot;helloName&quot; class="&quot;tutorial.HelloName">&quot;&gt;
      <result&lt;result name="success">helloName&quot;success&quot;&gt;helloName-success.jsp</result>jsp&lt;/result&gt;
      <result&lt;result name="error">helloName&quot;error&quot;&gt;helloName-error.jsp</result>jsp&lt;/result&gt;
    </action>&lt;/action&gt;

  </package>
</xwork>&lt;/package&gt;
&lt;/xwork&gt;

Create the success and error pages

...

Code Block
html
html
titlehelloName-success.jsp
<%@&lt;%@ taglib uri="action2"&quot;action2&quot; prefix="saf" %>
<html>
<head>&quot;saf&quot; %&gt;
&lt;html&gt;
&lt;head&gt;
    <title>Success Page</title>
</head>
<body>&lt;title&gt;Success Page&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    <p>&lt;p&gt;
      Hello, <saf&lt;saf:property value="name"&quot;name&quot; />&gt;!
    </p>
</body>
</html>&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
Code Block
html
html
titlehelloName-error.html
<html>
<head>
	<title>Error Page</title>
</head>
<body>
<p>&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;Error Page&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;p&gt;
Hmmm, you did not enter a name. Please try again!
</p>
</body>
</html>&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;

Try it!

If you are coding along, go ahead and try your form now. Open the input page (http://localhost/tutorial/helloName.html), and click the submit button to see what happens. Try it with and without entering a name.

...

Code Block
html
html
titlehelloName.html
<html>
<head>&lt;html&gt;
&lt;head&gt;
  <title>A&lt;title&gt;A simple form with data</title>
</head>
<body>data&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
   <p>What&lt;p&gt;What is your name?</p>&lt;/p&gt;

   <form&lt;form action="&quot;helloName2.action"&quot; method="post">&quot;post&quot;&gt;
     <p><input&lt;p&gt;&lt;input type="text"&quot;text&quot; name="name"></p>&quot;name&quot;&gt;&lt;/p&gt;
     <p><input&lt;p&gt;&lt;input type="submit"&quot;submit&quot; value="&quot;Submit your name."&quot; /></p>&gt;&lt;/p&gt;
   </form>
</body>
</html>&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

Create the Action class

Code Block
java
java
titleHelloName2.java
package tutorial;

import com.opensymphony.xwork.ActionSupport;
import org.apache.struts.action2.interceptor.ParameterAware;

import java.util.Map;

public class HelloName2 extends ActionSupport implements ParameterAware {

  Map parameters;

  public Map getParameters() {
    return parameters;
  }

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

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

...

Code Block
xml
xml
titleaction.xml
<&lt;!DOCTYPE xwork PUBLIC "&quot;-//OpenSymphony Group//XWork 1.0//EN" "&quot; &quot;http://www.opensymphony.com/xwork/xwork-1.0.dtd">

<xwork>&quot;&gt;

&lt;xwork&gt;
  <include&lt;include file="&quot;action-default.xml"&quot; />&gt;

  <package&lt;package name="default"&quot;default&quot; extends="&quot;action-default">&quot;&gt;

     <action&lt;action name="helloWorld"&quot;helloWorld&quot; class="&quot;tutorial.HelloWorld">&quot;&gt;
       <result&lt;result name="success">helloWorld.jsp</result>&quot;success&quot;&gt;helloWorld.jsp&lt;/result&gt;
     </action>&lt;/action&gt;

     <action&lt;action name="helloName"&quot;helloName&quot; class="&quot;tutorial.HelloName">&quot;&gt;
       <result&lt;result name="success">helloName&quot;success&quot;&gt;helloName-success.jsp</result>jsp&lt;/result&gt;
       <result&lt;result name="error">helloName&quot;error&quot;&gt;helloName-error.jsp</result>jsp&lt;/result&gt;
     </action>&lt;/action&gt;

     <action&lt;action name="helloName2"&quot;helloName2&quot; class="&quot;tutorial.HelloName2">&quot;&gt;
       <result&lt;result name="success">helloName2&quot;success&quot;&gt;helloName2-success.jsp</result>jsp&lt;/result&gt;
       <result&lt;result name="error">helloName&quot;error&quot;&gt;helloName-error.jsp</result>jsp&lt;/result&gt;
     </action>&lt;/action&gt;

  </package>
</xwork>&lt;/package&gt;
&lt;/xwork&gt;

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 <saf:property> tag.

Code Block
html
html
<%@&lt;%@ taglib uri="action2"&quot;action2&quot; prefix="saf" %>
<html>
<head>
  <title>Success&quot;saf&quot; %&gt;
&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;Success Page - Without Using Getters and Setters</title>
</head>
<body>
  <p>Setters&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;p&gt;
    Hello, <saf&lt;saf:property value="&quot;parameters.yourName"&quot; />&gt;!
  </p>
</body>
</html>&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;

Try it!

Go ahead and try it now. Load helloName.html, enter "Bob" in the text field, and click the form submit button. You should see helloName2-success.jsp saying "Hello, Bob!"

...