19.2. Struts: An Application ToolkitFrom the preceding discussion it is clear that a great deal of new infrastructure is needed to support controllers. There must be some means to associate form beans and controller actions with forms. These controller actions must know where to send users after successfully completing an action. There also must be a mechanism to send validation errors to users. The Java classes to implement the controller actions must also be written. This is a lot of work, but much of it has already been done by a toolkit called struts. Struts is much more than a way to build controllers; it is a complete application framework containing custom tags, a controller framework, and much more. Among the many other services it provides, struts adds another layer between data and presentation. Up until now content on a page could come from one of two places: either hard-coded in the page or from a bean. A typical example is the CD database from Chapter 18, where the name of the artist was provided by a bean, but the preceding string "Albums by: " was in the page itself. Struts takes the approach that the only elements that should be part of a JSP are the structural ones such as table cells, paragraph breaks, and so on. All other text, such as messages to users, labels for form elements, and so on, should live in a common file separate from all JSPs. Separating content from structure ensures some level of consistency because a message used on several pages is defined in one place. It also makes it easier to make changes because there is no question about where to find a particular message. Most importantly, isolating all of a site's text in one file makes it possible to easily support multiple languages and locales. A site might have multiple versions of such a fileone for English that contains message.entry=Welcome message.departure=Goodbye and another for German that contains message.entry=Willkommen message.departure=Auf Wiedersehen Using tags from the struts library or a utility class, a developer can just refer to "message.entry," and the appropriate text will be retrieved depending on whether the location has been set to an English-speaking or German-speaking locale. Note that the dots in the names do not necessarily imply that there is any sort of hierarchy as the dots in bean properties do. Here the dots are just a convenient way to mentally group messages into convenient units.
|