![]() | 本教程假设你以完成 Coding Struts 2 Actons 课程,在 Google Code - http://code.google.com/p/struts2-examples/downloads/list下载Coding_Actions_Struts2_Ant (或Coding_Actions_Struts2_Mvn) |
引言
本课将学习使用Struts 2关于表单处理的更多的知识,将讲解怎样使用Java model类存储一个表单的输入域,并学习怎样用建立Struts 2表单匹配model类。
表单和Java Model类
假设用户要提交一个注册的表单,用户需要提供first name, last name, email address, 和age。
用一个JavaBean封装这些数据,在 the Coding Struts 2 Actions项目的org.apache.struts.register.model包下加入Person类。
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() ;
}
}
上面的类注意几点:setter/getter,toString方法,age是int型。
表单结构
创建此表单的关键在于联系表单域和model类的成员变量,先写一个view页面register.jsp,后面再说别的。
<?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>
因为使用Struts 2标签,页面顶部要加Struts标签库声明。
该Struts 2表单会提交到一个register action,在struts.xml中定义这个action。
注意四个Struts 2 textfield标签,每个标签有一个包含Person类属性的name值。name属性值也没有一个personBean对象的引用,这个对象是Person类型,当我们建立处理这个表单的Action时,要在action类中包含这个对象。
完整的name值personBean.firstName使Struts 2用输入的值作为参数传给personBean对象的setFirstName方法。
注意age属性是int值,Struts 2会将输入自动转换成int传给setAge方法。
建立一个处理代表提交内容的Action类
类代码如下:
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;
}
}
Register类覆盖了execute方法,该方法只返回一个SUCCESS,但在真实的应用中execute方法可能会调用其他类或者对象处理表单逻辑,例如将信息存入数据库。
personBean成员变量和表单中的name属性的personBean相匹配。Struts 2 framework会查找Action类并查找叫 personBean的对象。它会使用Person的default constructor创建对象。然后调用personBean对象的setXXX方法并向其传入输入内容作参数。这些动作都发生在execute方法执行前。
当execute方法执行时,各个成员变量已经有相应的值了。
通过使用一个Java model类封装表单域可以避免action类中有好多setter/getter。
处理SUCCESS返回值
我们想在execute方法返回SUCCESS时显示一个thank.jsp页面,代码如下:
<?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>
在struts.xml插入action节点
xml代码如下:
<action name="register" class="org.apache.struts.register.action.Register" method="execute"> <result name="success">/thankyou.jsp</result> </action>
我们不必告诉Struts 2任何关于处理表单的事情,只要对应好name属性值和model类的成员变量的名字相同即可。(e.g. personBean.lastName)
建立到register.jsp的链接
在index.jsp加入以下代码:
<p><a href="register.jsp">Please register</a> for our prize drawing.</p>
运行实例
点击index.jsp中到register.jsp页面的链接,可以看到:
填写表单并提交,会看到thankyou.jsp页面。
下篇教程会介绍表单的校验。