|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
JAVA是一种可以撰写跨平台应用软件的面向对象的程序设计语言,由升阳(SunMicrosystems)公司的詹姆斯·高斯林(JamesGosling)等人于1990年代初开发。静态
这篇文章以实例代码来论述Dynaforms在struts1.1种的援用??译者注
假如你利用过struts先前的版本,你就会注重到你必要消费大批的时分来写ActionForm类文件,而这些类文件关于struts都长短常关头的(它充任“View”的一部分),一般它的布局就是beanproperties在加上一个validate办法(偶然另有reset办法)。
跟着struts1.1版本的推出,开辟员有了别的一种办法来完成后面的义务:利用DynaBeans。DynaBeans静态天生JavaBeans。这就意味着我们能够经由过程设置(一般使用xml)
来天生formbean而不是在formbean中硬编码。
为了懂得DynaBeans(struts中为Dynaforms)是怎样工做的,让我们看一个复杂的表单,字段有:name,address,telephone等,上面的代码为一般的写法(没有利用Dynaforms)。
article1.CustomerForm
packagearticle1;
importorg.apache.struts.action.ActionForm;
importorg.apache.struts.action.ActionErrors;
importorg.apache.struts.action.ActionMapping;
importorg.apache.struts.action.ActionError;
importjavax.servlet.http.HttpServletRequest;
publicclassCustomerFormextendsActionForm{
protectedbooleannullOrBlank(Stringstr){
return((str==null)||(str.length()==0));
}
publicActionErrorsvalidate(ActionMappingmapping,
HttpServletRequestrequest){
ActionErrorserrors=newActionErrors();
if(nullOrBlank(lastName)){
errors.add("lastName",
newActionError("article1.lastName.missing"));
}
if(nullOrBlank(firstName)){
errors.add("firstName",
newActionError("article1.firstName.missing"));
}
if(nullOrBlank(street)){
errors.add("street",
newActionError("article1.street.missing"));
}
if(nullOrBlank(city)){
errors.add("city",
newActionError("article1.city.missing"));
}
if(nullOrBlank(state)){
errors.add("state",
newActionError("article1.state.missing"));
}
if(nullOrBlank(postalCode)){
errors.add("postalCode",
newActionError("article1.postalCode.missing"));
}
if(nullOrBlank(phone)){
errors.add("phone",
newActionError("article1.phone.missing"));
}
returnerrors;
}
privateStringlastName;
privateStringfirstName;
privateStringstreet;
privateStringcity;
privateStringstate;
privateStringpostalCode;
privateStringphone;
publicStringgetLastName(){
returnlastName;
}
publicvoidsetLastName(StringlastName){
this.lastName=lastName;
}
publicStringgetFirstName(){
returnfirstName;
}
publicvoidsetFirstName(StringfirstName){
this.firstName=firstName;
}
publicStringgetStreet(){
returnstreet;
}
publicvoidsetStreet(Stringstreet){
this.street=street;
}
publicStringgetCity(){
returncity;
}
publicvoidsetCity(Stringcity){
this.city=city;
}
publicStringgetState(){
returnstate;
}
publicvoidsetState(Stringstate){
this.state=state;
}
publicStringgetPostalCode(){
returnpostalCode;
}
publicvoidsetPostalCode(StringpostalCode){
this.postalCode=postalCode;
}
publicStringgetPhone(){
returnphone;
}
publicvoidsetPhone(Stringphone){
this.phone=phone;
}
}
看到上边的写法(这么长一段代码[固然年夜多的工具都能够主动天生set和get办法]感受怎样?假如要为每个表单装备一个formbean,那末将是一件多了使人疾苦的事变??译者注),你晓得了它是一个尺度的JavaBean,只是多了一个validate办法,validate办法确保client断的输出都是正当的。
响应的jsp页面一样也是很复杂的,以下:
customer.jsp
<%@tagliburi="/WEB-INF/c.tld"prefix="c"%>
<%@taglibprefix="fmt"uri="/WEB-INF/fmt.tld"%>
<%@tagliburi="/WEB-INF/struts-tiles.tld"prefix="tiles"%>
<%@tagliburi="/WEB-INF/struts-html.tld"prefix="html"%>
<head>
<title>ExampleofastandardCustomerform</title>
</head>
<h1>ExampleofastandardCustomerform</h1>
<html:formaction="/addCustomer">
LastName:<html:textproperty="lastName"/>
<html:errorsproperty="lastName"/><br>
FirstName:<html:textproperty="firstName"/>
<html:errorsproperty="firstName"/><br>
StreetAddr:<html:textproperty="street"/>
<html:errorsproperty="street"/><br>
City:<html:textproperty="city"/>
<html:errorsproperty="city"/><br>
State:<html:textproperty="state"maxlength="2"size="2"/>
<html:errorsproperty="state"/><br>
PostalCode:<html:textproperty="postalCode"maxlength="5"
size="5"/>
<html:errorsproperty="postalCode"/><br>
Telephone:<html:textproperty="phone"maxlength="11"size="11"/>
<html:errorsproperty="phone"/><br>
<html:submit/>
</html:form>
响应的action也没有庞大的营业代码,只是将从client端传过去的值打印到把持台。
article1.AddCustomerAction
packagearticle1;
importorg.apache.struts.action.Action;
importorg.apache.struts.action.ActionMapping;
importorg.apache.struts.action.ActionForward;
importorg.apache.struts.action.ActionForm;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjavax.servlet.ServletException;
importjava.io.IOException;
publicclassAddCustomerActionextendsAction{
publicActionForwardexecute(ActionMappingmapping,
ActionFormform,
HttpServletRequestrequest,
HttpServletResponseresponse)
throwsServletException,IOException{
CustomerFormcustForm=(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());
returnmapping.findForward("success");
}
}
上面看看struts-config.xml的设置,struts使用该设置文件将上述文件接洽到一同来协同完成义务。
<struts-config>
<form-beans>
<form-beanname="customerForm"type="jdj.article1.Customer"/>
</form-beans>
<action-mappings>
<actionpath="/addCustomer"type="article1.AddCustomerAction"
name="customerForm"scope="request"
input="/addCustomer.jsp">
<forwardname="success"path="/addCustomerSucceeded.jsp"
redirect="false"/>
</action>
</action-mappings>
<message-resourcesparameter="ApplicationResources"/>
<plug-inclassName="org.apache.struts.validator.ValidatorPlugIn">
<set-propertyvalue="/WEB-INF/validator-rules.xml"
property="pathnames"/>
struts-config.xml</plug-in></struts-config>
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEstruts-configPUBLIC
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-beanname="customerForm"type="article1.CustomerForm"/>
</form-beans>
<action-mappings>
<actionpath="/addCustomer"type="article1.AddCustomerAction"
name="customerForm"scope="request"input="/customer.jsp">
<forwardname="success"path="/addCustomerSucceeded.jsp"
redirect="false"/>
</action>
</action-mappings>
<message-resourcesparameter="ApplicationResources"/>
<plug-inclassName="org.apache.struts.validator.ValidatorPlugIn">
<set-propertyvalue="/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-propertyxml标签,它同意你在struts-config.xml中界说formbean的属性元素。以我们的例子来讲,struts-config.xml中将是以下这个模样:
<form-beanname="dynaCustomerForm"
type="org.apache.struts.action.DynaActionForm">
<form-propertyname="lastName"type="java.lang.String"/>
<form-propertyname="firstName"type="java.lang.String"/>
<form-propertytype="java.lang.String"name="street"/>
<form-propertyname="city"type="java.lang.String"/>
<form-propertyname="state"type="java.lang.String"/>
<form-propertyname="postalCode"type="java.lang.String"/>
</form-bean>
上边的修改关于jsp页面没有任何的影响。不外你要关于本来的action举行略微的修改应为:你如今已不在向execute()中传送formbean(没有getset办法),以是你应当把form转型到DynaActionForm,然后使用办法get(filename)来获得client端数据新的action代码以下:
article1.AddDynaCustomerAction
packagearticle1;
importorg.apache.struts.action.*;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjavax.servlet.ServletException;
importjava.io.IOException;
publicclassAddDynaCustomerActionextendsAction{
publicActionForwardexecute(ActionMappingmapping,
ActionFormform,
HttpServletRequestrequest,
HttpServletResponseresponse)
throwsServletException,IOException{
DynaActionFormcustForm=(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"));
returnmapping.findForward("success");
}
}
从上边的代码能够看出,仿佛”屏障“了actionform,但是我们也“丧失”了一些其他的,比如:严整输出正当性的成绩。有两种办法能够恢复校验功效:一是创立一个DynaActionForm的子类,然后在子类中完成validate()办法。以下代码:
article1.DynaCustomerForm
packagearticle1;
importorg.apache.struts.action.*;
importjavax.servlet.http.HttpServletRequest;
publicclassDynaCustomerFormextendsDynaActionForm{
protectedbooleannullOrBlank(Stringstr){
return((str==null)||(str.length()==0));
}
publicActionErrorsvalidate(ActionMappingmapping,
HttpServletRequestrequest){
ActionErrorserrors=newActionErrors();
if(nullOrBlank((String)this.get("lastName"))){
errors.add("lastName",
newActionError("article1.lastName.missing"));
}
if(nullOrBlank((String)this.get("firstName"))){
errors.add("firstName",
newActionError("article1.firstName.missing"));
}
if(nullOrBlank((String)this.get("street"))){
errors.add("street",
newActionError("article1.street.missing"));
}
if(nullOrBlank((String)this.get("city"))){
errors.add("city",newActionError("article1.city.missing"));
}
if(nullOrBlank((String)this.get("state"))){
errors.add("state",
newActionError("article1.state.missing"));
}
if(nullOrBlank((String)this.get("postalCode"))){
errors.add("postalCode",
newActionError("article1.postalCode.missing"));
}
if(nullOrBlank((String)this.get("phone"))){
errors.add("phone",newActionError("article1.phone.missing"));
}
returnerrors;
}
}
假如是如许,我们就要变动struts-config.xml来利用DynaActionForm的子类,如许的效果仿佛是又回到了先前的模样(为每个表单写DynaActionForm),呵呵。。。
以是保举的做法是利用struts1.1种的ValidatorFramework,这方面的内容在今后的文章中在申明。
关于
JamesTurneristheownerandmanagerofBlackBearSoftware,LLC,whichspecializesincustomJava-basede-CommerceandCRMsolutionsdelivery.Heisalsotheauthorof"MySQLandJSPWebApplications:Data-DrivenProgrammingUsingTomcatandMySQL"(ISBN:0672323095)andistheco-authorof"Struts:KickStart"(ISBN:0672324725),whichwillbepublishedinNovember.Hecanbereachedatturner@blackbear.com.
JAVA是一种可以撰写跨平台应用软件的面向对象的程序设计语言,由升阳(SunMicrosystems)公司的詹姆斯·高斯林(JamesGosling)等人于1990年代初开发。 |
|