Versions Compared

Key

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

...

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

	&lt;form</p>

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

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

</body>
</html>

Create the Action class

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

...

We can just add a new action mapping to the file we started in the Hello World lesson.

Code Block
xml
xml
titleactionstruts.xml
&lt;<!DOCTYPE xworkstruts PUBLIC &quot;
    "-//OpenSymphonyApache Software GroupFoundation//XWork 1DTD Struts Configuration 2.0//EN&quot; &quot;"
    "http://wwwstruts.opensymphonyapache.comorg/xworkdtds/xworkstruts-12.0.dtd&quot;&gt;

&lt;xwork&gt;">

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

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

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

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

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

Create the success and error pages

...

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

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
&lt;html&gt;
&lt;head&gt;<html>
<head>
  &lt;title&gt;A<title>A simple form with data&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;data</title>
</head>
<body>
   &lt;p&gt;What<p>What is your name?&lt;/p&gt;</p>

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

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(&quot;name&quot;"name");
    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&quot;&gt;

&lt;xwork&gt;">

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

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

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

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

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

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

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=&quot;action2&quot;"action2" prefix=&quot;saf&quot; %&gt;
&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;Success"saf" %>
<html>
<head>
  <title>Success Page - Without Using Getters and Setters&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;p&gt;Setters</title>
</head>
<body>
  <p>
    Hello, &lt;saf<saf:property value=&quot;"parameters.yourName&quot;" /&gt;>!
  &lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;</p>
</body>
</html>

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!"

...