struts的动态表单的应用
如果你使用过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="pathname
闽公网安备 35060202000074号