Archive for April, 2009

Struts-Validator FrameWork

Sunday, April 5th, 2009
In Struts we have two kinds of validation a). Server Side Validation b). Client Side Validation.
The server side validation can be done in two ways. One can use ActionForm to validate. The second method is make use of XML based validation. The XML based validation will happen based on validation.xml.In the form based validation the validation happens in validate method of ActionForm.
ActionErrors class is used to hold the Error messages and used to display it on View.
Adding Validator Framework Support
		<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
			<set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
		</plug-in>
		
Using validation.xml
The above configuration adds validator support in the struts.The validation.xml file is used to declare sets of validations that should be applied to Form Beans. Each Form Bean that you want to validate has its own definition in this file. Inside that definition you specify the validations that you want to apply to the Form Bean’s fields. Following is a sample validation.xml file that illustrates how validations are defined:
			<form-validation>
				<formset>
					<!-- validation mappings -->
					<form name="exampleXMLForm">
						<field property="name" depends="required, minlength">
							<arg key="exampleXMLForm.name" />
							<arg key="${var:minlength}" resource="false" />
							<var>
								<var-name>minlength</var-name>
								<var-value>3</var-value>
							</var>
						</field>
						<field property="age" depends="required, integer, validwhen">
							<arg key="exampleXMLForm.age"/>
							<arg name="validwhen" key="${var:min}" resource="false" />
							<var>
								<var-name>test</var-name>
								<var-value>(*this* > 18)</var-value>
							</var>
						</field>
					</form>
				</formset>
			</form-validation>
The above xml defines the fileds Name and age validations. This is an XML based validation. The peiece of code defines the actionmapping and form bean definition in struts-config.xml
			<form-beans>
				<form-bean name="exampleForm" type="com.techmaddy.tmvalidator.ExampleForm" />
				<form-bean name="exampleXMLForm" type="com.techmaddy.tmvalidator.ExampleXMLForm" />
			</form-beans>
			<action-mappings>
				<action attribute="exampleForm" input="/example.jsp" name="exampleForm" path="/example" scope="request"			type="com.techmaddy.tmvalidator.ExampleAction" validate="true">
					<forward name="example" path="/example.jsp" />
				</action>
				<action attribute="exampleXMLForm" input="/exampleXML.jsp"			name="exampleXMLForm" path="/exampleXML" scope="request"			type="com.techmaddy.tmvalidator.ExampleXMLAction" validate="true">
					<forward name="example" path="/exampleXML.jsp" />
				</action>
			</action-mappings>
Using ActionForm

This Class is used to validate the the user input form and sets the error message in ActionErrors.


			package com.techmaddy.tmvalidator;

			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.action.ActionMessage;

			/** * * @author Kumarasamy */

			public class ExampleForm extends ActionForm {

				private String name;

				private Integer age;

				public String getName() {
					return name;
				}

				public void setName(String name) {
					this.name = name;
				}

				public Integer getAge() {
					return age;
				}

				public void setAge(Integer age) {
					this.age = age;
				}

				public ActionErrors validate(ActionMapping mapping,HttpServletRequest request) {
					ActionErrors actionErrors = new ActionErrors();

					if (name.length() < 3) {
						actionErrors.add("name", new ActionMessage("error.name"));
					}

					if (age == null || age < 18) {
						actionErrors.add("age", new ActionMessage("error.age"));
					}// return collection of action messages

					return actionErrors;
				}

				//Reset the values
					public void reset(ActionMapping mapping, HttpServletRequest request) {
						setName("");
						setAge(0);
					}
				}
				
Download
To download the WAR file which Contains the example for Struts Validator FrameWork Click Here
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
Get Adobe Flash player