Struts

adplus-dvertising
Application- 14 : Creating a Struts Application by using DynaValidationForm
Previous Home Next

In this chapter, you create a new application and provide validation in it from validation.xml file. DynaValidationForm class extends DynaActionForm and provides basic field validation based on an XML( validation.xml and validator- rules.xml) files. In which, the key passed into the validate is the action element's 'name' attribute from the in struts-config.xml which should match the form element's name attribute in the validation.xml.

Validator-rules.xml file contained in struts-core.jar, and should be referenced in the struts-config.xml under the plug-in element for the ValidatorPlugIn. Important point is that NetBean IDE automatic configure the Validatior and Tiles and also plugin these resource into struts-config.xml file. Once you have configured the Validator Plug-In, so that it can load your Validator Resources you just have to extend org.apache.struts.validator.action.ValidatorForm instead of org.apache.struts.action.ActionForm. Then when the Validate method is called, the action's name attribute from the struts-config.xml is used to load the validations for the current form. So the form element's name attribute in the validator-rules.xml should match action element's name attribute.

For each piece of the application, we show you the code and then explain the code. This application design in NetBeans IDE- 6.8 with Struts 1.3

Requirement for your struts application-

  • At least one view component is required in every Application.
  • A subclass of ActionForm is used to submit the date as application needed.
  • A subclass of Action class which provide specific processing functionality for the application.
  • A web.xml file is required for calling ActionServlet and struts-config.xml.
  • A struts-config.xml file is required for updating and mapping the update.

Let's Start the application, this application provide a good understanding how struts framework works and also understanding the development of Struts applications.

Now, Following the some steps for develop your first application name as ValidationEx_1. ValidationEx_1 application contain following page-

  • Three View file name as index.jsp and sucess.jsp
  • One DynaValidatorForm class name as a registerForm, used for store data as application need.
  • One Action class name as registerAction.java, used for provide specific processing functionality.
  • Two XML file name as web.xml and struts-config.xml file, used for provide control, mapping and validation in application.

Step- 1 : Start your IDE ( NetBeans 6.8 with struts 1.3).

Step- 2 : Open a new project( Crtl+Shift+N), name it , add server( Apache Tomcat 6.0.20 ), then add struts framework 1.3 , and don't forget to add Struts TLDs.

When you click Finish button, your NetBeans IDE configure everything in place and project open in your IDE.

Step- 3: Open index.jsp page, from your struts application and code it as describe below.

<%-- Document: index.jsp
Description : First Page of ValidationEx_1 Application--%>
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" 
prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" 
prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" 
prefix="logic" %>
<html:html lang="true">
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<title><bean:message key="welcome.title"/></title>
<html:base/>
</head>
<body style="background-color: white">
<html:form action="/register" >

<h3><bean:message key="welcome.heading"/></h3>
<%-- ======= Add error into JSP page ======= --%>
<html:errors />
<ul>
<table border="0" cellspacing="4" cellpadding="4">
<tbody>
<tr>
<td><bean:message key="welcome.name" /></td>
<td><html:text property="name" size="25" /></td>
</tr>
<tr>
<td><bean:message key="welcome.number" /></td>
<td><html:text property="number" size="25" /></td>
</tr>
<tr>
<td><bean:message key="welcome.age" /></td>
<td><html:text property="age" size="25" /></td>
</tr>
<tr>
<td><bean:message key="welcome.email" /></td>
<td><html:text property="email" size="25" /></td>
</tr>
<tr>
<td><bean:message key="welcome.gender"/></td>
<td><html:radio property="gender" value="male" /> Male
<html:radio property="gender" value="female" />
Female </td>
</tr>
<tr>
<td align="center"><html:submit /></td>
<td align="center"><html:reset /></td>
</tr>
</tbody>
</table>
</ul>
</html:form>
</body>
</html:html>

Step- 4: Open sucess.jsp page, from your struts application and code it as describe below.

<%-- Document: sucess.jsp
 Description : Second Page of ValidationEx_1 Application--%>
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" 
prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html"
prefix="html" %>
<html:html lang="true">
<head>
<meta http-equiv="Content-Type" 
content="text/html; charset=UTF-8">
<title><bean:message key="welcome.title"/></title>
</head>
<body style="background-color: white">
<h1>Congratulations ! your Detail successfully submit</h1>
<h2> Check your detail</h2>
<ul>
<table border="1" cellspacing="4" cellpadding="4" 
style="border-bottom-style: groove; border-color: blue">
<tbody>
<tr>
<td> Name: </td>
<td><b><bean:write name="registerForm" 
property="name" /></b></td>
</tr>
<tr>
<td> Phone/Mobile No: </td>
<td><b><bean:write name="registerForm" 
property="number" /></b></td>
</tr>
<tr>
<td> Age: </td>
<td><b><bean:write name="registerForm"
property="age" /></b></td>
</tr>
<tr>
<td> E-mail Id: </td>
<td><b><bean:write name="registerForm"
property="email" /></b></td>
</tr>
<tr>
<td> Gender: </td>
<td><b><bean:write name="registerForm"
property="gender" /></b></td>
</tr>
</tbody>
</table>
</ul>
</body>
</html:html>

Step- 5: Open web.xml file place in WEB-INF folder, this file is used to map the ActionServlet( work as backbone of application, every Action, Validation and ActionForm class control by this class) , struts-config.xml file, welcome file and five different TagLibrary.

<!-- Save as a web.xml-->
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<!-- ActionServlet map here, however you can't find
this class within the project but it work as a backbone 
of the project -->
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet
</servlet-class>
<!-- Your struts-config.xml file map here-->
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml
</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<!-- Action class map with URL -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<!-- Welcome file map here: -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<jsp-config>
<!-- All the Tag Library which used with 
in the project -->
<taglib>
<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld
</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld
</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld
</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-nested.tld
</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld
</taglib-location>
</taglib>
</jsp-config>
</web-app>

Step- 6: Open struts-config.xml file place in WEB-INF folder, this file is used to map Model class, Exception class, Validation class, Action class( Action class further choose View class) and ApplicationResource class.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//
DTD Struts Configuration 1.3//EN"
"http://jakarta.apache.org/struts
/dtds/struts-config_1_3.dtd">
<struts-config>
<!-- Create a DynaValidatorForm and pass the property in it -->
<form-beans>
<form-bean name="registerForm" 
type="org.apache.struts.validator.DynaValidatorForm" >
<form-property name="name" type="java.lang.String" />
<form-property name="age" type="java.lang.Integer" />
<form-property name="email" type="java.lang.String" />
<form-property name="number" type="java.lang.Integer" />
<form-property name="gender" type="java.lang.String" />
</form-bean>
</form-beans>
<global-exceptions>
</global-exceptions>
<global-forwards>
</global-forwards>
<action-mappings>
<action input="/index.jsp" name="registerForm"
path="/register" scope="request" 
type="r4r.struts.registerAction">
<forward name="success" path="/sucess.jsp"/>
<forward name="failure" path="/index.jsp"/>
</action>
</action-mappings>
<controller processorClass="org.apache.struts
.tiles.TilesRequestProcessor"/>
<message-resources parameter="com/myapp/struts
/ApplicationResource"/>
<!-- =========== Tiles plugin =========-->
<plug-in className="org.apache.struts
.tiles.TilesPlugin" >
<set-property property="definitions-config" 
value="/WEB-INF/tiles-defs.xml" />
<set-property property="moduleAware" value="true" />
</plug-in>

<!-- ======== Validator plugin ========== -->
<plug-in className="org.apache.struts
.validator.ValidatorPlugIn">
   <!-- Validator plugin utilize two validator.xml 
   and validator-rule.xml files validator-rules.xml
   file define error rule while validator.xml set up error-->
<set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
</struts-config>

Step- 7: Open validation.xml file place into WEB-INF folder, this file used to provide the validation as well as error in JSP page of application, this( validation.xml) file call by struts-cofig.xml file as we define earlier.

<?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>
<!-- This is a minimal Validator form file with a 
couple of examples.-->
<global>
 <!-- An example global constant
<constant>
<constant-name>postalCode</constant-name>
<constant-value>^\d{5}\d*$</constant-value>
</constant>
end example-->
</global>
 <!--  -->
<!-- add validation into index.jsp page o
f ValidationEx_1 application -->
<formset>
<!-- Add validation into form-->
<form name="registerForm" >
<!-- add validation into each field present
into registerForm-->
<field property="name" depends="required" >
<arg key="registerForm.name" />
</field>
<field property="number" depends="required, minlength" >
<arg0 key="registerForm.number" />
<arg1 key="${var:minlength}" name="minlength" 
resource="false" />
<var>
<var-name>minlength</var-name>
<var-value>10</var-value>
</var>
</field>
<field property="age" depends="required, intRange" >
<arg0 key="registerForm.age" />
<arg1 key="${var:min}" name="intRange" 
resource="false" />
<arg2 key="${var:max}" name="intRange" 
resource="false"/>
<var>
<var-name>min</var-name>
<var-value>15</var-value>
</var>
<var>
<var-name>max</var-name>
<var-value>65</var-value>
</var>
</field>
<field property="email" depends="required,email" >
<arg0 key="registerForm.email" />
<arg1 key="$(var:email)" name="email" resource="false" />
<var>
<var-name>email</var-name>
<var-value>^([_A-Za-z0-9-]*@([A-Za-z0-9-])+((\\.com)|
(\\.net))$)</var-value>
</var>
</field>
<field property="gender" depends="required" >
<arg key="registerForm.gender" />
</field>
</form>
</formset>
</form-validation>

Step- 8: Open validator-rules.xml file place into WEB-INF folder, file used to define the rule of validation set up by validation.xml file and error mapping into ApplicationResource.properties file.This( validator-rules.xml) file is also define for the custom error message.

<!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">
<!-- $Id: validator-rules.xml 481833 2006-12-03 17:32:52Z
niallp $ This file contains the default 
Struts Validator pluggable validator definitions.  
It is contained in struts-core.jar, and should be referenced
in the struts-config.xml under the plug-in element
   for the ValidatorPlugIn.
<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>
   These are the default error messages associated with
   each validator defined in this file.  They should be
   added to your projects ApplicationResources.properties
   file or you can associate new ones by modifying the
   pluggable validators msg attributes in this file.
   # 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.
   Note: Starting in Struts 1.2.0 the default 
   javascript definitions have been consolidated to 
   commons-validator. The default can be overridden
   by supplying a <javascript> element with a CDATA 
   section, just as in struts 1.1.-->
   <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.ActionMessages,
     org.apache.commons.validator.Validator,
     javax.servlet.http.HttpServletRequest"
     msg="errors.required"/>
 <validator name="requiredif"
     classname="org.apache.struts.validator.FieldChecks"
     method="validateRequiredIf"
     methodParams="java.lang.Object,
     org.apache.commons.validator.ValidatorAction,
     org.apache.commons.validator.Field,
     org.apache.struts.action.ActionMessages,
     org.apache.commons.validator.Validator,
     javax.servlet.http.HttpServletRequest"
     msg="errors.required"/>
 <validator name="validwhen"
     msg="errors.required"
     classname="org.apache.struts.validator.validwhen.ValidWhen"
     method="validateValidWhen"
     methodParams="java.lang.Object,
     org.apache.commons.validator.ValidatorAction,
     org.apache.commons.validator.Field,
     org.apache.struts.action.ActionMessages,
     org.apache.commons.validator.Validator,
     javax.servlet.http.HttpServletRequest"/>
 <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.ActionMessages,
     org.apache.commons.validator.Validator,
     javax.servlet.http.HttpServletRequest"
     depends=""
     msg="errors.minlength"
     jsFunction="org.apache.commons.validator
	 .javascript.validateMinLength"/>
 <validator name="maxlength"
     classname="org.apache.struts.validator.FieldChecks"
     method="validateMaxLength"
     methodParams="java.lang.Object,
     org.apache.commons.validator.ValidatorAction,
     org.apache.commons.validator.Field,
     org.apache.struts.action.ActionMessages,
     org.apache.commons.validator.Validator,
     javax.servlet.http.HttpServletRequest"
     depends=""
     msg="errors.maxlength"
     jsFunction="org.apache.commons.validator
	 .javascript.validateMaxLength"/>
 <validator name="mask"
     classname="org.apache.struts.validator.FieldChecks"
     method="validateMask"
     methodParams="java.lang.Object,
     org.apache.commons.validator.ValidatorAction,
     org.apache.commons.validator.Field,
     org.apache.struts.action.ActionMessages,
     org.apache.commons.validator.Validator,
     javax.servlet.http.HttpServletRequest"
     depends=""
     msg="errors.invalid"/>
 <validator name="byte"
     classname="org.apache.struts.validator.FieldChecks"
     method="validateByte"
     methodParams="java.lang.Object,
     org.apache.commons.validator.ValidatorAction,
     org.apache.commons.validator.Field,
     org.apache.struts.action.ActionMessages,
     org.apache.commons.validator.Validator,
     javax.servlet.http.HttpServletRequest"
     depends=""
     msg="errors.byte"
     jsFunctionName="ByteValidations"/>
 <validator name="short"
     classname="org.apache.struts.validator.FieldChecks"
     method="validateShort"
     methodParams="java.lang.Object,
     org.apache.commons.validator.ValidatorAction,
     org.apache.commons.validator.Field,
     org.apache.struts.action.ActionMessages,
     org.apache.commons.validator.Validator,
     javax.servlet.http.HttpServletRequest"
     depends=""
     msg="errors.short"
     jsFunctionName="ShortValidations"/>
 <validator name="integer"
     classname="org.apache.struts.validator.FieldChecks"
     method="validateInteger"
     methodParams="java.lang.Object,
     org.apache.commons.validator.ValidatorAction,
     org.apache.commons.validator.Field,
     org.apache.struts.action.ActionMessages,
     org.apache.commons.validator.Validator,
     javax.servlet.http.HttpServletRequest"
     depends=""
     msg="errors.integer"
     jsFunctionName="IntegerValidations"/>
 <validator name="long"
     classname="org.apache.struts.validator.FieldChecks"
     method="validateLong"
     methodParams="java.lang.Object,
     org.apache.commons.validator.ValidatorAction,
     org.apache.commons.validator.Field,
     org.apache.struts.action.ActionMessages,
     org.apache.commons.validator.Validator,
     javax.servlet.http.HttpServletRequest"
     depends=""
     msg="errors.long"/>
 <validator name="float"
     classname="org.apache.struts.validator.FieldChecks"
     method="validateFloat"
     methodParams="java.lang.Object,
     org.apache.commons.validator.ValidatorAction,
     org.apache.commons.validator.Field,
     org.apache.struts.action.ActionMessages,
     org.apache.commons.validator.Validator,
     javax.servlet.http.HttpServletRequest"
     depends=""
     msg="errors.float"
     jsFunctionName="FloatValidations"/>
 <validator name="double"
     classname="org.apache.struts.validator.FieldChecks"
     method="validateDouble"
     methodParams="java.lang.Object,
     org.apache.commons.validator.ValidatorAction,
     org.apache.commons.validator.Field,
     org.apache.struts.action.ActionMessages,
     org.apache.commons.validator.Validator,
     javax.servlet.http.HttpServletRequest"
     depends=""
     msg="errors.double"/>
 <validator name="byteLocale"
     classname="org.apache.struts.validator.FieldChecks"
     method="validateByteLocale"
     methodParams="java.lang.Object,
     org.apache.commons.validator.ValidatorAction,
     org.apache.commons.validator.Field,
     org.apache.struts.action.ActionMessages,
     org.apache.commons.validator.Validator,
     javax.servlet.http.HttpServletRequest"
     depends=""
     msg="errors.byte"/>
 <validator name="shortLocale"
     classname="org.apache.struts.validator.FieldChecks"
     method="validateShortLocale"
     methodParams="java.lang.Object,
     org.apache.commons.validator.ValidatorAction,
     org.apache.commons.validator.Field,
     org.apache.struts.action.ActionMessages,
     org.apache.commons.validator.Validator,
     javax.servlet.http.HttpServletRequest"
     depends=""
     msg="errors.short"/>
 <validator name="integerLocale"
     classname="org.apache.struts.validator.FieldChecks"
     method="validateIntegerLocale"
     methodParams="java.lang.Object,
     org.apache.commons.validator.ValidatorAction,
     org.apache.commons.validator.Field,
     org.apache.struts.action.ActionMessages,
     org.apache.commons.validator.Validator,
     javax.servlet.http.HttpServletRequest"
     depends=""
     msg="errors.integer"/>
 <validator name="longLocale"
     classname="org.apache.struts.validator.FieldChecks"
     method="validateLongLocale"
     methodParams="java.lang.Object,
     org.apache.commons.validator.ValidatorAction,
     org.apache.commons.validator.Field,
     org.apache.struts.action.ActionMessages,
     org.apache.commons.validator.Validator,
     javax.servlet.http.HttpServletRequest"
     depends=""
     msg="errors.long"/>
 <validator name="floatLocale"
     classname="org.apache.struts.validator.FieldChecks"
     method="validateFloatLocale"
     methodParams="java.lang.Object,
     org.apache.commons.validator.ValidatorAction,
     org.apache.commons.validator.Field,
     org.apache.struts.action.ActionMessages,
     org.apache.commons.validator.Validator,
     javax.servlet.http.HttpServletRequest"
     depends=""
     msg="errors.float"/>
 <validator name="doubleLocale"
     classname="org.apache.struts.validator.FieldChecks"
     method="validateDoubleLocale"
     methodParams="java.lang.Object,
     org.apache.commons.validator.ValidatorAction,
     org.apache.commons.validator.Field,
     org.apache.struts.action.ActionMessages,
     org.apache.commons.validator.Validator,
     javax.servlet.http.HttpServletRequest"
     depends=""
     msg="errors.double"/>
 <validator name="date"
     classname="org.apache.struts.validator.FieldChecks"
     method="validateDate"
     methodParams="java.lang.Object,
     org.apache.commons.validator.ValidatorAction,
     org.apache.commons.validator.Field,
     org.apache.struts.action.ActionMessages,
     org.apache.commons.validator.Validator,
     javax.servlet.http.HttpServletRequest"
     depends=""
     msg="errors.date"
     jsFunctionName="DateValidations"/>
 <validator name="intRange"
     classname="org.apache.struts.validator.FieldChecks"
     method="validateIntRange"
     methodParams="java.lang.Object,
     org.apache.commons.validator.ValidatorAction,
     org.apache.commons.validator.Field,
     org.apache.struts.action.ActionMessages,
     org.apache.commons.validator.Validator,
     javax.servlet.http.HttpServletRequest"
     depends="integer"
     msg="errors.range"/>
 <validator name="longRange"
     classname="org.apache.struts.validator.FieldChecks"
     method="validateLongRange"
     methodParams="java.lang.Object,
     org.apache.commons.validator.ValidatorAction,
     org.apache.commons.validator.Field,
     org.apache.struts.action.ActionMessages,
     org.apache.commons.validator.Validator,
     javax.servlet.http.HttpServletRequest"
     depends="long"
     msg="errors.range"/>
 <validator name="floatRange"
     classname="org.apache.struts.validator.FieldChecks"
     method="validateFloatRange"
     methodParams="java.lang.Object,
     org.apache.commons.validator.ValidatorAction,
     org.apache.commons.validator.Field,
     org.apache.struts.action.ActionMessages,
     org.apache.commons.validator.Validator,
     javax.servlet.http.HttpServletRequest"
     depends="float"
     msg="errors.range"/>
 <validator name="doubleRange"
     classname="org.apache.struts.validator.FieldChecks"
     method="validateDoubleRange"
     methodParams="java.lang.Object,
     org.apache.commons.validator.ValidatorAction,
     org.apache.commons.validator.Field,
     org.apache.struts.action.ActionMessages,
     org.apache.commons.validator.Validator,
     javax.servlet.http.HttpServletRequest"
     depends="double"
     msg="errors.range"/>
 <validator name="creditCard"
     classname="org.apache.struts.validator.FieldChecks"
     method="validateCreditCard"
     methodParams="java.lang.Object,
     org.apache.commons.validator.ValidatorAction,
     org.apache.commons.validator.Field,
     org.apache.struts.action.ActionMessages,
     org.apache.commons.validator.Validator,
     javax.servlet.http.HttpServletRequest"
     depends=""
     msg="errors.creditcard"/>
 <validator name="email"
     classname="org.apache.struts.validator.FieldChecks"
     method="validateEmail"
     methodParams="java.lang.Object,
     org.apache.commons.validator.ValidatorAction,
     org.apache.commons.validator.Field,
     org.apache.struts.action.ActionMessages,
     org.apache.commons.validator.Validator,
     javax.servlet.http.HttpServletRequest"
     depends=""
     msg="errors.email"/>
 <validator name="url"
     classname="org.apache.struts.validator.FieldChecks"
     method="validateUrl"
     methodParams="java.lang.Object,
     org.apache.commons.validator.ValidatorAction,
     org.apache.commons.validator.Field,
     org.apache.struts.action.ActionMessages,
     org.apache.commons.validator.Validator,
     javax.servlet.http.HttpServletRequest"
     depends=""
     msg="errors.url"/>
 <!--
   This simply allows struts to include the 
   validateUtilities into a page, it should
   not be used as a validation rule.-->
 <validator name="includeJavaScriptUtilities"
     classname=""
     method=""
     methodParams=""
     depends=""
     msg=""
     jsFunction="org.apache.commons.validator
	 .javascript.validateUtilities"/>
    </global>
</form-validation>

Step- 9: Now introduce Bean class into struts-config.xml file, Since In this application we used DynaValidationForm which extend DynaActionForm so, no need to make another Bean/Form class.

Step- 10: Introduce Action class in to struts-config.xml file, as we define in our first application, that Netbean IDE able to configure your action class in struts-config.xml file.

/* Save as a registerAction.java
 * Action class of ValidationEx_1 application */
package r4r.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.DynaValidatorForm;
public class registerAction extends
	org.apache.struts.action.Action {
/* forward name="" path="" */
private static final String SUCCESS = "success";
private String name, email, gender;
private int age, number;
/ * This is the action called from the Struts framework. */
@Override
public ActionForward execute(ActionMapping mapping, 
	ActionForm form,HttpServletRequest request, 
	HttpServletResponse response)throws Exception {
DynaValidatorForm validatorForm = (DynaValidatorForm) form;
name = (String) validatorForm.get("name");
email = (String) validatorForm.get("email");
gender = (String) validatorForm.get("gender");
age = (Integer) validatorForm.get("age");
number = (Integer) validatorForm.get("number");
return mapping.findForward(SUCCESS);
}
}

Run your application:

Now your application is complete, so now start run your application. Start your server( which you embedded when configure our struts application), then right click your application and click upon Run option or open your welcome( index.jsp) page and press shift+F6.

Check validation into JSP page

Previous Home Next