关于struts自定义validator的问题(70分)

  • 主题发起人 主题发起人 andachong
  • 开始时间 开始时间
A

andachong

Unregistered / Unconfirmed
GUEST, unregistred user!
各位老大,帮我解决一下关于struts自定义validator的问题
自定义validator出错!!
那位老大可以帮小弟看看哪里错了~~
在JSP上
<html:errors/>
<html:form action=&quot;myAction.do&quot;
focus=&quot;myForm&quot;
>
用户名:<html:text property=&quot;name&quot;
/><br>
EMail:<html:text property=&quot;mail&quot;
/><br>
生日:<html:text property=&quot;birthday&quot;
/><br>
密码:<html:text property=&quot;password&quot;
/><br>
验证密码:<html:text property=&quot;password2&quot;
/><br>
<html:submit>
<bean:message key=&quot;btn.save&quot;/>
</html:submit>
</html:form>
以下是ActionForm
public class MyForm extends ValidatorForm {
private String name;
private String mail;
private String birthday;
private String password;
private String password2;
......
}
自定义validator
public class MyValidator implements Serializable {
public static boolean validateTwofields(Object bean,
ValidatorAction va, Field field,
ActionMessages errors,
HttpServletRequest request) {
String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
String sProperty2 = field.getVarValue(&quot;secondProperty&quot;);
String value2 = ValidatorUtils.getValueAsString(bean, sProperty2);
if (!GenericValidator.isBlankOrNull(value) &&
!GenericValidator.isBlankOrNull(value2)) {
try {
if (!value.equals(value2)) {
errors.add(field.getKey(),
Resources.getActionMessage(request, va, field));
}
} catch (Exception e) {
errors.add(field.getKey(),
Resources.getActionMessage(request, va, field));
}
} else
{
errors.add(field.getKey(),
Resources.getActionMessage(request, va,
field));
}
return errors.isEmpty();
}
}
validator-rules.xml中加
<!--myvalidator-->
<validator name=&quot;twofields&quot;
classname=&quot;validatortest.MyValidator&quot;
method=&quot;validateTwofields&quot;
methodParams=&quot;java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
javax.servlet.http.HttpServletRequest&quot;
depends=&quot;required&quot;
msg=&quot;errors.twofields&quot;
/>
validation.xml中加
<formset>
<form name=&quot;myForm&quot;>
<field property=&quot;name&quot;
depends=&quot;required,minlength,maxlength&quot;>
<arg0 key=&quot;name&quot;
resource=&quot;false&quot;/>
<arg1 key=&quot;minLength&quot;
name=&quot;${var:minlength}&quot;
resource=&quot;false&quot;/>
<arg1 key=&quot;maxLength&quot;
name=&quot;${var:maxlength}&quot;
resource=&quot;false&quot;/>
<var>
<var-name>minlength</var-name>
<var-value>4</var-value>
</var>
<var>
<var-name>maxlength</var-name>
<var-value>20</var-value>
</var>
</field>
<field property=&quot;mail&quot;
depends=&quot;required,email&quot;>
<arg0 key=&quot;mail&quot;
resource=&quot;false&quot;/>
</field>
<field property=&quot;birthday&quot;
depends=&quot;required,date&quot;>
<arg0 key=&quot;birthday&quot;
resource=&quot;false&quot;/>
</field>
<field property=&quot;password&quot;
depends=&quot;required,twofields&quot;>
<arg0 key=&quot;password&quot;
resource=&quot;false&quot;/>
<var>
<var-name>secondProperty</var-name>
<var-value>password2</var-value>
</var>
</field>
<field property=&quot;password&quot;
depends=&quot;required,twofields&quot;>
<arg0 key=&quot;password&quot;
resource=&quot;false&quot;/>
<var>
<var-name>secondProperty</var-name>
<var-value>password2</var-value>
</var>
</field>
</form>
</formset>
错误提示
org.apache.commons.validator.ValidatorException: validatortest.MyValidator
at org.apache.commons.validator.ValidatorAction.loadValidationClass(ValidatorAction.java:646)
at org.apache.commons.validator.ValidatorAction.executeValidationMethod(ValidatorAction.java:555)
at org.apache.commons.validator.Field.validateForRule(Field.java:811)
at org.apache.commons.validator.Field.validate(Field.java:890)
at org.apache.commons.validator.Form.validate(Form.java:174)
at org.apache.commons.validator.Validator.validate(Validator.java:367)
at org.apache.struts.validator.ValidatorForm.validate(ValidatorForm.java:112)
at org.apache.struts.action.RequestProcessor.processValidate(RequestProcessor.java:921)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:206)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
 
帮顶!
http://www.source520.com
站长开发推广同盟 站长朋友的终极驿站
同时拥有海量源码电子经典书籍下载
http://www.source520.com/search/search.asp
&quot;编程.站长&quot;论坛搜索引擎-----为中国站长注入动力!
 
后退
顶部