您的位置:程序门 -> java -> j2se / 基础类



struts validator验证两个关联字段出现问题!请高手帮忙!


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


struts validator验证两个关联字段出现问题!请高手帮忙!
发表于:2007-01-05 10:11:35 楼主
jsp上有两个下拉列表框combox1对应变量trywmcsx,   combox2对应变量trzdbhsx,其中的数据是从db中取得的,但其内容不能相同!
validation.xml写法:
<form   name= "/jbbhszqt ">
<field   property= "trywmcsx "   depends= "required ">
<msg   name= "required "   key= "validate.required.err "/>
<arg0   key= "tr.ywmc "/>
</field>
<field   property= "trzdbhsx "   depends= "required ">
<msg   name= "required "   key= "validate.required.err "/>
<arg0   key= "tr.zdbh "/>
</field>
<field   property= "trywmcsx "   depends= "twofields ">
<msg   name= "twofields "   key= "validate.twofields.err "/>
<arg0   key= "tr.ywmc "/>
<arg1   key= "tr.zdbh "/>
<var>
<var-name> secondproperty </var-name>
<var-value> trzdbhsx </var-value>
</var>
</field>
</form>
validator-rules.xml写法
<validator   name= "twofields "   classname= "seerp.common.validator.validateactionform "  

method= "validatetwofields "  

methodparams= "java.lang.object,

org.apache.commons.validator.validatoraction,

org.apache.commons.validator.field,

org.apache.struts.action.actionerrors,

javax.servlet.http.httpservletrequest "  

depends= " "   msg= "validate.twofields.err ">

<javascript> <![cdata[

function   validatetwofields(form)   {
var   bvalid   =   true;
var   focusfield   =   null;
var   i   =   0;
var   fields   =   new   array();
otwofields   =   new   twofields();

for   (x   in   otwofields)   {

var   field   =   form[otwofields[x][0]];
var   secondfield   =   form[otwofields[x][2]( "secondproperty ")];

if   (field.type   ==   'text '   ¦ ¦
field.type   ==   'textarea '   ¦ ¦
field.type   ==   'select-one '   ¦ ¦
field.type   ==   'radio '   ¦ ¦
field.type   ==   'password ')   {

var   value;
var   secondvalue;

//   get   field 's   value
if   (field.type   ==   "select-one ")   {

var   si   =   field.selectedindex;
value   =   field.options[si].value;
secondvalue   =   secondfield.options[si].value;
}   else   {

value   =   field.value;
secondvalue   =   secondfield.value;
}

if   (value   ==   secondvalue)   {

if   (i   ==   0)   {

focusfield   =   field;
}

fields[i++]   =   otwofields[x][1];
bvalid   =   false;
}
}
}

if   (fields.length   >   0)   {

focusfield.focus();

alert(fields.join( '\n '));

}

return   bvalid;
}
string.prototype.lenb   =   function()   {
return   this.replace(/[^\x00-\xff]/g, "** ").length;
}]]> </javascript>

</validator>

vilidator   java文件:
/**
*   @机能名:验证相关联的两个字段
*   @param   object
*                         object
*   @param   validatoraction
*                         validatoraction
*   @param   field
*                         field
*   @param   actionerrors
*                         actionerrors
*   @param   httpservletrequest
*                         request
*   @return   boolean
*/
public   static   boolean   validatetwofields(object   bean,   validatoraction   va,
field   field,   actionerrors   errors,   httpservletrequest   request)   {

log.debug( "start ");

string   value   =   validatorutil
.getvalueasstring(bean,   field.getproperty());

string   sproperty2   =   field.getvarvalue( "secondproperty ");

string   value2   =   validatorutil.getvalueasstring(bean,   sproperty2);

if   (!genericvalidator.isblankornull(value))   {

try   {

if   (value.equals(value2))   {

errors.add(field.getkey(),   resources.getactionerror(
request,   va,   field));
}
}   catch   (exception   e)   {

e.printstacktrace();
}
}

log.debug( "end ");

return   errors.isempty();
}

jsp写法:
<html:javascript   formname= "/jbbhszqt "   method= "validatejbbhszqt "   dynamicjavascript= "true "   staticjavascript= "false "   />

1> 进行客户端验证的时候,即使内容不同,也弹出错误信息!
2> 在进行服务器端验证的时候,会出现以下错误:
2007/01/05   09:48:42.273   [error]   org.apache.catalina.core.containerbase.[catalina].[localhost].[/seerp].[action].invoke:servlet.service()   for   servlet   action   threw   exception
javax.servlet.jsp.jspexception:   failed   to   obtain   specified   collection
at   org.apache.struts.taglib.html.optionscollectiontag.dostarttag(optionscollectiontag.java:222)

请高手帮忙!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


快速检索

最新资讯
热门点击