Struts2 Validation Framework Tutorial with Example

struts2-validation-framework
Welcome to Part-3 of 7-part series of tutorials where we will go through different practical aspects of Struts2 Framework. In the last part we Created a Basic Struts2 Application from Scratch. I strongly recommend you to go through the previous articles in case you are new to Struts2.

In this article we will learn how to leverage Struts2 Validation Framework in an application. For this we will use StrutsHelloWorld application which we created in previous article as base and starts adding validation logic to it.

Introduction to Struts2 Validation Framework

Struts Action 2 relies on a validation framework provided by XWork to enable the application of input validation rules to your Actions before they are executed. Struts2 Validation Framework allows us to separate the validation logic from actual Java/JSP code, where it can be reviewed and easily modified later.

The Struts2 Validation Framework alleviates much of the headache associated with handling data validation, allowing you to focus on validation code and not on the mechanics of capturing data and redisplaying incomplete or invalid data.

Validation framework comes with set of useful routines to handle form validation automatically and it can handle both server side as well as client side form validation. If certain validation is not present, you can create your own validation logic by implementing java interface com.opensymphony.xwork2.Validator and plug it into validation framework as a re-usable component.

Validator uses XML configuration files to determine which validation routines should be installed and how they should be applied for a given application. validators.xml file contains all common validators declaration. If validators.xml file is not present in classpath, a default validation file is loaded from path com/opensymphony/xwork2/validator/validators/default.xml.

The first configuration file, validator-rules.xml, declares the validation routines that should be plugged into the framework and provides logical names for each of the validations. The validator-rules.xml file also defines client-side JavaScript code for each validation routine. Validator can be configured to send this JavaScript code to the browser so that validations are performed on the client side as well as on the server side.

Validators Scope

There are two types of Validators in Struts2 Validation Framework.

  1. Field Validators
  2. Non-field validators

Field validators, as the name indicate, act on single fields accessible through an action. A validator, in contrast, is more generic and can do validations in the full action context, involving more than one field (or even no field at all) in validation rule. Most validations can be defined on per field basis. This should be preferred over non-field validation wherever possible, as field validator messages are bound to the related field and will be presented next to the corresponding input element in the respecting view.

<validators>
  <field name="bar">
      <field-validator type="required">
          <message>You must enter a value for bar.</message>
      </field-validator>
  </field>
</validators>

Non-field validators only add action level messages. Non-field validators are mostly domain specific and therefore offer custom implementations. The most important standard non-field validator provided by XWork is ExpressionValidator.

<validators>
      <validator type="expression">
            <param name="expression">foo lt bar</param>
            <message>Foo must be greater than Bar.</message>
      </validator>
</validators>

Getting Started

Let us add validation logic to StrutsHelloWorld application that we created in previous article. For this tutorial, we will create an Action class called CustomerAction which will contain few fields. Create a file CustomerAction.java in package net.viralpatel.struts2.
customer-action-struts2
Copy following content into it.
CustomerAction.java

package net.viralpatel.struts2;

import com.opensymphony.xwork2.ActionSupport;

public class CustomerAction extends ActionSupport{
	private String name;
	private Integer age;
	private String email;
	private String telephone;

	public String addCustomer() {
		return SUCCESS;
	}

	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 String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getTelephone() {
		return telephone;
	}
	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}
}

Note that CustomerAction class has fields name, email, telephone and age. Also it has a method called addCustomer() which doesn’t have any logic, it just return SUCCESS.

Now we will add entry for this new action class in struts.xml file. Open the struts.xml file which will be present under resources folder. And add following content between <package></package> tag.

		<action name="customer"
			class="net.viralpatel.struts2.CustomerAction">
			<result name="success">SuccessCustomer.jsp</result>
			<result name="input">Customer.jsp</result>
		</action>

Note that we are mapping the CustomerAction class with name customer. Also on success user will be redirected to SuccessCustomer.jsp page. Notice that there is another result tag with name input. Whenever the validation logic encounter some validation error, it redirects the user back to page specified as input. Thus in our example, user will be redirected back to Customer.jsp in case of any errors.

Create two new JSPs Customer.jsp (which will contain Customer form) and SuccessCustomer.jsp (which will be displayed on success).
struts2-validation-jsp-files
Customer.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Customer Form - Struts2 Demo | ViralPatel.net</title>
</head>

<body>
<h2>Customer Form</h2>

<s:form action="customer.action" method="post">
	<s:textfield name="name" key="name" size="20" />
	<s:textfield name="age" key="age" size="20" />
	<s:textfield name="email" key="email" size="20" />
	<s:textfield name="telephone" key="telephone" size="20" />
	<s:submit method="addCustomer" key="label.add.customer" align="center" />
</s:form>
</body>
</html>

SuccessCustomer.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Customer Page - Struts2 Demo | ViralPatel.net</title>
</head>

<body>
	<h2>Customer Added Successfully.</h2>
</body>
</html>

We have created Customer.jsp file which will display Customer form. But we don’t have link to this page from our web application. So we will create a link to Customer.jsp from Welcome.jsp page. Open Welcome.jsp page and add following link code into it.

	<s:a href="Customer.jsp">Add Customer</s:a>

Now open the ApplicationResources.properties file from /resources folder and add following key/values in it.

name= Name
age= Age
email= Email
telephone= Telephone
label.add.customer=Add Customer

errors.invalid=${getText(fieldName)} is invalid.
errors.required=${getText(fieldName)} is required.
errors.number=${getText(fieldName)} must be a number.
errors.range=${getText(fieldName)} is not in the range ${min} and ${max}.

Execute the code in Eclipse and see the output. You will see login page. Enter username=admin and password=admin123 and do login. On welcome page you will see a link to Add Customer page. Click on that link and you will see Customer page.
struts2-customer-form

Adding Validation Logic

Now we are ready with the basic customer form on which we will add the validation logic. Following will be the validations rules:

  1. Name field is mandatory
  2. Age field is mandatory. It should be a number between 1 and 100.
  3. Email field is mandatory. It should be a valid email address.
  4. Telephone is mandatory.

In order to define validation logic for particular form, we first have to create an XML file which will hold this data. Struts2 define a specific naming convention in defining validation xml files. The format is <ActionClassName>-validation.xml. So for our application we will create a file CustomerAction-validation.xml. Note that this file should be present in the same package as of action class.
Create file CustomerAction-validation.xml in package net.viralpatel.struts2. And copy following content into it.
struts2-validation-xml
CustomerAction-validation.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
  		"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
  		"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
	<field name="name">
		<field-validator type="requiredstring">
			<param name="trim">true</param>
			<message key="errors.required" />
		</field-validator>
	</field>
	<field name="age">
		<field-validator type="required">
			<message key="errors.required" />
		</field-validator>
		<field-validator type="int">
			<param name="min">1</param>
			<param name="max">100</param>
			<message key="errors.range"/>
		</field-validator>
	</field>
	<field name="email">
		<field-validator type="requiredstring">
			<message key="errors.required" />
		</field-validator>
		<field-validator type="email">
			<message key="errors.invalid" />
		</field-validator>
	</field>
	<field name="telephone">
		<field-validator type="requiredstring">
			<message key="errors.required" />
		</field-validator>
	</field>
</validators>

And that’s it. We just added validation logic to our example. Note that the validations xml file contains different field-validators.

Client Side Validation

It is very easy to add Client Side validation or JavaScript validation to any form in Struts2. All you have to do is to add validate=”true” in form tag in your JSP file. For example open Customer.jsp and add validate=”true” in form tag. Struts2 automatically generates the JavaScript code for client side validation of form.

<s:form action="customer.action" method="post" validate="true">
	...
</s:form>

That’s All Folks

Execute the application and test the Customer form with different values.
Customer page
struts2-customer-form
Customer page with errors
customer-page-validation-errors
Customer page on success
customer-page-success

Download Source Code

Click here to download Source Code without JAR files (9KB)

Moving On

Now that we have implemented Struts2 Validation framework in our example, we know how exactly struts2 validation is handled. Also we know different types of validators like field-validators and non-field. In next part we will study Tiles framework and implement it in our application.



30 Comments

  • Niall wrote on 29 December, 2009, 1:15

    Hi there,

    Great resource many thanks. Hey one thing I notice is that if I leave the Customer form empty and submit, then I get the following exception in the log;

    Caused by: java.lang.NoSuchMethodException: net.viralpatel.struts2.CustomerAction.setAge([Ljava.lang.String;)
    at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1206)

    Does Struts2 try and set the field as a String even though the type of it is int ?

    Cheers,
    Niall

    • Viral Patel wrote on 29 December, 2009, 3:53

      Hi Niall,
      Thanks for pointing out the error. It seems there is some bug with the current version (There is JIRA ticket on this on Struts2 issue tracker – https://issues.apache.org/struts/browse/WW-2128).
      The problem is when we use generic type as attributes, ognl tries to set it as String. So here the workaround is to use wrapper classes (Int, Float) instead of generics (int, float).
      I am modifying the source code.
      Thanks again.

  • karan wrote on 4 January, 2010, 11:06

    Hi Viral
    There is a error in the naming CustomerAction-validations.xml….if the name is changed to CustomerAction-validation.xml then only this example work properly else all fields are not validated by the framework …..i hope you will see that issue and post the reply …

    Neways thanx for your nice and easy tutorials…
    Hope u will continue the same.

    • Viral Patel wrote on 4 January, 2010, 16:36

      @karan – Thanks for pointing out the error. I have updated the tutorial. Although screenshot is showing correct name class.validation.xml. Thanks again.

  • karan wrote on 5 January, 2010, 14:13

    Hii Viral ….
    you have rightly said that screen shots are showing the correct name …..i actually saw the corrections from the screen shots when my example was not working …….thanx for updating tutorial so fast …….it will help a lot to starters like me ..

    and how can we change the fonts or colors or alignments of our errror messages shown in this example…this question seems foolish ..but i dont mind bieng a fool …lolzz

    and lastly could you please give a small tutorial on how to internationalize a webpage…actually i m trying this ..but getting some errors on output screen…if not then please post any gud link for all that in reply…..

    Thanx….

  • Vishwanath Tomar wrote on 23 January, 2010, 20:05

    I like the way you write/explain the things.. I visit many pages, but the page you wrote are very good.

    Thanks, keep it up.

    ~VT

  • Jagadish wrote on 7 March, 2010, 14:07

    hi Viral,

    A ton thanks for your tutorial. Its working like a charm.

    If you can provide a bit more theory about how these validations actually works, it would be more helpfull.

    Any ways thanks for your work……..Hoping to see lots of useful stuff from you.

    Regards,

    Jagadish

  • Mayur wrote on 20 March, 2010, 3:31

    It is a really great tutorial..Couldn’t thank you enough.

  • yourfriendship wrote on 24 April, 2010, 9:04

    thanks !tutorial are very good!

  • Ashwin wrote on 29 April, 2010, 0:07

    Hi, How do you implement validations with struts having a wildcard mapping? Because i seem to get an error

  • Abiodun wrote on 31 May, 2010, 17:41

    Thanks viral for the great work. You really simplified things. I was almost giving
    Up on struts until I found ur tutorial. Pls would be grateful if you could include
    Reading from an excel file and writing to an excel file using struts 2

  • Harsimranjit Singh wrote on 13 June, 2010, 21:37

    Its amazing dear… THE BEST article of struts on entire web that i’v searched like hell.. The best part is you are providing all the required jars, tools and the source code of-course.. I am surely going to come back on your blog, for any kind of reference… keep it up buddy… you’ll succeed..

  • reza wrote on 14 October, 2010, 8:14

    Hi viral,

    i’ve followed your tutorial. but, when i click the button, the value of the fields fill it’s own label… do i missed something?

    thanks.
    Reza

  • reza wrote on 14 October, 2010, 8:41

    hi,

    just, ignore my previous post. it iwas my fault, i missed to add some keys on ApplicationResources.properties file.
    Btw, i’m very pleased with your tutorial. thank you so much.

    regards,
    Reza

  • vijaya wrote on 9 November, 2010, 2:09

    Hi Viral,
    This tutorial is really good,i am just curious to know more about inbuilt validators in struts2.
    regards,
    vijaya

  • khushboo agrawal wrote on 24 November, 2010, 11:07

    Hi veera,
    This tutorial is helped me alot to learn about struts2 so i want to thank you.

    Thanks

  • monti wrote on 24 November, 2010, 20:13

    hi viral i mgetting this error when i attempted to run the given code.
    it appeared after pressing the add customer button

    exception

    javax.servlet.ServletException: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:515)
    org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:422)

    root cause

    java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String

    please help me in solving the problem

  • Sharad wrote on 1 December, 2010, 0:58

    Hi Viral,
    This tutorial helps me a lot…
    Thanks

  • Sadashiv wrote on 22 February, 2011, 19:10

    Struts2 Validation Framework Tutorial with Example
    when i am going to run the Customer.jsp it’s giving fallowing error please help me

    type Exception report

    message

    description The server encountered an internal error () that prevented it from fulfilling this request.

    exception

    org.apache.jasper.JasperException: An exception occurred processing JSP page /Customer.jsp at line 11

    8:
    9: Customer Form
    10:
    11:
    12:
    13:
    14:

    Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:401)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

    root cause

    javax.servlet.ServletException: java.lang.ExceptionInInitializerError
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:862)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:791)
    org.apache.jsp.Customer_jsp._jspService(Customer_jsp.java:83)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

    root cause

    java.lang.ExceptionInInitializerError
    org.apache.struts2.views.jsp.TagUtils.getStack(TagUtils.java:58)
    org.apache.struts2.views.jsp.StrutsBodyTagSupport.getStack(StrutsBodyTagSupport.java:44)
    org.apache.struts2.views.jsp.ComponentTagSupport.doStartTag(ComponentTagSupport.java:48)
    org.apache.jsp.Customer_jsp._jspx_meth_s_005fform_005f0(Customer_jsp.java:104)
    org.apache.jsp.Customer_jsp._jspService(Customer_jsp.java:73)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

    root cause

    org.apache.commons.logging.LogConfigurationException: org.apache.commons.logging.LogConfigurationException: org.apache.commons.logging.LogConfigurationException: Invalid class loader hierarchy. You have more than one version of ‘org.apache.commons.logging.Log’ visible, which is not allowed. (Caused by org.apache.commons.logging.LogConfigurationException: Invalid class loader hierarchy. You have more than one version of ‘org.apache.commons.logging.Log’ visible, which is not allowed.) (Caused by org.apache.commons.logging.LogConfigurationException: org.apache.commons.logging.LogConfigurationException: Invalid class loader hierarchy. You have more than one version of ‘org.apache.commons.logging.Log’ visible, which is not allowed. (Caused by org.apache.commons.logging.LogConfigurationException: Invalid class loader hierarchy. You have more than one version of ‘org.apache.commons.logging.Log’ visible, which is not allowed.))
    org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:543)
    org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:235)
    org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:209)
    org.apache.commons.logging.LogFactory.getLog(LogFactory.java:351)
    com.opensymphony.xwork2.util.logging.commons.CommonsLoggerFactory.getLoggerImpl(CommonsLoggerFactory.java:29)
    com.opensymphony.xwork2.util.logging.LoggerFactory.getLogger(LoggerFactory.java:42)
    org.apache.struts2.dispatcher.Dispatcher.(Dispatcher.java:96)
    org.apache.struts2.views.jsp.TagUtils.getStack(TagUtils.java:58)
    org.apache.struts2.views.jsp.StrutsBodyTagSupport.getStack(StrutsBodyTagSupport.java:44)
    org.apache.struts2.views.jsp.ComponentTagSupport.doStartTag(ComponentTagSupport.java:48)
    org.apache.jsp.Customer_jsp._jspx_meth_s_005fform_005f0(Customer_jsp.java:104)
    org.apache.jsp.Customer_jsp._jspService(Customer_jsp.java:73)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

    root cause

    org.apache.commons.logging.LogConfigurationException: org.apache.commons.logging.LogConfigurationException: Invalid class loader hierarchy. You have more than one version of ‘org.apache.commons.logging.Log’ visible, which is not allowed. (Caused by org.apache.commons.logging.LogConfigurationException: Invalid class loader hierarchy. You have more than one version of ‘org.apache.commons.logging.Log’ visible, which is not allowed.)
    org.apache.commons.logging.impl.LogFactoryImpl.getLogConstructor(LogFactoryImpl.java:397)
    org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:529)
    org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:235)
    org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:209)
    org.apache.commons.logging.LogFactory.getLog(LogFactory.java:351)
    com.opensymphony.xwork2.util.logging.commons.CommonsLoggerFactory.getLoggerImpl(CommonsLoggerFactory.java:29)
    com.opensymphony.xwork2.util.logging.LoggerFactory.getLogger(LoggerFactory.java:42)
    org.apache.struts2.dispatcher.Dispatcher.(Dispatcher.java:96)
    org.apache.struts2.views.jsp.TagUtils.getStack(TagUtils.java:58)
    org.apache.struts2.views.jsp.StrutsBodyTagSupport.getStack(StrutsBodyTagSupport.java:44)
    org.apache.struts2.views.jsp.ComponentTagSupport.doStartTag(ComponentTagSupport.java:48)
    org.apache.jsp.Customer_jsp._jspx_meth_s_005fform_005f0(Customer_jsp.java:104)
    org.apache.jsp.Customer_jsp._jspService(Customer_jsp.java:73)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

    root cause

    org.apache.commons.logging.LogConfigurationException: Invalid class loader hierarchy. You have more than one version of ‘org.apache.commons.logging.Log’ visible, which is not allowed.
    org.apache.commons.logging.impl.LogFactoryImpl.getLogConstructor(LogFactoryImpl.java:385)
    org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:529)
    org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:235)
    org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:209)
    org.apache.commons.logging.LogFactory.getLog(LogFactory.java:351)
    com.opensymphony.xwork2.util.logging.commons.CommonsLoggerFactory.getLoggerImpl(CommonsLoggerFactory.java:29)
    com.opensymphony.xwork2.util.logging.LoggerFactory.getLogger(LoggerFactory.java:42)
    org.apache.struts2.dispatcher.Dispatcher.(Dispatcher.java:96)
    org.apache.struts2.views.jsp.TagUtils.getStack(TagUtils.java:58)
    org.apache.struts2.views.jsp.StrutsBodyTagSupport.getStack(StrutsBodyTagSupport.java:44)
    org.apache.struts2.views.jsp.ComponentTagSupport.doStartTag(ComponentTagSupport.java:48)
    org.apache.jsp.Customer_jsp._jspx_meth_s_005fform_005f0(Customer_jsp.java:104)
    org.apache.jsp.Customer_jsp._jspService(Customer_jsp.java:73)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

    note The full stack trace of the root cause is available in the Apache Tomcat/6.0.29 logs.

    ——————————————————————————–

    Apache Tomcat/6.0.29

  • shantanu wrote on 3 March, 2011, 1:55

    heya!
    thanx for so many wonderful examples.. i used this code for simple form validation, but m ffacing problem in having more than one field with a name.. like if i want more fields with same name to get validated, by using array for setter-getter in class file, so that it gets validated with same name in .xml file… but its setting the values in text field by “, ” using your code.. plzz help.. refer to:
    first name
    last name
    age
    email

  • Shekar wrote on 6 March, 2011, 8:33

    Hi Viral, Thanks for this well documented tutorial. However when I tried the example code, I am mgetting the below mentioned error when i attempted to run the given code. This is happening when I press the add customer button

    exception

    java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
    at java.lang.String.compareTo(Unknown Source)
    at com.opensymphony.xwork2.validator.validators.AbstractRangeValidator.validate(AbstractRangeValidator.java:29)
    at com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.validate(AnnotationActionValidatorManager.java:219)
    at com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.validate(AnnotationActionValidatorManager.java:113)
    at com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.validate(AnnotationActionValidatorManager.java:100)
    at com.opensymphony.xwork2.validator.ValidationInterceptor.doBeforeInvocation(ValidationInterceptor.java:142)
    at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:148)
    at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:48)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)

    Would you please please help me in solving the problem

  • swapna wrote on 7 March, 2011, 17:09

    Hi Viral ,
    Thank u so much for providing such a Understandable tutorial. Really it”s too Good.

    • Viral Patel wrote on 7 March, 2011, 18:05

      @Swapna – Thanks for the kind words :)

  • Mike wrote on 17 June, 2011, 12:55

    Great tutorial so far. I have one problem – when I add ‘validate=”true”‘ to enable client-side validation, I get the following error message in my browser:

    validateForm_customer is not defined

    I inspected the HTML source and the source of the included validation.js and utils.js files, and there’s definitely no trace of that function. Is there something else that needs to be done to enable JS generation? I’m using Struts 2.2.3 and Tomcat 6.0.32.

    Thanks!

  • Lansana Bangura wrote on 16 July, 2011, 1:36

    I have tried the tutorials, but I am having difficulties. Each time I run the HelloWorld example, I have a 404 error in the browser. I have used the latest version of tomcat, and Struts, is it because of this or I’d have to use the older versions that were used by you?

    • Viral Patel wrote on 20 July, 2011, 23:58

      @Lansana – The 404 error might be coming due to issues in dependencies. Check the server logs. You might have some other exceptions there.

  • pirateme wrote on 16 July, 2011, 15:39

    org.apache.jasper.JasperException: /Customer.jsp (line: 11, column: 61) The JSP specification requires that an attribute name is preceded by whitespace
    org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:41)
    org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
    org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:88)
    org.apache.jasper.compiler.Parser.parseAttributes(Parser.java:164)
    org.apache.jasper.compiler.Parser.parseAttributes(Parser.java:153)
    org.apache.jasper.compiler.Parser.parseCustomTag(Parser.java:1236)
    org.apache.jasper.compiler.Parser.parseElements(Parser.java:1450)
    org.apache.jasper.compiler.Parser.parse(Parser.java:138)
    org.apache.jasper.compiler.ParserController.doParse(ParserController.java:239)
    org.apache.jasper.compiler.ParserController.parse(ParserController.java:102)
    org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:197)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:372)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:352)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:339)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:601)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:344)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:416)

    i am getting this error while running.Give me a solution…its urgent…..

  • Mohd Adnan wrote on 4 August, 2011, 13:16

    Hi,

    I would like to ask one thing? suppose our action contains any custom object say User user;

    so how to write validation code for user. I have mentioned user.id and user.firstName as key in validation.xml but it throws an exception while rendering page.

    help me??

  • Melev wrote on 21 October, 2011, 8:06

    Hi: validation on client side is not working, I don’t see the file: validator-rules.xml, where can i get it? can you provide me some useful information? thanks in advance!

  • Chandrasekhar wrote on 16 December, 2011, 17:29

    Hi Viral, Your Tutorial is Simply Awesome. Even to say i started likely Struts after visiting your site.
    Thx. But When i executed the above code in the customer screen no validation is happening. Even if i leave all fields empty and click add customer, its adding. I had all names same as like how u gave in ur example…

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Copyright © 2012 ViralPatel.net. All rights reserved.