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. [sc:Struts2_Tutorials]
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 interfacecom.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.- Field Validators
- Non-field validators
<validators>
<field name="bar">
<field-validator type="required">
<message>You must enter a value for bar.</message>
</field-validator>
</field>
</validators>
Code language: HTML, XML (xml)
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>
Code language: HTML, XML (xml)
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. Copy following content into it. CustomerAction.javapackage 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;
}
}
Code language: Java (java)
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>
Code language: HTML, XML (xml)
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). 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>
Code language: HTML, XML (xml)
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>
Code language: HTML, XML (xml)
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>
Code language: HTML, XML (xml)
Now open the ApplicationResources.properties file from /resources folder and add following key/values in it.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.Code language: HTML, XML (xml)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}.
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:- Name field is mandatory
- Age field is mandatory. It should be a number between 1 and 100.
- Email field is mandatory. It should be a valid email address.
- Telephone is mandatory.
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.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>
Code language: HTML, XML (xml)
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>
Code language: HTML, XML (xml)
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
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.
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.
@karan – Thanks for pointing out the error. I have updated the tutorial. Although screenshot is showing correct name class.validation.xml. Thanks again.
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….
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
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
It is a really great tutorial..Couldn’t thank you enough.
thanks !tutorial are very good!
Hi, How do you implement validations with struts having a wildcard mapping? Because i seem to get an error
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
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..
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
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
Hi Viral,
This tutorial is really good,i am just curious to know more about inbuilt validators in struts2.
regards,
vijaya
Hi veera,
This tutorial is helped me alot to learn about struts2 so i want to thank you.
Thanks
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
Hi Viral,
This tutorial helps me a lot…
Thanks
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
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
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
Hi Viral ,
Thank u so much for providing such a Understandable tutorial. Really it”s too Good.
@Swapna – Thanks for the kind words :)
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!
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?
@Lansana – The 404 error might be coming due to issues in dependencies. Check the server logs. You might have some other exceptions there.
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…..
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??
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!
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…
Hi, Even I am facing the same prob..Did you find any solution for that.???
Everything perfectly fine. I guess, no validation for “telephone” filed.
Hi, All your tutorials are simply superb. In one glance i learnt many more things in struts2.
I have a problem in my code that, i have used a drop down list and values r fetching from database. and its displaying fine in jsp page but on actionError i have to reload the list with error msg. Am not getting any clues to do. Please help me to do the stuff.
Thanks in advance.
Sharan K P
Viral thanks for the tutorial
but validation is not working
please provide the solution
thanks in advance
Regards
Rahul
Hi, the validate=”true” is not working, has anybody found the solution? if so then can u pls share it across.
Thanking you in advance.
In the previous article, u said the execute() method is the default method for the Action class,but in the CustomerAction class the method is addCustomer(), it seems you didn’t specify the method in the action tag .Would you explain it? thanks in advance .
Hi,
The tutorial is very much helpful,Thanks for this.
How do we validate credit card details in struts 2?
@dz
The method has been specified in the s:submit tag,so on submit this method will be searched for,in the action class.
Hi Viral,
Thanks a lot for your contribution on this tutorial, even i got error while running validating.
[4/24/12 22:19:56:784 IST] 00000039 filter E com.ibm.ws.webcontainer.filter.FilterInstanceWrapper service SRVE8109W: Uncaught exception thrown by filter IESCWEB: java.lang.NoClassDefFoundError: com.opensymphony.xwork2.validator.ValidatorFactory (initialization failure)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:145)
at com.opensymphony.xwork2.validator.ValidatorFileParser.addValidatorConfigs(ValidatorFileParser.java:192)
at com.opensymphony.xwork2.validator.ValidatorFileParser.parseActionValidatorConfigs(ValidatorFileParser.java:72)
at com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.loadFile(AnnotationActionValidatorManager.java:361)
at com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.buildClassValidatorConfigs(AnnotationActionValidatorManager.java:252)
at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1650)
Hi Viral,
I have worked out as per your suggestions. But validation cannot be done.. Can you help me out?
Sir, where we have to create this above validation (xml) file. I use netbeans 6.9 for struts2.But my program not working properly.can u help me.
thanks.
its really easy to understand.
but if we want to show error msg. in red font then what we have to do
Very easy to Learn.
thanks viral ………….
its very cleanto understand the strut 2 validation …………..
once again thanks ……………….please explain more theortical……….
Hi
The tutorial is very helpful for beginners like me , Thanks a lot.
I test the source code with Struts 2.3.4.1-all, Validation doesn’t work for me.
BTW, some information from Struts 2 website.
<!–
Add the following DOCTYPE declaration as first line of your XXX-validation.xml file:
–>
Finally, I solved this by changing the validation file. Just use the following as the first line to my validation file. It does work now!!
DOCTYPE validators PUBLIC “-//Apache Struts//XWork Validator 1.0.2//EN” “http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd”
Remove , because it cann’t be posted with them.
I got this error when i run this please help
org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:500)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:413)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:308)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:259)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:88)
root cause
Method public java.util.List org.apache.struts2.components.Form.getValidators(java.lang.String) threw an exception when invoked on org.apache.struts2.components.Form@1a8dfb3 – Class: freemarker.ext.beans.SimpleMethodModel
File: SimpleMethodModel.java
Method: exec
Line: 130 – freemarker/ext/beans/SimpleMethodModel.java:130:-1
org.apache.struts2.components.UIBean.end(UIBean.java:521)
org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)
org.apache.jsp.Login_jsp._jspx_meth_s_005fform_005f0(Login_jsp.java:159)
org.apache.jsp.Login_jsp._jspService(Login_jsp.java:80)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:371)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:308)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:259)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:88)
root cause
freemarker.template.TemplateModelException: Method public java.util.List org.apache.struts2.components.Form.getValidators(java.lang.String) threw an exception when invoked on org.apache.struts2.components.Form@1a8dfb3
freemarker.ext.beans.SimpleMethodModel.exec(SimpleMethodModel.java:130)
freemarker.core.MethodCall._getAsTemplateModel(MethodCall.java:93)
freemarker.core.Expression.getAsTemplateModel(Expression.java:89)
freemarker.core.IteratorBlock.accept(IteratorBlock.java:94)
freemarker.core.Environment.visit(Environment.java:221)
freemarker.core.IteratorBlock$Context.runLoop(IteratorBlock.java:167)
freemarker.core.Environment.visit(Environment.java:428)
freemarker.core.IteratorBlock.accept(IteratorBlock.java:102)
freemarker.core.Environment.visit(Environment.java:221)
freemarker.core.MixedContent.accept(MixedContent.java:92)
freemarker.core.Environment.visit(Environment.java:221)
freemarker.core.ConditionalBlock.accept(ConditionalBlock.java:79)
freemarker.core.Environment.visit(Environment.java:221)
freemarker.core.MixedContent.accept(MixedContent.java:92)
freemarker.core.Environment.visit(Environment.java:221)
freemarker.core.Environment.include(Environment.java:1508)
freemarker.core.Include.accept(Include.java:169)
freemarker.core.Environment.visit(Environment.java:221)
freemarker.core.MixedContent.accept(MixedContent.java:92)
freemarker.core.Environment.visit(Environment.java:221)
freemarker.core.Environment.process(Environment.java:199)
freemarker.template.Template.process(Template.java:259)
org.apache.struts2.components.template.FreemarkerTemplateEngine.renderTemplate(FreemarkerTemplateEngine.java:157)
org.apache.struts2.components.UIBean.mergeTemplate(UIBean.java:565)
org.apache.struts2.components.UIBean.end(UIBean.java:519)
org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)
org.apache.jsp.Login_jsp._jspx_meth_s_005fform_005f0(Login_jsp.java:159)
org.apache.jsp.Login_jsp._jspService(Login_jsp.java:80)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:371)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:308)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:259)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:88)
root cause
java.lang.NullPointerException
com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.buildValidatorKey(AnnotationActionValidatorManager.java:229)
com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.getValidators(AnnotationActionValidatorManager.java:86)
com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.getValidators(AnnotationActionValidatorManager.java:82)
org.apache.struts2.components.Form.getValidators(Form.java:265)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
freemarker.ext.beans.BeansWrapper.invokeMethod(BeansWrapper.java:866)
freemarker.ext.beans.SimpleMethodModel.exec(SimpleMethodModel.java:106)
freemarker.core.MethodCall._getAsTemplateModel(MethodCall.java:93)
freemarker.core.Expression.getAsTemplateModel(Expression.java:89)
freemarker.core.IteratorBlock.accept(IteratorBlock.java:94)
freemarker.core.Environment.visit(Environment.java:221)
freemarker.core.IteratorBlock$Context.runLoop(IteratorBlock.java:167)
freemarker.core.Environment.visit(Environment.java:428)
freemarker.core.IteratorBlock.accept(IteratorBlock.java:102)
freemarker.core.Environment.visit(Environment.java:221)
freemarker.core.MixedContent.accept(MixedContent.java:92)
freemarker.core.Environment.visit(Environment.java:221)
freemarker.core.ConditionalBlock.accept(ConditionalBlock.java:79)
freemarker.core.Environment.visit(Environment.java:221)
freemarker.core.MixedContent.accept(MixedContent.java:92)
freemarker.core.Environment.visit(Environment.java:221)
freemarker.core.Environment.include(Environment.java:1508)
freemarker.core.Include.accept(Include.java:169)
freemarker.core.Environment.visit(Environment.java:221)
freemarker.core.MixedContent.accept(MixedContent.java:92)
freemarker.core.Environment.visit(Environment.java:221)
freemarker.core.Environment.process(Environment.java:199)
freemarker.template.Template.process(Template.java:259)
org.apache.struts2.components.template.FreemarkerTemplateEngine.renderTemplate(FreemarkerTemplateEngine.java:157)
org.apache.struts2.components.UIBean.mergeTemplate(UIBean.java:565)
org.apache.struts2.components.UIBean.end(UIBean.java:519)
org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)
org.apache.jsp.Login_jsp._jspx_meth_s_005fform_005f0(Login_jsp.java:159)
org.apache.jsp.Login_jsp._jspService(Login_jsp.java:80)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:371)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:308)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:259)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:88)
note
please help please its urgent
Its a great tutorial helpled me a lot
@ViralPatel and everybody
(Plz have a look at the following points. If anyone of you find these correct or wrong then please share your view)
1) errors.required=${getText(fieldName)} is required.
// Instead of above line it should be ${fieldName} is required. That makes sense. i mean the text of it will be null. so we need to show “fieldname is required”
2) public String addCustomer() {return SUCCESS; }
// If we use any other method than execute() we need to mention that method in struts.xml.. but i don’t exactly remember how to add in struts.xml .. Plz give a touch on that part of struts.xml.
3) We can also extend our CustomerAction to ActionSupport and then overriding validate() we can achieve all these.. then how xml validation is beneficial over validation at class level (ActionSupport).
Btw you really made an great effort to create a wonderfull tutorial.
Thanks a lot
I want to know if i have 5 form then i have to made 5 ApplicationResources.properties files
hi viral…
Ur application is good and I executed also.. but in my own Login form application, when I entered currect username and password with validations that time I got correct output but when I entered wrong uname & password that time I need not to get validation errors through properties file… In this time tomcat6.0 server showing one warning that is “”Resource propertyfilename_en.properties not found” … give me any solution
Thank u very much. It is excellent.
Hi Viral,
my actionclass-validation.xml does not get loaded properly due to which server connection is closed, can you help me in undertanding why is it happening?
Hi viral, your tutorials are absolutely fine and being a great help for me in learning new things.
My query is,how can we validate password and confirm password and displays a error message if both password and confirm password are not matching. i tried it using “expression” validator. validation is working, but error message is not displaying on browser if not matching.plz help me out….
my code in LoginAction-validation.xml–:
pwd.equals(cpwd)
Password’s are not matching
you can alse use the inbuilt method of class ActionSupport
public String validate()
here is the example of it
Thanks, that’ s really a elegant approach. Sounds similar spring mvc
perfect code.
tested this code in eclipse indigo, apache7, struts2.
successfully running.
thanx
Hi i am following your steps. But when i submit the customer form it will sows the following error.
HTTP Status 500 –
——————————————————————————–
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
org.apache.struts2.impl.StrutsActionProxy.getErrorMessage(StrutsActionProxy.java:69)
com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:185)
org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:500)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:434)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.32 logs.
I don’t know? Why its come? And also how can i link the CustomerAction-validation.xml in this project? I am follows ur steps still i got the above error.
Help me…
Hey!
i think your some of your Jsp pages are containing error.!
Also,make sure you have all libraries in your project.!
I am getting the same error as Manikandan. I get the error only when I add the validate = “true” in the jsp. Please help!
More details of the error :
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Method public java.util.List org.apache.struts2.components.Form.getValidators(java.lang.String) threw an exception when invoked on org.apache.struts2.components.Form@7309eabd – Class: freemarker.ext.beans.SimpleMethodModel
File: SimpleMethodModel.java
Method: exec
Line: 130 – freemarker/ext/beans/SimpleMethodModel.java:130:-1
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:570)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:457)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:88)
root cause
Method public java.util.List org.apache.struts2.components.Form.getValidators(java.lang.String) threw an exception when invoked on org.apache.struts2.components.Form@7309eabd – Class: freemarker.ext.beans.SimpleMethodModel
File: SimpleMethodModel.java
Method: exec
Line: 130 – freemarker/ext/beans/SimpleMethodModel.java:130:-1
org.apache.struts2.components.UIBean.end(UIBean.java:521)
org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)
org.apache.jsp.Login_jsp._jspx_meth_s_005fform_005f0(Login_jsp.java:139)
org.apache.jsp.Login_jsp._jspService(Login_jsp.java:75)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:88)
root cause
freemarker.template.TemplateModelException: Method public java.util.List org.apache.struts2.components.Form.getValidators(java.lang.String) threw an exception when invoked on org.apache.struts2.components.Form@7309eabd
freemarker.ext.beans.SimpleMethodModel.exec(SimpleMethodModel.java:130)
freemarker.core.MethodCall._getAsTemplateModel(MethodCall.java:93)
freemarker.core.Expression.getAsTemplateModel(Expression.java:89)
freemarker.core.IteratorBlock.accept(IteratorBlock.java:94)
freemarker.core.Environment.visit(Environment.java:221)
freemarker.core.IteratorBlock$Context.runLoop(IteratorBlock.java:167)
freemarker.core.Environment.visit(Environment.java:428)
freemarker.core.IteratorBlock.accept(IteratorBlock.java:102)
freemarker.core.Environment.visit(Environment.java:221)
freemarker.core.MixedContent.accept(MixedContent.java:92)
freemarker.core.Environment.visit(Environment.java:221)
freemarker.core.ConditionalBlock.accept(ConditionalBlock.java:79)
freemarker.core.Environment.visit(Environment.java:221)
freemarker.core.MixedContent.accept(MixedContent.java:92)
freemarker.core.Environment.visit(Environment.java:221)
freemarker.core.Environment.include(Environment.java:1508)
freemarker.core.Include.accept(Include.java:169)
freemarker.core.Environment.visit(Environment.java:221)
freemarker.core.MixedContent.accept(MixedContent.java:92)
freemarker.core.Environment.visit(Environment.java:221)
freemarker.core.Environment.process(Environment.java:199)
freemarker.template.Template.process(Template.java:259)
org.apache.struts2.components.template.FreemarkerTemplateEngine.renderTemplate(FreemarkerTemplateEngine.java:157)
org.apache.struts2.components.UIBean.mergeTemplate(UIBean.java:565)
org.apache.struts2.components.UIBean.end(UIBean.java:519)
org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)
org.apache.jsp.Login_jsp._jspx_meth_s_005fform_005f0(Login_jsp.java:139)
org.apache.jsp.Login_jsp._jspService(Login_jsp.java:75)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:88)
root cause
java.lang.NullPointerException
com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.buildValidatorKey(AnnotationActionValidatorManager.java:229)
com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.getValidators(AnnotationActionValidatorManager.java:86)
com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.getValidators(AnnotationActionValidatorManager.java:82)
org.apache.struts2.components.Form.getValidators(Form.java:265)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
freemarker.ext.beans.BeansWrapper.invokeMethod(BeansWrapper.java:866)
freemarker.ext.beans.SimpleMethodModel.exec(SimpleMethodModel.java:106)
freemarker.core.MethodCall._getAsTemplateModel(MethodCall.java:93)
freemarker.core.Expression.getAsTemplateModel(Expression.java:89)
freemarker.core.IteratorBlock.accept(IteratorBlock.java:94)
freemarker.core.Environment.visit(Environment.java:221)
freemarker.core.IteratorBlock$Context.runLoop(IteratorBlock.java:167)
freemarker.core.Environment.visit(Environment.java:428)
freemarker.core.IteratorBlock.accept(IteratorBlock.java:102)
freemarker.core.Environment.visit(Environment.java:221)
freemarker.core.MixedContent.accept(MixedContent.java:92)
freemarker.core.Environment.visit(Environment.java:221)
freemarker.core.ConditionalBlock.accept(ConditionalBlock.java:79)
freemarker.core.Environment.visit(Environment.java:221)
freemarker.core.MixedContent.accept(MixedContent.java:92)
freemarker.core.Environment.visit(Environment.java:221)
freemarker.core.Environment.include(Environment.java:1508)
freemarker.core.Include.accept(Include.java:169)
freemarker.core.Environment.visit(Environment.java:221)
freemarker.core.MixedContent.accept(MixedContent.java:92)
freemarker.core.Environment.visit(Environment.java:221)
freemarker.core.Environment.process(Environment.java:199)
freemarker.template.Template.process(Template.java:259)
org.apache.struts2.components.template.FreemarkerTemplateEngine.renderTemplate(FreemarkerTemplateEngine.java:157)
org.apache.struts2.components.UIBean.mergeTemplate(UIBean.java:565)
org.apache.struts2.components.UIBean.end(UIBean.java:519)
org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)
org.apache.jsp.Login_jsp._jspx_meth_s_005fform_005f0(Login_jsp.java:139)
org.apache.jsp.Login_jsp._jspService(Login_jsp.java:75)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:88)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.12 logs.
Hi rj4u, how did you fixed you error, i meet the same one.
I am also getting the same error, please forward you fixed that error.
Hi..I followed the steps
But when i submitted the customer form it is throwing error in console and redirecting to success page.
Error:
SEVERE: Caught exception while loading file net/viralpatel/struts2/CustomerAction-validation.xml
Connection timed out: connect – [unknown location]
at com.opensymphony.xwork2.util.DomHelper.parse(DomHelper.java:123)
If your action class is CustomerAction.java, try your vaidation xml file as customer-validation.xml.
C ya
Hi,
if I had a validateCustomer method in my class, which validation method would be executed?
in other words, which validation method prevails: xml or programmatic validation
thanks!
Hi I follow your steps but it seems like the validator is not working.
Thanks in advance for replying.
customeraction-validation.xml and ApplicationResources.properties dont match the field and key/values. This is the reason u guys are getting errors when u run the program….
try to match the fields in both the “ApplicationResources.properties ” file and “CustomerAction-validation.xml” file then the program will work without any errors…….also there is an error in the ‘Form’ tag of “customer.jsp”……please change that to
once u make the above corrections your program will be error free……
good luck executing the program guys……….if u guys have issues with bugs u can mail me at [email protected]……..please mention the name of the bug in the subject line…..im a java developer too…..im happy to help fresh minds who are eager to learn
I am trying to add struts 2 validations in struts 2+spring+hibernate application … but it is not getting effected can you help me
Same error as rj4u,
I am using freemarker-2.3.19.jar , struts2-core-2.3.4.1.jar, xwork-core-2.3.4.1.jar
@RjforU,Adolf
Download the project folder shared by them.Replace your lib files with their lib files.It will work.
Hi
My whole framework works well…I validates my form …redirect to form with error message when validation fails but ….when it redirect to the form after validation fail…I lose all my form data. I do not wish for user to re-enter information …I would like to retain all user input and let user update it…only thing I have done different is that I have not used key in the for I have separate label and name…Can you help me please ?
Thanks
error at struts 2 xml validation when using multiple validation per field. im using xwork-validator-1.0.3.dtd
You must enter a value for bar.
[/code xml]
Working fine with single field-validator though.
** also tried short-circuit still the same exception
I needed to add !DOCTYPE for my CustomerAction-validation.xml document. Great tutorial!!!
Heloo.. I am a novice here…
pls help me with the following error
Hey Kshama Joshi,
i’ve got the same error like:
solved it by changeing the .dtd transcription in the -validation.xml Doctype to:
the Open Symphony “xwork-validator.dtd” wasn’t available in the Framework i’ve loaded. so changing the Doctype to the one mentioned above should solve your problem.
Best Regards Fabi
sorry the code snippet changed my Doctype:
i mean you have to add:
BR Fabi
@rj4u and Adolf ,,Just add aappend(.action) to your attribute tag of ur JSP file like
hey i have download ur example it works fine but when i try to build my own project everything works fine but client side validation is not performed. should i have to include some pacakges.
Good tutorial!!
I have several runtime error in ecllipse in my struts 2 SImple Login Form. Pl see my errors
Hi,
You dont have all libraries needed in your lib folder.Check it once.
Hey i want to know what can i use the datatype for grouping the variable,plz any one know this tell me….im trying to develop the web site blog i have completed 60% but i dont have idea about how can i gruop the reply variable which is automatically increament as per replys will increament…Plz help me in this…
Thanks…. all your struts related tutorials are very mush useful as well as helpful..
Hey really a nice example….
but i am facing one problem where will i get the ApplicationResources.properties file in netbeans 7.1
i am getting the output but validations are not getting performed.please help me.
thanks in advance
i have struts2-tiles-plugin-2.0.11.2.jar in lib,but when deploy gives this error in console:
can help me?
thanks
i have struts2-tiles-plugin-2.0.11.2.jar in lib,but when deploy gives this error in console:
can help me?
thanks
i have struts2-tiles-plugin-2.0.11.2.jar in lib,but when deploy gives this error in console:
can help me?
thanks
when i run application i get the following error !!!
Sep 28, 2013 6:38:05 PM com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
WARNING: No configuration found for the specified action: ‘Display’ in namespace: ”. Form action defaulting to ‘action’ attribute’s literal value.
Sep 28, 2013 6:38:06 PM com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
WARNING: No configuration found for the specified action: ‘Display’ in namespace: ”. Form action defaulting to ‘action’ attribute’s literal value.
Sep 28, 2013 6:38:08 PM com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
WARNING: Could not find action or result
There is no Action mapped for namespace / and action name Display. – [unknown location]
at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:189)
at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:61)
at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:475)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
Article explaning with field level validations is quite impressive.
But how to validate in case of index based form fields?
Any Idea is highly appreciated.. :)
when i run application i get the following error !!!
please help me.
There is no Action mapped for namespace [/] and action name [Customer] associated with context path [/StrutsExample]. – [unknown location]
at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:185)
at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:553)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
Nov 18, 2013 3:40:12 PM org.apache.struts2.dispatcher.Dispatcher error
SEVERE: Exception occurred during processing request: There is no Action mapped for namespace [/] and action name [Customer] associated with context path [/StrutsExample].
There is no Action mapped for namespace [/] and action name [Customer] associated with context path [/StrutsExample]. – [unknown location]
at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:185)
at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:553)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
Hi Virat,
Its nice tutorial, very help ful for beginners like me.
I have tried as mentioned in the totorial for client side validation.
But it is not working.
Pls update how to implement validation when we are using beans.
Thanks
How to Skip the validation from Submit Button
simply call the validation method in onblur event
how to add validation code in struts2
Do you mind if I quote a couple of your articles as long as I provide
credit and sources back to your blog? My
blog site is in the exact same area of interest as yours and my users would truly benefit from a lot of the information you provide here.
Please let me know if this okay with you. Cheers!
Struts2 validation is not working when if you use tag and if you display both the result and the UI form in same jsp .. why?
I am working with struts2 and with json.
my client side is on android and when user/client send request in url it should check server validation and store in database and should only return required fields with status and set message.
I dint get validate=”true” in customer.jsp. What kind of javascript validation code is added by struts-2? Please explain..
Thanks:
Ramakrishna k.c
from where we have to open -validation.xml in netbeans7.0
Hi
when i put Validate =”true” in JSP i am facing error ,but without validate=”true”it is working fine
Many thanks for the great tutorial. You mentioned to add validate=”true” in form tag to enable javascript validation. It is working fine. But one thing i noticed is that for the very first time (when you click Add Customer button ) , validation is server-side, but after that validation is client side. Do you see that too?
I have this error when I don’t complete the fields in the form:
05/01/2015 12:07:09 org.apache.struts2.components.ServletUrlRenderer warn
ADVERTENCIA: No configuration found for the specified action: ‘login’ in namespace: ‘/pages’. Form action defaulting to ‘action’ attribute’s literal value.
05/01/2015 12:07:10 org.apache.struts2.components.ServletUrlRenderer warn
ADVERTENCIA: No configuration found for the specified action: ‘login’ in namespace: ‘/pages’. Form action defaulting to ‘action’ attribute’s literal value.
05/01/2015 12:07:21 org.apache.struts2.dispatcher.Dispatcher warn
ADVERTENCIA: Could not find action or result: /FotoMaster/pages/login
No result defined for action fotomaster.actions.LoginAction and result input – action – file:/D:/FotoMaster/workspaceSharon/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/FotoMaster/WEB-INF/classes/struts.xml:10:69
When I complete the fields in the form, it works.
I don’t see the validationForm_login method in the source code, struts2 doesn’t generate it.
Doesn’t work the validation of struts 2
Can you help me?
Thanks
Bro i am having separate bean class for setter and getter. do i need to create validation.xml for that class??? or action class only?
Jst for info, i am having 3 classes,
1)Action class having methods which are configured in struts.xml lik save update viewall delete
2)bean class having only getter and setter and its constructor
3)class which having business logic methods which are called by action class methods
hi
can u explain how to skip the field-validator in xml like if & else cases?Instead of removing the field-validator,i need to use the if and else cases in xml. (not using the the java bean)
Thanks in advance