JBoss.orgCommunity Documentation

Chapter 6. Struts Validation Examples

6.1. Starting Point
6.2. Defining the Validation Rule
6.3. Client-Side Validation
6.4. Server Side Validation
6.5. Editing the JSP File
6.6. Editing the Action
6.7. Editing the Form Bean

Validation of input is an important part of any Web application. All Apache Jakarta frameworks, including Struts, can use a common Jakarta Validation Framework for streamlining this aspect of Web application development. The Validation Framework allows you to define validation rules and then apply these rules on the client-side or the server-side.

JBoss Developer Studio makes using the Validation Framework in Struts even easier with the help of a specialized editor for the XML files that controls validation in a project. In this document, we'll show you how this all works by creating some simple client-side validation and server-side validation examples.

The example assumes that you have already created our sample "StrutsHello" application from the Getting Started Guide for Creating a Struts Application. You should have the JBoss Developer Studio perspective open on this StrutsHello project.

In these steps you will set up the validation that can be used for either client-side or server side validation. You need to enable validation as a part of the project, define an error message, and tie it into an appropriate part of the application.


  • Expand the "form-beans" node under the StrutsHelloConfigurationdefaultstruts-config.xml node. Then, drag the form bean "GetNameForm" and drop it onto a formset in the XML Editor

  • In the Validation Editor, expand the formset node, right-click GetNameForm, and select Create Field... from the context menu

  • Enter a name for Property in the dialog box. A new property will be created:


  • In the Properties view for the name field to the right of the "tree" for the validation.xml file, click on the Change...button next to the Depends entry field

  • In the displayed double list, select required from the left list and then click Add

  • Click Ok

  • Right-click name and select Add Arg... from the context menu

  • In the Add Arg dialog box, click on the Change...button next to the Key field

  • In the Key dialog box that appears now, click on the Add button

  • Enter "name.required" in the Name field, and enter a person's name in the Value field

  • Click Finish, then Ok, and then Ok again

  • Select FileSave All from the menu bar

Client-side validation uses a scripting language (like JavaScript) running in the client browser to actually do the validation. In a Struts application using the Validation Framework, however, you don't actually have to do any of the script coding. The Validation Framework handles this.

To see how this works in our application, you'll just need to make a couple of modifications to one of the JSP files.

onsubmit="return validateGetNameForm(this)"

    

The file should now look like this:


<%@ taglib uri="/WEB-INF/struts-html" prefix="html" %>

<html:html >
    <head>
        <html:javascript formName="GetNameForm"/>
	    <title></title>
    </head>
    <body>
        <html:form action="/greeting.do" onsubmit="return validateGetNameForm(this)">
            Input name:<html:text property="name"/><html:submit value="Say Hello!"/>
         </html:form>
    </body>
</html:html>

    

A JavaScript error message should be displayed in an alert box.

Server side validation does the validation inside the application on the server. In a Struts application using the Validation Framework, you still don't have to do any of the actual validation coding. The Validation Framework handles this. You will though have to make a few changes to the JSP file you modified for client-side validation along with a change to an action and a few changes to the form bean class.

The JSP file should now look like this:

<%@ taglib uri="/WEB-INF/struts-html" prefix="html" %>
<html:html >
<head>
    <html:javascript formName="GetNameForm"/>
    	<title></title>
    </head>
    <body>
        <html:form action="/greeting.do" >Input name:<html:text property="name"/>
            <html:submit value="Say Hello!"/>
        </html:form>
        <html:errors/>
    </body>
</html:html>

    

The file should now look like this:

package sample;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
public class GetNameForm extends

org.apache.struts.validator.ValidatorForm 
{

	private String name = "";
	
	/**
	* @return Returns the name.
	*/
	public String getName() 
	{
		return name;
	}
	
	/**
	* @param name The name to set.
	*/
	public void setName(String name) 
	{
		this.name = name;
	}
	
	public GetNameForm () 
	{
	}
	
	public void reset(ActionMapping actionMapping, 
		HttpServletRequest request) 
	{
		this.name = "";
	}
	
	// public ActionErrors validate(ActionMapping actionMapping,
	//	HttpServletRequest request) 
	//{
	// ActionErrors errors = new ActionErrors();
	// return errors;
	// }
    }

    

The error message should appear in a refreshed version of the form.