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
 
Struts 2 Validation Example 
 

Validation Login application In this section we will write the code to validate the login application. After completing this section you will be able to write validations for your Struts 2 projects. The concepts defined in this section are so illustrativ

 

Struts 2 Validation Example

                         

Validation Login application
In this section we will write the code to validate the login application. After completing this section you will be able to write validations for your Struts 2 projects. The concepts defined in this section are so illustrative that a learner quickly develops his/her skills in Struts 2 framework.

Struts 2 is very elegant framework that provides a lot of functionality to develop web based applications quickly. Here you will learn to write the form validation code in Struts 2 very easily. We will add the form validation code in our login application.

For validation the login application java script can be added to the jsp page or in action class, but Struts 2 provides another very easy method to validate your fields automatically. You can even use the same configuration file to generate client side script ( in next section we will see how to generate client side validation code).

The Struts 2 validation framework uses xml based configuration file. The file name should be <Your action class> -validation.xml. In our case our action class name is Login.java, so our validation configuration file will be Login-validation.xml. The Login-validation.xml will be saved into "webapps\struts2tutorial\WEB-INF\src\java\net\roseindia" directory. Here is the content of Login-validation.xml file:

<?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="username">

		<field-validator type="requiredstring">

			<param name="trim">true</param>

			<message>Login name is required</message>

		</field-validator>

	</field>

	<field name="password">

		<field-validator type="requiredstring">

			<param name="trim">true</param>

			<message>Password is required</message>

		</field-validator>

	</field>

        </validators>
      

 In the above configuration file the field name corresponds to the ActionForm properties. For the username and password elements the requiredstring validation is applied and the message in the <message>....</message> tag is used to display the message if validation fails.

Compiling the application

To compile the application go to "\webapps\struts2tutorial\WEB-INF\src" directory and type ant command. The ant tool will compile the application for you.

Adding the link into index.html

Finally we add the link in the index.html to access the login form. 

<ul>
<li><a href="roseindia/showLogin.action">Login Application</a></li>
</ul>

In the next section we will run and test the application.

                         

» 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

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

i have an issue on struts validation. i have created a Action-validation.xml file there i have defined 2 fileds as requiredstring. but in some pages i do not want to validate two fileds i want validation to be done for one field

Posted by sorup on Wednesday, 12.17.08 @ 07:52am | #82873

I have three to four Option Button how can i Valiate the selected button

Posted by subash on Monday, 09.8.08 @ 17:20pm | #78017

I am getting an error with JBoss 5 RC1 :
URI scheme is not "file"

Posted by bob on Sunday, 09.7.08 @ 10:51am | #77685

I lost my theme when the validation result is false.

How can I force to have a single theme for the app?

Posted by juan pablo on Tuesday, 03.4.08 @ 03:01am | #51223

Its really helpful

Posted by vajahat ali on Monday, 01.28.08 @ 16:57pm | #46404

Thanks yar, Its really helpful meterial, Chak dey RoseIndia!!!

Vajahat,Lahore, Pakistan

Posted by Vj Ali on Monday, 01.28.08 @ 16:50pm | #46401

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.