Versions Compared

Key

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

...

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

	<form action="form03.action" method="post">
		<p><input type="text" name="yourName"></p>
		<p><input type="submit" value="Submit your name." /></p>
	</form>

</body>
</html>

 

2. Create the form action

...

Code Block
java
java
package lessons;

import com.opensymphony.xwork.ActionSupport;

public class Form03Action extends ActionSupport {

  String yourName;

  public void setYourName(String p_yourName) {
    yourName = p_yourName;
  }

  public String getYourName() {
    return yourName;
  }


  public String execute() throws Exception {
    if (yourName == null || yourName.length() == 0)
      return ERROR;
    else
      return SUCCESS;
  }

}

 

3. Register the action in xwork.xml:

...

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

<xwork>
  <!-- Include webwork defaults (from WebWork JAR). -->
  <include file="webwork-default.xml" />
  
  <!-- Configuration for the default package. -->
  <package name="default" extends="webwork-default">
    <!-- Default interceptor stack. --> 
    <default-interceptor-ref name="defaultStack" /> 
    
    <!-- 02 --> 
    <action name="form02" class="lessons.Form02Action"> 
      <result name="success" type="dispatcher">page02-success.jsp</result> 
    </action> 

    <!-- 03 -->
    <action name="form03" class="lessons.Form03Action">
      <result name="success" type="dispatcher">page03-success.jsp</result>
      <result name="error" type="dispatcher">page03-error.jsp</result>
    </action>

  </package>
</xwork>

 

4. Create the success and error pages

...

Code Block
html
html
<%@ taglib uri="webwork" prefix="ww" %>
<html>
<head>
	<title>Success page for form with data</title>
</head>
<body>

Hello, <ww:property value="yourName" />!

</body>
</html>

 

Create webapp/page03-error.jsp:

Code Block
html
html
<html>
<head>
	<title>Error page for form with data</title>
</head>
<body>

Hmm, you don't seem to have entered a name. Go back and try again please.

</body>
</html>

 

Try it

Don't forget to compile your action to webapp/WEB-INF/classes, and to restart your web application if necessary.

...

  • When the action is called, its setYourName() setter method is called with the contents of the form field named yourName.
  • After the action has been called (which is when its execute() method returns), WebWork has two options. If ERROR is returned, WebWork will return page03-error.jsp; if SUCCESS, page03-success.jsp. Just as in the last lesson, the <ww:property> tag calls the action's getter (in this case, getYourName()).

...

For the form field named "yourName" in the previous lesson, we also had to create the getters and setters getYourName() and setYourName() in the action, as well as the private variable yourName. With dozens of forms and hundreds of form fields, you'll be typing thousands of getters and setters. That can get old fast. In this lesson, we'll repeat the last lesson, but without any of that extra typing.

...

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

	<form action="form04.action" method="post">
		<p><input type="text" name="yourName"></p>
		<p><input type="submit" value="Submit your name." /></p>
	</form>

</body>
</html>

 

2. Create the form action

Paste this code into src/lessons/Form04Action.java:

Code Block
java
java
package lessons;

import com.opensymphony.xwork.ActionSupport;
import com.opensymphony.webwork.interceptor.ParameterAware;

import java.util.Map;

public class Form04Action extends ActionSupport implements ParameterAware {

  Map parameters;

  public Map getParameters() {
    return parameters;
  }

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

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

 

Register the action in xwork.xml:

Edit webapp/WEB-INF/classes/xwork.xml:

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

<xwork>
  <!-- Include webwork defaults (from WebWork JAR). -->
  <include file="webwork-default.xml" />
  
  <!-- Configuration for the default package. -->
  <package name="default" extends="webwork-default">
    <!-- Default interceptor stack. --> 
    <default-interceptor-ref name="defaultStack" /> 
    
    <!-- 02 --> 
    <action name="form02" class="lessons.Form02Action"> 
      <result name="success" type="dispatcher">page02-success.jsp</result> 
    </action> 

    <!-- 03 -->
    <action name="form03" class="lessons.Form03Action">
      <result name="success" type="dispatcher">page03-success.jsp</result>
      <result name="error" type="dispatcher">page03-error.jsp</result>
    </action>
    
    <!-- 04 -->
    <action name="form04" class="lessons.Form04Action">
      <result name="success" type="dispatcher">page04-success.jsp</result>
      <result name="error" type="dispatcher">page03-error.jsp</result>
      <interceptor-ref name="servlet-config"/>
    </action>

  </package>
</xwork>

 

Create the success and error pages

...

Code Block
html
html
<%@ taglib uri="webwork" prefix="ww" %>
<html>
<head>
	<title>Success page for form with data</title>
</head>
<body>

Hello, <ww:property value="parameters.yourName" />!

</body>
</html>

 

Try it

Don't forget to compile your action to webapp/WEB-INF/classes, and to restart your web application if necessary.

...

Back on the page04-success.jsp page, <ww:property value="yourName" /> isn't going to work any more, because there is no getYourName() getter in the action. Instead, <ww:property value="parameters.yourName" /> calls the getParameters() getter, and is able to get the value of the "yourName" field. Pretty neat!

Wiki Markup
We haven't covered how to handle radio buttons, checkboxes, and other strange html form fields. That involves dealing with the fact that every entry in the {{parameters}} Map is a String\[\]. We'll cover this in a later lesson.