这篇文章以实例代码来阐述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 clas