Ajax validation with Struts 2

Support for Ajax and JavaScript takes the pain out of Web-form validation

Writing code to validate Web-form input can be even more of a chore than implementing form-processing logic. But help is at hand, thanks to the Struts 2 framework. Oleg Mikheev looks under the hood of the Struts 2 validation mechanism and shows you how its Java, JavaScript, and Ajax support can take the pain out of Web-form validation.

Web applications often require user input, which can range from simple username/password values to data entered into a complex form with dependent fields. The task of validating Web-form input is often more complex than implementing the logic to execute on the data after it's submitted. A validation framework can help simplify validation coding -- and the more complex your validation rules, the more pain the framework can spare you. Apache Struts was one of the first Web application frameworks designed to automate Web forms processing, and it is the best-known. This article explores the powerful form-validation options offered by Struts' successor, Struts 2. I'll delve briefly into Struts 2's underlying validation architecture, then show you how the framework supports both server- and client-side validation, including advanced Ajax validation. The article features an example Web application with full source code and binaries ready for deployment into a servlet container.

Struts 2's validation architecture

Struts 2 is based on WebWork, another powerful but less popular Web application framework. Struts 2 shares WebWork's approach to validation. The only difference is that WebWork relies on the validation framework in XWork 1, while Struts 2 uses the improved and customized XWork 2. XWork is a comprehensive command-pattern framework that does a huge part of the work related to Struts 2 configuration, instantiation, and runtime processing. (Although XWork is mostly known for its connection with WebWork and Struts 2, it can be used as a separate framework.)

The key component in XWork is Action. Action is basically a class that contains the code that you want to execute on a specific request originating from the browser. Another important XWork component is Interceptor. As its name suggests, Interceptor intercepts calls to Action to do more processing on them -- a mechanism quite similar to the concept of aspect-oriented programming (AOP). XWork comes with a number of already implemented interceptors, and Struts 2 adds some more of its own. You are free to implement your own custom interceptors too.

Struts 2 actions and interceptors are configured in a struts.xml file. Struts 2 looks for struts.xml in WEB-INF/classes (unlike Struts 1, which stores its struts.xml in the WEB-INF folder). Configurations can be grouped in a package -- another XWork component that represents a logical configuration unit that groups other components. Packages can be extended and overridden by "sub" packages. In most cases you won't create your Struts 2 configuration from scratch; instead it will extend the default configuration that is stored in the struts-default package. The Struts 2 default configuration is stored in the struts-core.jar archive's root folder in the struts-default.xml file. You can examine this file if you are interested in deeper exploration of Struts 2 defaults and internals.

Struts 2 interceptors can be grouped into an InterceptorStack. The struts-default package defines a number of interceptor stacks, but in most cases you'll use the defaultStack. Struts 2 validation is handled by a validation interceptor that is included in the defaultStack.

1 2 3 4 5 6 Page 1
Page 1 of 6