Quantcast
Channel: Javaonlineguide.net » validation
Viewing all articles
Browse latest Browse all 6

Struts Validator Framework Example- step by step guide.

$
0
0

The following steps guides the process of installing and configuring the Struts Validator component. The Struts Validator depends on various libraries to work properly. It requires the following libraries

->commons-collections.jar

->commons-beanutils.jar

->commons-logging.jar

->commons-digester.jar

The above JAR files needs to be either in Application Server (Tomcat/WebSphere) common/lib directory or in the Web application’s WEB-INF/lib directory.

For Using Validator Framework,two XML files are required.

1. validation-rules.xml

2. validator.xml.

Validation-rules.xml specifies the validation rules available. As the Validator comes with several default rules, we have to copy it into application’s WEB-INF directory. The following lines are some of the default validation rules available in the Validation-rules.xml

// Sample Validation-rules.xml


<DOCTYPE form-validation PUBLIC
 "-//Apache Software Foundation//DTD Commons 
 Validator Rules Configuration 1.1.3//EN"
 "http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">
<form-validation>

<global>

<validator name="required"
 classname="org.apache.struts.validator.FieldChecks"
 method="validateRequired"
 methodParams="java.lang.Object,
 org.apache.commons.validator.ValidatorAction,
 org.apache.commons.validator.Field,
 org.apache.struts.action.ActionErrors,
 javax.servlet.http.HttpServletRequest"
 msg="errors.required">
 
 </validator>

<validator name="minlength"
 classname="org.apache.struts.validator.FieldChecks"
 method="validateMinLength"
 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="errors.minlength">

</validator>

</global>
</form-validation> 

Default error messages associated with each validator defined in Validator-rules file should be added to ApplicationResources.properties file or you can associate new ones by modifying the pluggable validators msg attributes in this file.

ApplicationResources.properties commonly located in your applications JavaSource/props folder.

Sample ApplicationResources.properties

errors.invalid={0} is invalid.

errors.minlength={0} can not be less than {1} characters.

errors.required={0} is required.

errors.date={0} is not a date.

 

Validator plugin
**************

Now the following lines need to be added in the structs-config.xml for connecting the Validator to the application.


<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
 <set-property
 property="pathnames"
 value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
 </plug-in> 

Sample Validation.xml
*******************


<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE form-validation PUBLIC
 "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0//EN"
 "http://jakarta.apache.org/commons/dtds/validator_1_0.dtd">

<form-validation>
 
 <formset> 
 <form name="UserForm"> 
 <field property="DOB" depends="date">
 <arg0 key="prompt.date" />
 <var>
 <var-name>datePattern</var-name>
 <var-value>dd/MM/yyyy</var-value>
 </var>
 </field>
 
 <field property="userName" depends="required"> 
 <arg key="label.name" /> 
 </field> 
 
</form>
</formset> 

Example :
********

Now we are going to see how to use Validator in our application by creating a small Struts project. In our example, we will create a Employee form which will accept Employee Name, Age, Email, Mobile Number

Step 1) Create a Employee Form Jsp . The JSP is as follows

<TITLE>Employee Form.jsp</TITLE>
<body> <html:errors />
<html:javascript formName="empForm" />
<html:form action="/empEnty">
<bean:message key="label.empCode" />
<html:text property="empCode"></html:text> <br />
<bean:message key="label.empName" />
<html:text property="empName"></html:text> <br />
<bean:message key="label.age" />
<html:text property="age"></html:text> <br />
<bean:message key="label.email" />
<html:text property="email"></html:text> <br />
<bean:message key="label.mobile" />
<html:text property="mobile"></html:text> <br />
<html:submit value="Submit"></html:submit>
</html:form>
</body>

Step 2) Create a Employee Action form. The codes in the Action form are as follows

<package strutsvalidatorweb.forms;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.ValidatorForm;

public class empForm extends ValidatorForm {

private String mobile = null;
private String empCode = null;
private String email = null;
private String empName = null;
private String age = null;

public String getAge() {
return age;
}

public String getEmail() {
return email;
}

public String getEmpCode() {
return empCode;
}

public String getEmpName() {
return empName;
}

public String getMobile() {
return mobile;
}

public void setAge(String string) {
age = string;
}

public void setEmail(String string) {
email = string;
}

public void setEmpCode(String string) {
empCode = string;
}

public void setEmpName(String string) {
empName = string;
}

public void setMobile(String string) {
mobile = string;
}

} 

Step 3) Define Validations for the Action Form in Validation.xml file which is as follows.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">
<form-validation>
<formset>
<form name="empForm">
<field property="empCode" depends="required">
<arg0 key="label.empCode" />
</field>
<field property="empName" depends="required">
<arg0 key="label.empName" />
</field>
<field property="age" depends="required, integer, intRange">
<arg0 key="label.age" />
<arg1 key="${var:min}" resource="false"/>
<arg2 key="${var:max}" resource="false"/>
<var>
<var-name>min</var-name>
<var-value>18</var-value>
</var>
<var>
<var-name>max</var-name>
<var-value>60</var-value>
</var>
</field>
<field property="mobile" depends="required, mask">
<arg0 key="label.mobile" />
<arg1 key="label.mobile" />
<var>
<var-name>mask</var-name>
<var-value>^[0-9]*$</var-value>
</var>
</field>
<field property="email" depends="email">
<arg0 key="label.email" />
<arg1 key="label.email" />
</field>
</form>
</formset>
</form-validation> 

Step 4) Writing Error Messages in ApplicationResources.properties

label.empCode=Employee Code
label.empName=Employee Name
label.age=Age
label.email=Email
label.mobile=MobileNo

errors.invalid={0} is invalid.
errors.maxlength={0} can not be greater than {1} characters.
errors.minlength={0} can not be less than {1} characters.
errors.range={0} is not in the range {1} through {2}.
errors.required={0} is required.
errors.byte={0} must be an byte.
errors.date={0} is not a date.
errors.double={0} must be an double.
errors.float={0} must be an float.
errors.integer={0} must be an integer.
errors.long={0} must be an long.
errors.short={0} must be an short 

Step 5) The Entries in struts-config.xml is as follows which includes Validator Plug-in

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

<struts-config>

<!-- Data Sources -->
<data-sources>
</data-sources>

<!-- Form Beans -->
<form-beans>
<form-bean name="empForm" type="strutsvalidatorweb.forms.empForm">
</form-bean>
</form-beans>

<!-- Global Exceptions -->
<global-exceptions>
</global-exceptions>

<!-- Global Forwards -->
<global-forwards>
</global-forwards>

<!-- Action Mappings -->
<action-mappings>
<action name="empForm" path="/empEnty" scope="request" type="strutsvalidatorweb.actions.empEnty" validate="true" input="/empForm.jsp" parameter="method">
<forward name="target" path="./target.jsp">
</forward>
</action>
</action-mappings>

<!-- Message Resources -->
<message-resources parameter="props.ApplicationResources"/>

<!-- Validator Configuration -->
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
</plug-in>
</struts-config> 

Step 6) Run the Application and the output is as follows.


Viewing all articles
Browse latest Browse all 6

Trending Articles