Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3
Info

This tutorial assumes you've completed the Processing Forms tutorial and have a working Form_Processing_Struts2_Ant (or Form_Processing_Struts2_Mvn) form_processing project. The example code for this tutorial, Form_Validation_Struts2_Ant or Form_Validation_Struts2_Mvn, is available on Google Code - http://code.google.com/p/struts2-examples/downloads/list. After downloading and unzipping the file, you'll have a folder named Form_Validation_Struts2_Ant (or Form_Validation_Struts2_Mvn). In that folder will be a README.txt file with instructions on now to build and run the example application.form_validation, is available for checkout from the Struts 2 GitHub repository: https://github.com/apache/struts-examples.

Introduction

In this tutorial we'll explore using Struts 2 to validate the user's input on a form. There are two ways you can use Struts 2 to do form validation. This tutorial will cover the more basic method, where the validation is included in the Struts 2 Action class. The code provided in this tutorial may be added to the Processing Forms example or you can download this complete example from Google Code - http://code.google.com/p/struts2-examples/downloads/list.

Tip

The Struts 2 user mailing list is an excellent place to get help. If you are having a problem getting the tutorial example applications to work search the Struts 2 mailing list. If you don't find an answer to your problem, post a question on the mailing list.

...

Add the following validate method to Register.java (the Action class).

Code Block
JavajavaJava
java
titlevalidate method

	public void validate(){
		
		if ( personBean.getFirstName().length() == 0 ){	

			addFieldError( "personBean.firstName", "First name is required." );
			
		}
		
				
		if ( personBean.getEmail().length() == 0 ){	

			addFieldError( "personBean.email", "Email is required." );
			
		}
		
		if ( personBean.getAge() < 18 ){	

			addFieldError( "personBean.age", "Age is required and must be 18 or older" );
			
		}
		
		
	}

...

If any errors have been added the then Struts 2 will not proceed to call the execute method. Rather the Struts 2 framework will return "input" as the result of calling the action.

...

To handle the return value of "input" we need to add the following result to our action node in struts.xml.

Code Block
XMLxmlXML
xml

<result name="input">/register.jsp</result>

...

So the following addFieldError method call:

Code Block
JavajavaJava
java

addFieldError( "personBean.firstName", "First name is required.")

will cause the message "First name is required" to be displayed above the firstName field on the form.

If you have made the above changes to the Processing FormForms tutorial or you have downloaded from Google Code either the Form_Validation_Struts2_Ant or Form_Validation_Struts2_Mvn projects run the application (see the README.txt in the project root folder). Click on the Please register link. On the registration form, just click the submit button and you should see:

...