package net.viralpatel.struts.validation.form;
import org.apache.struts.validator.ValidatorForm;
public class CustomerForm extends ValidatorForm {
private String name;
private String telephone;
private String email;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Code language: Java (java) We will use this validator plugin to validate this form. Note that the form bean is extended from class ValidatorForm and not ActionForm as we generally do in Struts project. <!-- 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>
Code language: HTML, XML (xml) This definition tells Struts to load and initialize the Validator plug-in for your application. Upon initialization, the plug-in loads the comma-delimited list of Validator config files specified by the pathnames property. Each config file’s path should be specified by use of a Web application-relative path, as shown in the previous example. <?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>
<global>
<constant>
<constant-name>telephoneFormat</constant-name>
<constant-value>^\d{5,10}$</constant-value>
</constant>
</global>
<formset>
<form name="CustomerForm">
<field property="name" depends="required">
<arg key="label.name" />
</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>1</var-value>
</var>
<var>
<var-name>max</var-name>
<var-value>125</var-value>
</var>
</field>
<field property="telephone" depends="required, mask">
<arg key="label.telephone" />
<arg1 key="label.telephone" />
<var>
<var-name>mask</var-name>
<var-value>${telephoneFormat}</var-value>
</var>
</field>
<field property="email" depends="email">
<arg0 key="label.email" />
<arg1 key="label.email" />
</field>
</form>
</formset>
</form-validation>
Code language: HTML, XML (xml) In the above xml file, we have defined the rules for form validation. Note that we are validating form CustomerForm and the fields being validated are name, age, telephone and email. <field> tag defines the validation for a property of form. We can specify different rules like required, integer, email, intRange, mask etc in depends attribute of field tag.. Also you can define constants that can be reused in the validation xml using global constants tag. <form-beans>
<form-bean name="CustomerForm"
type="net.viralpatel.struts.validation.form.CustomerForm" />
</form-beans>
...
...
...
<action-mappings>
...
<action path="/customer" name="CustomerForm" validate="true"
input="/index.jsp"
type="net.viralpatel.struts.validation.action.CustomerAction">
<forward name="success" path="/Customer.jsp" />
<forward name="failure" path="/index.jsp" />
</action>
...
</action-mappings>
Code language: HTML, XML (xml) label.name= Name
label.email= Email
label.telephone= Telephone
label.age= Age
# general error msgs
errors.header=<font size="2"><UL>
errors.prefix=<LI><span style="color: red">
errors.suffix=</span></LI>
errors.footer=</UL></font>
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.
Code language: HTML, XML (xml) index.jsp and copy following content in it. <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>
<title>Struts Validation Framework example.</title>
</head>
<body>
<html:errors />
<html:javascript formName="CustomerForm" />
<html:form action="/customer">
<bean:message key="label.name" />
<html:text property="name"></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.telephone" />
<html:text property="telephone"></html:text>
<br />
<html:submit value="Submit"></html:submit>
</html:form>
</body>
</html>
Code language: HTML, XML (xml) Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
please provide struts 1.2 server side and client side validation
Hi I read about this CustomerAction but I don't see any codes for that. Can anyone enlighten me? Thanks
Hi JJ,
CustomerAction class file is a normal implementation of Action class that does not have anything special in it. The error handling is done using Validation framework. CustomerAction class will not be invoked in case of any validation errors.
Simple & to the point.
Good article for the beginners.
Very good I leaned jar file creation in one shot
Hi All,
actually when i run this application , i got this exception
java.lang.NoClassDefFoundError: org/apache/commons/validator/ValidatorResources
i loaded all necessary jar file but couldnot solve this problem can anyone tell me why this happening.
Thanks
Hi Neeraj,
Can you give the list of the jars that you have used in the project. Check struts-validation jar and struts.jar file. Open these jars and see if the class ValidatorResources is available. I am very sure the problem is with the jar only.
THANKS,
I was vary confuse about this .
sorry ,
in my system same this program generate different result it's print the welcome statement which is wrote in MessageResource.properties file . ?
why this msg is shown ?
now what can I do .?
good.thank u.i like very much for this site