We shall replace the validate() method in this example - http://www.javaissues.com/2011/03/struts-1x-reset-and-validate-methods-in.html with the Validator Framework
In addition to the Struts 1.3.8 jars and the Apache Commons jars, also add the Jakarta-Oro jar into the lib folder of your application. Download the oro jar from here
1) Change the LoginForm.java file to make the LoginForm class extend from the ValidatorForm class instead of the ActionForm class. Also remove the validate() method from the LoginForm class.
2) Modify the struts-config.xml file to add the <plug-in> element,
3) Add a new validation.xml file to the WEB-INF folder of the web application, as below. This configuration will validate the username and password fields for required field validation and also for the form input to comply/match the regular expressions.
4) In the message resources bundle, the ApplicationResources.properties file, add the following messages,
5) Open the following URL in the browser and test for required field validation as well as form field input validation that complies with the regular expressions in the validation.xml
In addition to the Struts 1.3.8 jars and the Apache Commons jars, also add the Jakarta-Oro jar into the lib folder of your application. Download the oro jar from here
1) Change the LoginForm.java file to make the LoginForm class extend from the ValidatorForm class instead of the ActionForm class. Also remove the validate() method from the LoginForm class.
public class LoginForm extends ValidatorForm {
...
}
2) Modify the struts-config.xml file to add the <plug-in> element,
<struts-config>
...
<message-resources parameter="ApplicationResources" />
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/org/apache/struts/validator/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
</struts-config>
3) Add a new validation.xml file to the WEB-INF folder of the web application, as below. This configuration will validate the username and password fields for required field validation and also for the form input to comply/match the regular expressions.
<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.3.0//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_3_0.dtd">
<form-validation>
<global>
<constant>
<constant-name>id</constant-name>
<constant-value>^([a-zA-Z_]{1}[a-zA-Z0-9_-]{7,14})$</constant-value>
</constant>
<constant>
<constant-name>pass</constant-name>
<constant-value>^([a-zA-Z0-9_-]{8,15})$</constant-value>
</constant>
</global>
<formset>
<form name="loginForm">
<field property="username" depends="required,mask">
<arg position="0" key="username.required"/>
<var>
<var-name>mask</var-name>
<var-value>${id}</var-value>
</var>
</field>
<field property="password" depends="required,mask">
<arg position="0" key="password.required"/>
<msg name="mask" key="password.maskmsg" />
<var>
<var-name>mask</var-name>
<var-value>${pass}</var-value>
</var>
</field>
</form>
</formset>
</form-validation>
4) In the message resources bundle, the ApplicationResources.properties file, add the following messages,
# Struts Validator Error Messages
errors.required={0} is required.
errors.minlength={0} can not be less than {1} characters.
errors.maxlength={0} can not be greater than {1} characters.
errors.invalid={0} is invalid.
errors.byte={0} must be a byte.
errors.short={0} must be a short.
errors.integer={0} must be an integer.
errors.long={0} must be a long.
errors.float={0} must be a float.
errors.double={0} must be a double.
errors.date={0} is not a date.
errors.range={0} is not in the range {1} through {2}.
errors.creditcard={0} is an invalid credit card number.
errors.email={0} is an invalid e-mail address.
username.required=Username
password.required=Password
password.maskmsg=Password should be of length between 8 and 15 characters
5) Open the following URL in the browser and test for required field validation as well as form field input validation that complies with the regular expressions in the validation.xml
http://localhost:8080/WebAppName/login.jsp
No comments:
Post a Comment