Info |
---|
This tutorial assumes you've completed the Coding Struts 2 Actons tutorial and have a working Coding_Actions_Struts2_Ant (or Coding_Actions_Struts2_Mvn) coding_actions project. The example code for this tutorial, Form_Processing_Struts2_Ant or Form_Processing_Struts2_Mvn, is available on Google Code - http://code.google.com/p/struts2-examples/downloads/list. After downloading and unzipping the file, you'll have a folder named Form_Processing_Struts2_Ant (or Form_Processing_Struts2_Mvn). In that folder will be a README.txt file with instructions on now to build and run the example applicationform_processing, is available for checkout from the Struts 2 GitHub subversion repository: https://github.com/apache/struts-examples. |
Introduction
In this tutorial we'll explore using Struts 2 to do more involved processing of a form submission. We'll cover how to use a Java model class to store the form input and how to create the Struts 2 form to match up with that model class.
...
To encapsulate this data, we'll use a simple Java class that follows the basic Java Bean specifications (public set/get methods for each instance field). If you're following along add this class to package org.apache.struts.register.model in the Coding Struts 2 Actions example.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
public class Person
{
private String firstName;
private String lastName;
private String email;
private int age;
public String getFirstName()
{
return firstName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public int getAge()
{
return age;
}
public void setAge( int age)
{
this.age = age;
}
public String toString()
{
return "First Name: " + getFirstName() + " Last Name: " + getLastName() +
" Email: " + getEmail() + " Age: " + getAge() ;
}
}
|
...
To collect the above information we'll use a Struts 2 form. When creating this form the key concept we need to employ is to tie each form field to a specific instance field of an object of type Person. Let's look over the form first and then discuss some key points. Create a view page named register.jsp (in WebContent (Ant version) or src/main/webapp (Maven version) )
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Register</title>
</head>
<body>
<h3>Register for a prize by completing this form.</h3>
<s:form action="register">
<s:textfield name="personBean.firstName" label="First name" />
<s:textfield name="personBean.lastName" label="Last name" />
<s:textfield name="personBean.email" label ="Email"/>
<s:textfield name="personBean.age" label="Age" />
<s:submit/>
</s:form>
</body>
</html>
|
...
Here is the Action class used for this example. Place it in package org.apache.struts.register.action.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
package org.apache.struts.register.action;
import org.apache.struts.register.model.Person;
import com.opensymphony.xwork2.ActionSupport;
public class Register extends ActionSupport {
private static final long serialVersionUID = 1L;
private Person personBean;
@Override
public String execute() throws Exception {
//call Service class to store personBean's state in database
return SUCCESS;
}
public Person getPersonBean() {
return personBean;
}
public void setPersonBean(Person person) {
personBean = person;
}
}
|
...
When SUCCESS is returned by the execute method we want to display a simple thank you page that shows the user's registration. Add the thankyou.jsp below to either WebContent (Ant) or src/main/webapp (Maven).
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Registration Successful</title>
</head>
<body>
<h3>Thank you for registering for a prize.</h3>
<p>Your registration information: <s:property value="personBean" /> </p>
<p><a href="<s:url action='index' />" >Return to home page</a>.</p>
</body>
</html>
|
...
To specify the relationship between the form submission page, the Struts 2 Action class, and the success view page we need to add an action node to struts.xml. Add this action node to struts.xml (src folder (Ant version) or src/main/resources (Maven version) ) after the hello action and before the closing package node.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<action name="register" class="org.apache.struts.register.action.Register" method="execute">
<result name="success">/thankyou.jsp</result>
</action>
|
...
So that the user can find the registration page, add this link to index.jsp
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<p><a href="register.jsp">Please register</a> for our prize drawing.</p>
|
...
If everything is correct, you should be able to create the war file (ant archive or mvn package), deploy the war file to your Servlet container, and open this URL in your web browser: http://localhost:8080/Form_Processing_Struts2_Antform_processing/index.action (Ant version) or http://localhost:8080/Form_Processing_Struts2_Mvn/index.action
(Maven version). On that page should be a link to register. Click on that link and you should see the register.jsp page.
...