ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

What Is Struts
Pages: 1, 2, 3, 4, 5

Choosing a Model Layer for Struts

As we said earlier, the Struts framework doesn't provide much in the way of the model, but it can integrate with any current model approach, including JDBC and EJB, as well as third-party packages like Hibernate, iBATIS, and Object Relational Bridge. One of the most important best practices for the Struts framework is that business logic should stay out of the Action class, regardless of what you're using for the Model layer. Putting business logic in the Action classes hurts reusability of the Action class and couples the framework too tightly with the business tier. Instead, the Action classes should utilize a service layer, even when using straight JDBC. Back in Example 1, for example, the authenticate() method was called on a service called Beer4AllService. This might have been a Stateless Session Bean, a RMI object, or maybe just a simple Java class that wraps JDBC code. The good news is that you're not limited with Struts.

The Struts Presentation Layer

The Struts presentation layer is almost as flexible as the model layer. You can (and most users do) use JavaServer Pages, of course. But you can also use one of many alternative presentation technologies if JSP isn't your thing. From Velocity Templates to XSLT to JavaServer Faces, Struts easily integrates with many.

If you are using JSP, then you're in luck, because Struts has a fairly exhaustive set of JSP custom tags.

The Venerable Struts Tag Library

The support for custom tags was added to the JSP language to allow developers to extend the JSP library. The Struts framework includes over 60 JSP custom tags grouped into one of the five libraries shown in Table 1.

Table 1. Struts includes many custom tags that many building JSP applications easier.

Tag Library


HTML Tag Library

Contains tags used to create Struts input forms, as well as other tags generally useful in the creation of HTML-based user interfaces.

Bean Tag Library

Contains tags useful for accessing beans and their properties, as well as defining new beans.

Logic Tag Library

Contains tags that are useful in managing conditional generation of output text, looping over object collections for repetitive generation of output text, and application flow management.

Nested Tag Library

Contains tags useful for dealing with object graphs and nested properties.

Tiles Tag Library

Tiles builds on the "include" feature provided by the JavaServer Pages specification to provide a full-featured, robust framework for assembling presentation pages from component parts.

The Struts framework also works quite nicely with the JSP Standard Tag Library (JSTL), which was created to provide a set of standard tags that will work in all compliant JSP containers.

Presentation Validation

Another great feature of Struts is the presentation validation that's included with the Commons Validator. The Validator framework was created by David Winterfeldt and integrated into Struts in version 1.1. The Validator provides for presentation validation (and with some customization, business validation) by declaring the rules in an external XML file. The framework comes with many pre-built validation rules, like checking for required fields, max and min lengths, and date validations. It also supports regular expressions. You can even create you own validation rules and easily add them to your application.

Pages: 1, 2, 3, 4, 5

Next Pagearrow