Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

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

Ads

Tutorials   
Login form in Struts2 version 2.3.16 Struts 2 Login Application Struts 2 Hello World Annotation Example Struts 2 Tutorial: Easy steps to learn Struts 2 Struts2 Application in Eclipse: Running the application in Eclipse IDE Struts 2 Hello World Application using Eclipse Interceptors in Struts 2 Struts 2 Action Tag Struts 2 in Agile Development Environment Struts 2 UI Tags Generic Tags Creating own Interceptor What are Interceptors in Struts 2 and how do they work? Results Value Stack / OGNL Action in Struts 2 Framework Struts 2 Interceptors Struts 2.3.15.1 Maven - How to use Maven dependency of Struts 2.3.15.1? Download Struts 2.3.15.1 How Struts 2 Framework works? Architecture of Struts 2 Framework Features and Enhancements in Struts 2.3.15.1 Struts 2 Tutorials - Struts version 2.3.15.1 Struts Flow Diagram Step By Step Struts Tutorial Struts 2 Login Form Example Struts 2 Hello World Example Introduction to Struts 2 Framework Downloading and Installing Struts 2.3.8 Struts 2.3.8 released with new features Struts 2.3.8 Tutorials and Examples Subset Tag Actionerror Tag Struts 2 Autocompleter,Struts 2 Autocomplete Generator Tag Struts 2 Application Struts 2 zero configuration,Struts 2 zero configuration Example Static Parameter, Static Parameter in Struts 2, Struts 2 Static Parameter Pagination Example Struts 2.2.1 Title Struts2 ElseIf Tag Example Struts2 Else Tag Example Struts2 If,Else,ElseIf Tag Example Struts2 tag refrences and Example How to download, compile and test the tutorials using ant. Struts 2.1.8 Login Form Struts 2.1.8 Hello World Example Download Struts 2.1.8, Downloading and installing Struts 2.1.8 Showcase application
Ads

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.

     

Advertisements

Ads
Share on Google+Share on Google+

Struts 2 Validation Example

Posted on: March 14, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Struts 2 Validation Example   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:2
?eref AKYÜZ
June 26, 2011
pls Helpp

Where should i save the index.html file??? pls send me an e-mail. I really need help
Hosan
May 23, 2014
Java Struts

Rose india was very helpful in understanding the concepts of struts. Guys get use of this website to develop your programming in struts2.

Ads

 

Ads