concept ActionSupport in category java

appears as: ActionSupport
Struts 2 in Action

This is an excerpt from Manning's book Struts 2 in Action.

@Result( value="/chapterTwo/AnnotatedNameCollector.jsp" )

public class AnnotatedNameCollector extends ActionSupport {

   /* EMPTY */

}

There’s more going on than we currently need to know, but we’ll give the short version for now. This class extends ActionSupport. ActionSupport is a frameworkprovided implementation of the Action interface we mentioned earlier. In terms of action logic, it does nothing but render the result we’ve defined. It does provide some support to help some common tasks, which we’ll learn about in chapter 3. For now, just note that it’s a convenient helper class. Also, since it implements the Action interface for us, we can drop the class-naming convention and the framework will still pick this up while scanning for actions.

In this section, we’re going to introduce the ActionSupport class, a convenience class that provides default implementations of the Action interface and several other useful interfaces, giving us such things as data validation and localization of error messages. This convenience class is a perfect example of the Struts 2 straight and narrow we spoke of a bit earlier. The framework doesn’t force you to use this, but it’s a good idea to use it when learning the framework. In fact, it’s pretty much always a good idea to use it unless you have reason not to.

Following in the tradition of “support” classes, ActionSupport provides default implementations of several important interfaces. If your actions extend this class, they automatically gain the use of these implementations. This alone makes this class worth learning. However, the implementations provided by this class also provide a great case study in how to make an action cooperate with interceptors to achieve powerfully reusable solutions to common tasks. In this case, validation and text localization services are provided via a combination of interceptors and interfaces. The interceptors control the execution of the services while the actions implement interfaces with methods that are invoked by the interceptors. This important pattern will become clearer as we work through the details of ActionSupport by examining its use in our Struts 2 Portfolio application, which we’ll do a couple of pages from now.

Listing 3.6. Using ActionSupport to get the validation error messages
public void validate(){ PortfolioService ps = getPortfolioService(); if ( getPassword().length() == 0 ){ addFieldError( "password", getText("password.required") ); } if ( getUsername().length() == 0 ){ addFieldError( "username", getText("username.required") ); } if ( getPortfolioName().length() == 0 ){ addFieldError( "portfolioName", getText( "portfolioName.required" )); } if ( ps.userExists(getUsername() ) ){ addFieldError("username", getText( "user.exists")); } }

As you can see, instead of String literals, we now retrieve our message text from ActionSupport’s implementation of TextProvider. We now use the getText() method to retrieve our messages from properties files based upon a key. This layer of separation makes our message text much more manageable. Changing messages means only editing the properties file; the source code’s semantic keys never need to be changed.

ActionSupport also provides a basic internationalization solution for the localizing message text. The com.opensymphony.xwork2.LocaleProvider interface exposes a single method, getLocale(). ActionSupport implements this interface to retrieve the user’s locale based upon the locale setting sent in by the browser. You could implement your own version of this interface to search somewhere else for the locale, such as in the database. But if the browser setting is good enough for your requirements, you don’t have to do too much to achieve a basic level of internationalization.

You still retrieve your message texts as we did earlier. Even when we weren’t taking advantage of it, ActionSupport’s TextProvider implementation has been checking the locale every time it retrieves a message text for us. It does this by calling the getLocale() method of the LocaleProvider interface. With the locale in hand, the TextProvider, a.k.a. ActionSupport, tries to locate a properties file for that locale. Of course, you have to provide the properties file for the locale in question, or it will just serve up the standard English. But it’s simple to provide properties files for all locales that you wish to support. In Struts 2 Portfolio, we’re providing a Spanish properties file. The hard part is finding a translator. In order to see this in action, set your browser’s language support to Spanish and submit the registration form again, omitting one of the fields to see the error message that it provides.

As with validation, the internationalization provided by ActionSupport is relatively primitive. If it suffices for your application, great. If you need more, we’ll see how to get cutting-edge internationalization from the Struts 2 framework in chapter 11. For now, we’ve got a pretty decent start on building actions.

Figure 11.3. Our PortfolioHomePage action extends ActionSupport and thus carries a default implementation of the TextProvider onto the ValueStack.