A
andachong
Unregistered / Unconfirmed
GUEST, unregistred user!
各位老大,帮我解决一下关于struts自定义validator的问题
自定义validator出错!!
那位老大可以帮小弟看看哪里错了~~
在JSP上
<html:errors/>
<html:form action="myAction.do"
focus="myForm"
>
用户名:<html:text property="name"
/><br>
EMail:<html:text property="mail"
/><br>
生日:<html:text property="birthday"
/><br>
密码:<html:text property="password"
/><br>
验证密码:<html:text property="password2"
/><br>
<html:submit>
<bean:message key="btn.save"/>
</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("secondProperty"
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="twofields"
classname="validatortest.MyValidator"
method="validateTwofields"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
javax.servlet.http.HttpServletRequest"
depends="required"
msg="errors.twofields"
/>
validation.xml中加
<formset>
<form name="myForm">
<field property="name"
depends="required,minlength,maxlength">
<arg0 key="name"
resource="false"/>
<arg1 key="minLength"
name="${var:minlength}"
resource="false"/>
<arg1 key="maxLength"
name="${var:maxlength}"
resource="false"/>
<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="mail"
depends="required,email">
<arg0 key="mail"
resource="false"/>
</field>
<field property="birthday"
depends="required,date">
<arg0 key="birthday"
resource="false"/>
</field>
<field property="password"
depends="required,twofields">
<arg0 key="password"
resource="false"/>
<var>
<var-name>secondProperty</var-name>
<var-value>password2</var-value>
</var>
</field>
<field property="password"
depends="required,twofields">
<arg0 key="password"
resource="false"/>
<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)
自定义validator出错!!
那位老大可以帮小弟看看哪里错了~~
在JSP上
<html:errors/>
<html:form action="myAction.do"
focus="myForm"
>
用户名:<html:text property="name"
/><br>
EMail:<html:text property="mail"
/><br>
生日:<html:text property="birthday"
/><br>
密码:<html:text property="password"
/><br>
验证密码:<html:text property="password2"
/><br>
<html:submit>
<bean:message key="btn.save"/>
</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("secondProperty"
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="twofields"
classname="validatortest.MyValidator"
method="validateTwofields"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
javax.servlet.http.HttpServletRequest"
depends="required"
msg="errors.twofields"
/>
validation.xml中加
<formset>
<form name="myForm">
<field property="name"
depends="required,minlength,maxlength">
<arg0 key="name"
resource="false"/>
<arg1 key="minLength"
name="${var:minlength}"
resource="false"/>
<arg1 key="maxLength"
name="${var:maxlength}"
resource="false"/>
<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="mail"
depends="required,email">
<arg0 key="mail"
resource="false"/>
</field>
<field property="birthday"
depends="required,date">
<arg0 key="birthday"
resource="false"/>
</field>
<field property="password"
depends="required,twofields">
<arg0 key="password"
resource="false"/>
<var>
<var-name>secondProperty</var-name>
<var-value>password2</var-value>
</var>
</field>
<field property="password"
depends="required,twofields">
<arg0 key="password"
resource="false"/>
<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)