Frameworks| Hibernate| Struts| JSF| JavaFX| Ajax| Spring| DOJO| JDO| iBatis| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Client Side validation in Struts 2 application 
 

In this section we will see how to write code that will generate Java Script code for client side validation. In the last section we developed Login-validator.xml configuration file for defining the server side validation. In this section we will use the

 

Client Side validation in Struts 2 application

                         

In this section we will see how to write code that will generate Java Script code for client side validation. In the last section we developed Login-validator.xml configuration file for defining the server side validation. In this section we will use the same Login-validator.xml file for generating the client side java script.

 

Developing JSP pages

Here is the code of login jsp page (loginClientSideValidation.jsp)

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Struts 2 Login Application!</title>

<link href="<s:url value="/css/main.css"/>" rel="stylesheet"
type="text/css"/>

</head>
<body>


<s:form action="doLoginClientSideValidation" method="POST" validate="true">

<tr>
<td colspan="2">
Login
</td>

</tr>

<s:actionerror />
<s:fielderror />

<s:textfield name="username" label="Login name"/>
<s:password name="password" label="Password"/>
<s:submit value="Login" align="center"/>

</s:form>

</body>

</html>

Note that in the above code we have just added  validate="true" in the <s:form tag...>. This is the only work we have to do and rest work is done by Struts 2 validator framework. The validator framework generates JavaScript for validating the form at client side.

Changes in the struts.xml

Add the following code into struts.xml file:

<action name="showLoginClientSideValidation">
<result>/pages/loginClientSideValidation.jsp</result>
</action>

<action name="doLoginClientSideValidation" class="net.roseindia.Login">
<result name="input">/pages/loginClientSideValidation.jsp</result>
<result name="error">/pages/loginClientSideValidation.jsp</result>
<result>/pages/loginsuccess.jsp</result>
</action>


The action showLoginClientSideValidation displays login form while doLoginClientSideValidation handles the validation request.

Adding the link into index.html

Finally we add the link in the index.html to access the login form for testing client side validation.

<ul>
<li><a href="roseindia/showLoginClientSideValidation.action">Login Application (Client Side Validation)</a></li>
</ul>

Testing the Client side validation

Start tomcat and type http://localhost:8080/struts2tutorial/. Your browser should show the following screen:

 

Now click on "Login Application (Client Side Validation)" link. Then your browser will display the Login page as shown below:

 

Click on the "Login" button without entering anything. Java Script will show the error message as shown below:

 

Now enter the "Login Name" and click on the "Login" button, application will show error as shown below:

 

Examining the Java Script code generated

The following html code is generated by the framework:

<html>

<head>

<title>Struts 2 Login Application!</title>

<link href="/struts2tutorial/css/main.css" rel="stylesheet"

type="text/css"/>
</head>

<body>
<script src="/struts2tutorial/struts/xhtml/validation.js"></script>
<form namespace=
"/roseindia" id="doLoginClientSideValidation" name="doLoginClientSideValidation" onsubmit="return validateForm_doLoginClientSideValidation();" action="/struts2tutorial/roseindia/doLoginClientSideValidation.action" method="POST">

<table class="wwFormTable">

<tr>

<td colspan="2">

Login

</td>

</tr>

<tr>

<td class="tdLabel"><label for="doLoginClientSideValidation_username" class="label">Login name:</label></td>

<td

><input type="text" name="username" value="" id="doLoginClientSideValidation_username"/>

</td>

</tr>

<tr>

<td class="tdLabel"><label for="doLoginClientSideValidation_password" class="label">Password:</label></td>

<td

><input type="password" name="password" id="doLoginClientSideValidation_password"/>

</td>

</tr>

<tr>

<td colspan="2"><div align="center"><input type="submit" id="doLoginClientSideValidation_0" value="Login"/>

</div></td>

</tr>

 

</table></form>

 

 

<script type="text/javascript">

function validateForm_doLoginClientSideValidation() {

form = document.getElementById("doLoginClientSideValidation");

clearErrorMessages(form);

clearErrorLabels(form);

var errors = false;

// field name: username

// validator name: requiredstring

if (form.elements['username']) {

field = form.elements['username'];

var error = "Login name is required";

if (field.value != null && (field.value == "" || field.value.replace(/^\s+|\s+$/g,"").length == 0)) {

addError(field, error);

errors = true;

}

}

// field name: password

// validator name: requiredstring

if (form.elements['password']) {

field = form.elements['password'];

var error = "Password is required";

if (field.value != null && (field.value == "" || field.value.replace(/^\s+|\s+$/g,"").length == 0)) {

addError(field, error);

errors = true;

}

}

return !errors;

}

</script>

</body>

</html>

In the above code you can see the JavaScript code and function validateForm_doLoginClientSideValidation() which is generated for client side validation

                         

» View all related tutorials
Related Tags: c ajax flex deployment ide plugins dojo struts io tags servlet annotations help sed release test vi port new tag

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

12 comments so far (
post your own) View All Comments Latest 10 Comments:

Hi,
This is Raj. New to struts validation framwork. I was trying a sample poc kind of application using struts validation. On the fly framework generating java script files. but i am not able to locate that file physically. using browser's source view, the path which it displays that path is not available in my apllication deployment directory.

Your's help appreciated.
Thanks in advance
Raj

Posted by Raj on Friday, 12.19.08 @ 01:17am | #82954

hi,
I am new to Struts 2 and i tried this application,
when i validate it ,its simply showing user name and password not empty, but fielderrors or not displaying any messages.I used maven to create my project architecture..
can u pls send me the entire code..

Thanks a lot in advance

Posted by Arif on Wednesday, 10.29.08 @ 13:40pm | #81388

i want login form of client side validation purpose

Posted by suresh on Tuesday, 08.19.08 @ 10:24am | #73704

In the form declaration on the Login.jsp, specify action name and its package namespace separately as :

<s:form action="doLogin" namespace="/roseindia/net" method="POST" validate="true">

This should work now :))

Posted by Diksha Jain on Tuesday, 06.10.08 @ 14:40pm | #62816

--------Original Message --------------------------
Hi Im new to Struts 2 and im trying to use the client side validation but but the java script function is no created, the call is genarated but y repeat the function not. Can someano tell me why.

Thanks very much.

Bye

Posted by William on Thursday, 12.27.07 @ 08:35am | #43908
-----------------------------------
My Opinion

Yes it was done twice. client validation is faster and will not send the data to the server. if client validation fails incase when javascript is disabled...a 2nd catch validation will be activated on the server side...You can see how elegant Struts 2 is.

You can see client validation when you see view page source.

The client validation is OK for me the same message is being shown

Posted by Jaizon F. Lubaton on Monday, 06.9.08 @ 23:31pm | #62749

I've a app that validate a form. When the validation is wrong, the theme change to "default" theme or something like that.

I use, in order to set my theme:

<head>
<title>Login</title>
<link rel=StyleSheet href="/resources/style.css" type="text/css"
media=screen>
</head>

Posted by juan pablo on Tuesday, 03.4.08 @ 01:59am | #51215

Hi Im new to Struts 2 and im trying to use the client side validation but but the java script function is no created, the call is genarated but y repeat the function not. Can someano tell me why.

Thanks very much.

Bye

Posted by William on Thursday, 12.27.07 @ 08:35am | #43908

Thank you for this sweeeet tutorial....

Posted by Harshi on Thursday, 11.8.07 @ 16:36pm | #36631

I try to click Login button more than one times, old error message is not clear, new error message is show in a new line.

Posted by ndlinh on Saturday, 09.29.07 @ 13:25pm | #30470

nice you.

Posted by M.Liang Liu on Saturday, 07.14.07 @ 09:19am | #21260

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.