Client Side Validation using Validator Framework Work in Struts

How does client side validation using validator framework work in
struts?


There are two configuration files used. One if called validator-rules.xml and the other is validation.xml. This is example of client side validation
Step 1:Java Script message.
In the JSP page : empform.jsp

<html:form action="/EmpSaveAaction" method="post" onsubmit="return
validateEmpForm(this);">
<html:text property="firstName" size="30" maxlength="30"/>
<html:text property="lastName" size="30" maxlength="30"/>
<html:submit>Save</html:submit>
<!-- Begin Validator Javascript Function-->
<html:javascript formName="empForm"/>
<!-- End of Validator Javascript Function-->
</html:form>

You have to add in JSP for activate client side validation .
Step 2: Add action mapping in struts-config.xml

<action
path="/EmpSaveAaction"
type="empForm"
name="AddressForm"
scope="request"
validate="true"
input="/empform.jsp">
<forward name="success" path="/success.jsp"/>
</action>
and add the form bean inside <form-beans> </form-beans> tag
<form-beans>
<form-bean name="empForm" type="com.techfaq.form.EmpForm" /> </form-beans>

Step 3: Validator-rules.xml:

The validator-rules.xml file defines the Validator definitions available for a given application. The validator-rules.xml file acts as a template, defining all of the possible Validators that are available to an application. Example validator-rules.xml File :

<form-validation>
<global>
<validator
name="required"
classname="org.apache.struts.util.StrutsValidator"
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"/>
</global>
</form-validation>

Step 4: validation.xml File :
The validation.xml file is where you couple the individual Validators defined in the validator-rules.xml to components within your application.
validation.xml File:

<form-validation>
<formset>
<form name="empForm">
<field
property="firstName"
depends="required">
<arg key="label.firstName"/>
</field>
<field
property="lastName"
depends="required">
<arg key="label.lastName"/>
</field>
</form>
</formset>
</form-validation>

In the empForm firstName and lastName are the required filed.
So in the above configuration you can see we add for both firstName and lastName. You can see depends="required" - "required" property is defind in validator-rules.xml.
In the resource bundle : application_resource.propertis file

label.firstName=First Name
label.lastName=Last Name
#Error messages used by the Validator
errors.required={0} is required.

Based on the validation.xml File configuration .
Error in jsp will be : Java Script message.
First Name is required.
Last Name is required. {0} will be filled by (First Name or Last Name) because validation.xml above configuration you have defind .


Related Topic Struts FAQs
Struts Validator Framework Work
Enable Front End Client Side Validation Based on the xml in validation.xml?
What is Struts Validator Framework?
Struts 1 vs Struts2 Difference Performance

nScraps.com 2011   Privacy Policy  Terms of Service  Feedback