It is difficult to handle validator framework, if you are new Very Happy to struts. I have also spent lot to time to learn it, so now finally I done it. I will give you step by step of using dyna form validation. It is simple server side dynaform action validation or and client side validation with source code.
1. Open struts-config.xml file and do entries
2. Make validation.xml file in WEB-INF folder
3. Create validator-rules.xml in WEB-INF folder.
4. Create action class according to your requirement.
5. Create ApplicationResources.properties file in your package.
6. Last finally create a jsp file where you want to show your errors.
This example using struts 1.2 jars. So do little care of DTD tld standards. And version of it struts 1.1 use different and struts 1.3 use different one.
1. create struts-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"> <struts-config> <data-sources /> <form-beans > <form-bean name="DynaFormTest" type="org.apache.struts.validator.DynaValidatorForm"> <form-property name="name" type="java.lang.String" /> <form-property name="phone" type="java.lang.String" /> </form-bean> </form-beans> <global-exceptions /> <global-forwards /> <action-mappings > <action attribute="DynaFormTest" input="input" name="DynaFormTest" path="/ValidateTest" scope="request" validate="true" type="com.sumit.struts.TestAction" > <forward name="input" path="/index.jsp" /> <forward name="back" path="index.jsp" /> </action> </action-mappings> <controller inputForward="true" /> <message-resources parameter="com.sumit.struts.ApplicationResources" /> <plug-in className="org.apache.struts.validator.ValidatorPlugIn"> <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" /> <set-property property="stopOnFirstError" value="true" /> </plug-in> </struts-config>
2. In this step make validation.xml file in WEB-INF
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0//EN" "validator_1_0.dtd" > <form-validation> <formset> <form name="DynaFormTest"> <field property="name" depends="required"> <msg name="required" key="error.name"/> </field> <field property="phone" depends="required,integer"> <msg name="required" key="error.phone"/> <msg name="integer" key="error.phone.integer"/> </field> </form> </formset> </form-validation>
3. Copy or create validator-rules.xml in WEB-INF, but this file create automatically when we true validate=”true” in action mapping tag in struts-config.xml
4. Create an action class according to your requirement.
But still I am giving a code as you can use it with using subclass of dynaform. I am using a package path com.sumit.struts
package com.valid.struts; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.validator.DynaValidatorActionForm; import org.apache.struts.validator.DynaValidatorForm; import org.apache.struts.actions.ForwardAction; public class TestAction extends DynaValidatorActionForm { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { DynaValidatorActionForm DynaFormTest = (DynaValidatorActionForm) form; DynaFormTest.get("name"); DynaFormTest.get("phone"); return mapping.findForward("back"); } }
5. Create ApplicationResources.properties file for internationalize errors
This is also in com.sumit.struts package
error.name= Name should not empty error.phone= Phone should not empty error.phone.integer=Phone should be integer
6. This is final and last step to use dynaform validation
This is server side validations
<%@ page language="java" pageEncoding="ISO-8859-1"%> <%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%> <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%> <html> <head> <title>JSP for DynaValidatorActionForm form</title> </head> <body> <html:form action="/ValidateTest"> name : <html:text property="name" /><html:errors property="name" /><br/> phone : <html:text property="phone"/><html:errors property="phone" /><br/> <html:submit/><html:cancel/> </html:form> </body> </html>
If you want to do javascript client side validation. Then change this jsp page with this one
<%@ page language="java" pageEncoding="ISO-8859-1"%> <%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%> <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%> <html> <head> <title>DynaValidatorActionForm form</title> <html:javascript formName="DynaFormTest" dynamicJavascript="true"/> </head> <body> <html:form action="/ValidateTest" onsubmit="return validateDynaFormTest(this)"> name : <html:text property="name" /><br/> phone : <html:text property="phone"/><br/> <html:submit/><html:cancel/> </html:form> </body> </html>




Link to Us