...
Code Block |
---|
| html |
---|
| html |
---|
title | helloName.html |
---|
|
<html>
<head>
<title>A<html>
<head>
<title>A simple form with data</title>
</head>
<body>
<p>Whatdata</title>
</head>
<body>
<p>What is your name?</p>
<form</p>
<form action=""helloName.action"" method="post">
<p><input type="text" name="name"></p>
<p><input type="submit" value=""post">
<p><input type="text" name="name"></p>
<p><input type="submit" value="Submit your name."" /></p>
</form>
</body>
</html>></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 |
---|
title | actionstruts.xml |
---|
|
<<!DOCTYPE xworkstruts PUBLIC "
"-//OpenSymphonyApache Software GroupFoundation//XWork 1DTD Struts Configuration 2.0//EN" ""
"http://wwwstruts.opensymphonyapache.comorg/xworkdtds/xworkstruts-12.0.dtd">
<xwork>">
<struts>
<include<include file="action"struts-default.xml"" />>
<package<package name="default""default" extends="action"struts-default">">
<action<action name="helloWorld""helloWorld" class=""tutorial.HelloWorld">">
<result<result name="success">helloWorld.jsp</result>"success">helloWorld.jsp</result>
</action></action>
<action<action name="helloName""helloName" class=""tutorial.HelloName">">
<result<result name="success">helloName"success">helloName-success.jsp</result>jsp</result>
<result<result name="error">helloName"error">helloName-error.jsp</result>jsp</result>
</action></action>
</package>
</xwork></package>
</struts>
|
Create the success and error pages
...
Code Block |
---|
| html |
---|
| html |
---|
title | helloName-success.jsp |
---|
|
<%@<%@ taglib uri="action2""action2" prefix="saf" %>
<html>
<head>"saf" %>
<html>
<head>
<title>Success Page</title>
</head>
<body><title>Success Page</title>
</head>
<body>
<p><p>
Hello, <saf<s:property value="name""name" />>!
</p>
</body>
</html></p>
</body>
</html>
|
Code Block |
---|
| html |
---|
| html |
---|
title | helloName-error.html |
---|
|
<html>
<head>
<title>Error Page</title>
</head>
<body>
<p><html>
<head>
<title>Error Page</title>
</head>
<body>
<p>
Hmmm, you did not enter a name. Please try again!
</p>
</body>
</html></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 |
---|
title | helloName.html |
---|
|
<html>
<head><html>
<head>
<title>A<title>A simple form with data</title>
</head>
<body>data</title>
</head>
<body>
<p>What<p>What is your name?</p></p>
<form<form action=""helloName2.action"" method="post">"post">
<p><input<p><input type="text""text" name="name"></p>"name"></p>
<p><input<p><input type="submit""submit" value=""Submit your name."" /></p>></p>
</form>
</body>
</html></form>
</body>
</html>
|
Create the Action class
Code Block |
---|
| java |
---|
| java |
---|
title | HelloName2.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""name");
if(name == null || name[0] == null || name[0].length() == 0)
return ERROR;
else
return SUCCESS;
}
}
|
...
Code Block |
---|
|
<<!DOCTYPE xwork PUBLIC ""-//OpenSymphony Group//XWork 1.0//EN" "" "http://www.opensymphony.com/xwork/xwork-1.0.dtd">
<xwork>">
<xwork>
<include<include file=""action-default.xml"" />>
<package<package name="default""default" extends=""action-default">">
<action<action name="helloWorld""helloWorld" class=""tutorial.HelloWorld">">
<result<result name="success">helloWorld.jsp</result>"success">helloWorld.jsp</result>
</action></action>
<action<action name="helloName""helloName" class=""tutorial.HelloName">">
<result<result name="success">helloName"success">helloName-success.jsp</result>jsp</result>
<result<result name="error">helloName"error">helloName-error.jsp</result>jsp</result>
</action></action>
<action<action name="helloName2""helloName2" class=""tutorial.HelloName2">">
<result<result name="success">helloName2"success">helloName2-success.jsp</result>jsp</result>
<result<result name="error">helloName"error">helloName-error.jsp</result>jsp</result>
</action></action>
</package>
</xwork></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 |
---|
|
<%@<%@ taglib uri="action2""action2" prefix="saf" %>
<html>
<head>
<title>Success"saf" %>
<html>
<head>
<title>Success Page - Without Using Getters and Setters</title>
</head>
<body>
<p>Setters</title>
</head>
<body>
<p>
Hello, <saf<saf:property value=""parameters.yourName"" />>!
</p>
</body>
</html></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!"
...