by james m. turner
这篇文章以实例代码来阐述dynaforms在struts1.1种的引用??译者注
如果你使用过struts先前的版本,你就会注意到你需要花费大量的时候来写actionform类文件,而这些类文件对于struts都是非常关键的(它充当“view”的一部分),通常它的结构就是bean properties在加上一个validate方法(有时还有reset方法)。
随着struts1.1版本的推出,开发员有了另外一种方法来完成前面的任务:使用dynabeans。dynabeans动态生成java beans。这就意味着我们可以通过配置(通常利用xml)
来生成formbean而不是在formbean中硬编码。
为了了解dynabeans(struts中为dynaforms)是如何工做的,让我们看一个简单的表单,字段有:name,address,telephone等,下面的代码为通常的写法(没有使用dynaforms)。
article1.customerform
package article1;
import org.apache.struts.action.actionform;
import org.apache.struts.action.actionerrors;
import org.apache.struts.action.actionmapping;
import org.apache.struts.action.actionerror;
import javax.servlet.http.httpservletrequest;
public class customerform extends actionform {
protected boolean nullorblank (string str) {
return ((str == null) || (str.length() == 0));
}
public actionerrors validate(actionmapping mapping,
httpservletrequest request) {
actionerrors errors = new actionerrors();
if (nullorblank(lastname)) {
errors.add("lastname",
new actionerror("article1.lastname.missing"));
}
if (nullorblank(firstname)) {
errors.add("firstname",
new actionerror("article1.firstname.missing"));
}
if (nullorblank(street)) {
errors.add("street",
new actionerror("article1.street.missing"));
}
if (nullorblank(city)) {
errors.add("city",
new actionerror("article1.city.missing"));
}
if (nullorblank(state)) {
errors.add("state",
new actionerror("article1.state.missing"));
}
if (nullorblank(postalcode)) {
errors.add("postalcode",
new actionerror("article1.postalcode.missing"));
}
if (nullorblank(phone)) {
errors.add("phone",
new actionerror("article1.phone.missing"));
}
return errors;
}
private string lastname;
private string firstname;
private string street;
private string city;
private string state;
private string postalcode;
private string phone;
public string getlastname() {
return lastname;
}
public void setlastname(string lastname) {
this.lastname = lastname;
}
public string getfirstname() {
return firstname;
}
public void setfirstname(string firstname) {
this.firstname = firstname;
}
public string getstreet() {
return street;
}
public void setstreet(string street) {
this.street = street;
}
public string getcity() {
return city;
}
public void setcity(string city) {
this.city = city;
}
public string getstate() {
return state;
}
public void setstate(string state) {
this.state = state;
}
public string getpostalcode() {
return postalcode;
}
public void setpostalcode(string postalcode) {
this.postalcode = postalcode;
}
public string getphone() {
return phone;
}
public void setphone(string phone) {
this.phone = phone;
}
}
看到上边的写法(这么长一段代码[虽然大多的工具都可以自动生成set和get方法]感想如何?如果要为每一个表单配备一个formbean,那么将是一件多了令人痛苦的事情??译者注),你知道了它是一个标准的javabean,只是多了一个validate方法,validate方法确保client断的输入都是合法的。
相应的jsp页面同样也是很简单的,如下:
customer.jsp
<%@ taglib uri="/web-inf/c.tld" prefix="c" %>
<%@ taglib prefix="fmt" uri="/web-inf/fmt.tld" %>
<%@ taglib uri="/web-inf/struts-tiles.tld" prefix="tiles" %>
<%@ taglib uri="/web-inf/struts-html.tld" prefix="html" %>
<head>
<title>example of a standard customer form</title>
</head>
<h1>example of a standard customer form</h1>
<html:form action="/addcustomer">
last name: <html:text property="lastname"/>
<html:errors property="lastname" /><br>
first name: <html:text property="firstname"/>
<html:errors property="firstname" /><br>
street addr: <html:text property="street"/>
<html:errors property="street" /><br>
city: <html:text property="city"/>
<html:errors property="city" /><br>
state: <html:text property="state" maxlength="2" size="2" />
<html:errors property="state" /><br>
postal code: <html:text property="postalcode" maxlength="5"
size="5" />
<html:errors property="postalcode" /><br>
telephone: <html:text property="phone" maxlength="11" size="11" />
<html:errors property="phone" /><br>
<html:submit/>
</html:form>
相应的action也没有复杂的业务代码,只是将从client端传过来的值打印到控制台。
article1.addcustomeraction
package article1;
import org.apache.struts.action.action;
import org.apache.struts.action.actionmapping;
import org.apache.struts.action.actionforward;
import org.apache.struts.action.actionform;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import javax.servlet.servletexception;
import java.io.ioexception;
public class addcustomeraction extends action {
public actionforward execute(actionmapping mapping,
actionform form,
httpservletrequest request,
httpservletresponse response)
throws servletexception, ioexception{
customerform custform = (customerform) form;
system.out.println("lastname = "
+ custform.getlastname());
system.out.println("firstname = "
+ custform.getfirstname());
system.out.println("street = " + custform.getstreet());
system.out.println("city = " + custform.getcity());
system.out.println("state = " + custform.getstate());
system.out.println("postalcode = "
+ custform.getpostalcode());
system.out.println("phone = " + custform.getphone());
return mapping.findforward("success");
}
}
下面看看struts-config.xml的配置,struts利用该配置文件将上述文件联系到一起来协同完成任务。
<struts-config>
<form-beans>
<form-bean name="customerform" type="jdj.article1.customer" />
</form-beans>
<action-mappings>
<action path="/addcustomer" type="article1.addcustomeraction"
name="customerform" scope="request"
input="/addcustomer.jsp">
<forward name="success" path="/addcustomersucceeded.jsp"
redirect="false" />
</action>
</action-mappings>
<message-resources parameter="applicationresources" />
<plug-in classname="org.apache.struts.validator.validatorplugin">
<set-property value="/web-inf/validator-rules.xml"
property="pathnames" />
struts-config.xml</plug-in></struts-config>
<?xml version="1.0" encoding="utf-8"?>
<!doctype struts-config public
"-//apache software foundation//dtd struts configuration 1.1//en"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean name="customerform" type="article1.customerform" />
</form-beans>
<action-mappings>
<action path="/addcustomer" type="article1.addcustomeraction"
name="customerform" scope="request" input="/customer.jsp">
<forward name="success" path="/addcustomersucceeded.jsp"
redirect="false" />
</action>
</action-mappings>
<message-resources parameter="applicationresources" />
<plug-in classname="org.apache.struts.validator.validatorplugin">
<set-property value="/web-inf/validator-rules.xml"
property="pathnames" />
</plug-in>
</struts-config>
上边通过配置,customerform来引用custemerform类, “/addcustomer”action使用customerform并且触发article1.addcustomeraction来处理请求。
到现在为止,上边代码熟悉struts得都应该很熟悉但是,如果应用struts1.1的新特性,你将会用更少的代码来完成上述同样的功能。使用dynaforms,我们应该更改customerform在struts-config.xml中信息来使用org.apache.struts.action.dynaactionform (为了便于读者比较使用前后的差别,我们将使用新的类新的jsp页面来完成同样的功能)
使用dynaactionform,你可以利用form-property xml标签,它允许你在struts-config.xml中定义formbean的属性元素。以我们的例子来说,struts-config.xml中将是如下这个样子:
<form-bean name="dynacustomerform"
type="org.apache.struts.action.dynaactionform">
<form-property name="lastname" type="java.lang.string"/>
<form-property name="firstname" type="java.lang.string"/>
<form-property type="java.lang.string" name="street"/>
<form-property name="city" type="java.lang.string"/>
<form-property name="state" type="java.lang.string"/>
<form-property name="postalcode" type="java.lang.string"/>
</form-bean>
上边的改动对于jsp页面没有任何的影响。不过你要对于原来的action进行稍微的改动应为:你现在已经不在向execute()中传递formbean(没有get set方法),所以 你应该把form转型到dynaactionform,然后利用方法get(filename)来取得client端数据新的action代码如下:
article1.adddynacustomeraction
package article1;
import org.apache.struts.action.*;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import javax.servlet.servletexception;
import java.io.ioexception;
public class adddynacustomeraction extends action {
public actionforward execute(actionmapping mapping,
actionform form,
httpservletrequest request,
httpservletresponse response)
throws servletexception, ioexception{
dynaactionform custform = (dynaactionform) form;
system.out.println("lastname = " + custform.get("lastname"));
system.out.println("firstname = " + custform.get("firstname"));
system.out.println("street = " + custform.get("street"));
system.out.println("city = " + custform.get("city"));
system.out.println("state = " + custform.get("state"));
system.out.println("postalcode = "
+ custform.get("postalcode"));
system.out.println("phone = " + custform.get("phone"));
return mapping.findforward("success");
}
}
从上边的代码可以看出,似乎”屏蔽“了actionform,然而我们也“丢失”了一些其他的,譬如:严整输入合法性的问题。有两种方法可以恢复校验功能:一是创建一个dynaactionform的子类,然后在子类中实现validate()方法。如下代码:
article1.dynacustomerform
package article1;
import org.apache.struts.action.*;
import javax.servlet.http.httpservletrequest;
public class dynacustomerform extends dynaactionform {
protected boolean nullorblank (string str) {
return ((str == null) || (str.length() == 0));
}
public actionerrors validate(actionmapping mapping,
httpservletrequest request) {
actionerrors errors = new actionerrors();
if (nullorblank((string)this.get("lastname"))) {
errors.add("lastname",
new actionerror("article1.lastname.missing"));
}
if (nullorblank((string)this.get("firstname"))) {
errors.add("firstname",
new actionerror("article1.firstname.missing"));
}
if (nullorblank((string)this.get("street"))) {
errors.add("street",
new actionerror("article1.street.missing"));
}
if (nullorblank((string)this.get("city"))) {
errors.add("city", new actionerror("article1.city.missing"));
}
if (nullorblank((string)this.get("state"))) {
errors.add("state",
new actionerror("article1.state.missing"));
}
if (nullorblank((string)this.get("postalcode"))) {
errors.add("postalcode",
new actionerror("article1.postalcode.missing"));
}
if (nullorblank((string)this.get("phone"))) {
errors.add("phone", new actionerror("article1.phone.missing"));
}
return errors;
}
}
如果是这样,我们就要更改struts-config.xml来使用dynaactionform的子类,这样的效果似乎是又回到了先前的样子(为每一个表单写dynaactionform),呵呵。。。
所以推荐的做法是使用struts1.1种的validator framework,这方面的内容在以后的文章中在说明。
关于作者:
james turner is the owner and manager of black bear software, llc, which specializes in custom java-based e-commerce and crm solutions delivery. he is also the author of "mysql and jsp web applications: data-driven programming using tomcat and mysql" (isbn: 0672323095) and is the co-author of "struts: kick start" (isbn: 0672324725), which will be published in november. he can be reached at turner@blackbear.com.
