选择显示字体大小

struts的动态表单的应用


这篇文章以实例代码来阐述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=&quot;/web-inf/c.tld&quot; prefix=&quot;c&quot; %>
<%@ taglib prefix=&quot;fmt&quot; uri=&quot;/web-inf/fmt.tld&quot; %>
<%@ taglib uri=&quot;/web-inf/struts-tiles.tld&quot; prefix=&quot;tiles&quot; %>
<%@ taglib uri=&quot;/web-inf/struts-html.tld&quot; prefix=&quot;html&quot; %>

<head>
<title>example of a standard customer form</title>
</head>
<h1>example of a standard customer form</h1>
<html:form action=&quot;/addcustomer&quot;>
last name: <html:text property=&quot;lastname&quot;/>
<html:errors property=&quot;lastname&quot; /><br>
first name: <html:text property=&quot;firstname&quot;/>
<html:errors property=&quot;firstname&quot; /><br>
street addr: <html:text property=&quot;street&quot;/>
<html:errors property=&quot;street&quot; /><br>
city: <html:text property=&quot;city&quot;/>
<html:errors property=&quot;city&quot; /><br>
state: <html:text property=&quot;state&quot; maxlength=&quot;2&quot; size=&quot;2&quot; />
<html:errors property=&quot;state&quot; /><br>
postal code: <html:text property=&quot;postalcode&quot; maxlength=&quot;5&quot;
size=&quot;5&quot; />
<html:errors property=&quot;postalcode&quot; /><br>
telephone: <html:text property=&quot;phone&quot; maxlength=&quot;11&quot; size=&quot;11&quot; />
<html:errors property=&quot;phone&quot; /><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(&quot;lastname = &quot;
+ custform.getlastname());
system.out.println(&quot;firstname = &quot;
+ custform.getfirstname());
system.out.println(&quot;street = &quot; + custform.getstreet());
system.out.println(&quot;city = &quot; + custform.getcity());
system.out.println(&quot;state = &quot; + custform.getstate());
system.out.println(&quot;postalcode = &quot;
+ custform.getpostalcode());
system.out.println(&quot;phone = &quot; + custform.getphone());

return mapping.findforward(&quot;success&quot;);
}
}

 

下面看看struts-config.xml的配置,struts利用该配置文件将上述文件联系到一起来协同完成任务。

<struts-config>
<form-beans>
<form-bean name=&quot;customerform&quot; type=&quot;jdj.article1.customer&quot; />
</form-beans>
<action-mappings>
<action path=&quot;/addcustomer&quot; type=&quot;article1.addcustomeraction&quot;
name=&quot;customerform&quot; scope=&quot;request&quot;
input=&quot;/addcustomer.jsp&quot;>
<forward name=&quot;success&quot; path=&quot;/addcustomersucceeded.jsp&quot;
redirect=&quot;false&quot; />
</action>
</action-mappings>
<message-resources parameter=&quot;applicationresources&quot; />
<plug-in classname=&quot;org.apache.struts.validator.validatorplugin&quot;>
<set-property value=&quot;/web-inf/validator-rules.xml&quot;
property=&quot;pathnames&quot; />
struts-config.xml</plug-in></struts-config>
<?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>
<!doctype struts-config public
&quot;-//apache software foundation//dtd struts configuration 1.1//en&quot;
&quot;http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd&quot;>
<struts-config>
<form-beans>
<form-bean name=&quot;customerform&quot; type=&quot;article1.customerform&quot; />
</form-beans>
<action-mappings>
<action path=&quot;/addcustomer&quot; type=&quot;article1.addcustomeraction&quot;
name=&quot;customerform&quot; scope=&quot;request&quot; input=&quot;/customer.jsp&quot;>
<forward name=&quot;success&quot; path=&quot;/addcustomersucceeded.jsp&quot;
redirect=&quot;false&quot; />
</action>
</action-mappings>
<message-resources parameter=&quot;applicationresources&quot; />
<plug-in classname=&quot;org.apache.struts.validator.validatorplugin&quot;>
<set-property value=&quot;/web-inf/validator-rules.xml&quot;
property=&quot;pathnames&quot; />
</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=&quot;dynacustomerform&quot;
type=&quot;org.apache.struts.action.dynaactionform&quot;>
<form-property name=&quot;lastname&quot; type=&quot;java.lang.string&quot;/>
<form-property name=&quot;firstname&quot; type=&quot;java.lang.string&quot;/>
<form-property type=&quot;java.lang.string&quot; name=&quot;street&quot;/>
<form-property name=&quot;city&quot; type=&quot;java.lang.string&quot;/>
<form-property name=&quot;state&quot; type=&quot;java.lang.string&quot;/>
<form-property name=&quot;postalcode&quot; type=&quot;java.lang.string&quot;/>
</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(&quot;lastname = &quot; + custform.get(&quot;lastname&quot;));
system.out.println(&quot;firstname = &quot; + custform.get(&quot;firstname&quot;));
system.out.println(&quot;street = &quot; + custform.get(&quot;street&quot;));
system.out.println(&quot;city = &quot; + custform.get(&quot;city&quot;));
system.out.println(&quot;state = &quot; + custform.get(&quot;state&quot;));
system.out.println(&quot;postalcode = &quot;
+ custform.get(&quot;postalcode&quot;));
system.out.println(&quot;phone = &quot; + custform.get(&quot;phone&quot;));

return mapping.findforward(&quot;success&quot;);
}
}

从上边的代码可以看出,似乎”屏蔽“了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(&quot;lastname&quot;))) {
errors.add(&quot;lastname&quot;,
new actionerror(&quot;article1.lastname.missing&quot;));
}
if (nullorblank((string)this.get(&quot;firstname&quot;))) {
errors.add(&quot;firstname&quot;,
new actionerror(&quot;article1.firstname.missing&quot;));
}
if (nullorblank((string)this.get(&quot;street&quot;))) {
errors.add(&quot;street&quot;,
new actionerror(&quot;article1.street.missing&quot;));
}
if (nullorblank((string)this.get(&quot;city&quot;))) {
errors.add(&quot;city&quot;, new actionerror(&quot;article1.city.missing&quot;));
}
if (nullorblank((string)this.get(&quot;state&quot;))) {
errors.add(&quot;state&quot;,
new actionerror(&quot;article1.state.missing&quot;));
}
if (nullorblank((string)this.get(&quot;postalcode&quot;))) {
errors.add(&quot;postalcode&quot;,
new actionerror(&quot;article1.postalcode.missing&quot;));
}
if (nullorblank((string)this.get(&quot;phone&quot;))) {
errors.add(&quot;phone&quot;, new actionerror(&quot;article1.phone.missing&quot;));
}
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 &quot;mysql and jsp web applications: data-driven programming using tomcat and mysql&quot; (isbn: 0672323095) and is the co-author of &quot;struts: kick start&quot; (isbn: 0672324725), which will be published in november. he can be reached at turner@blackbear.com.

  


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons