Views in Struts

Views present information from the model. The typical view architecture used with Struts is JSP, but in fact, any servlet—that is, any path mapped by the container to which Struts can forward control—can serve as a view. We will stick to JSP because it is so common, but increasingly developers are becoming interested in view frameworks with simpler syntax or other niceties, such as Velocity. Thankfully, Struts can still be used if you decide to pursue these alternatives.

The interesting question regarding views is how they communicate with Actions and the request processor. Views need to transfer data in two ways. First, they must be able to pick up data that is provided to them by Actions. This part is easy: an Action puts objects into request or session scope, just as any servlet would; references to these objects may then be retrieved by the JSP from request or session scope directly through Java scriplets referencing the HttpServletRequest or HttpSession objects or through tags, such as those provided by JSTL (JSP Standard Tag Library) or the Struts Tag Libraries (more on this topic later in this chapter).

The second requirement for data transfer in the view is to obtain values that should be displayed in a form. Enter the ActionForm.

The ActionForm Class

As we discuss the ActionForm class, please refer to Figure 19-2. The key to understanding ActionForms is to notice that they are situated at a number of places in the request/response lifecycle. In particular, ...

Get Java Enterprise in a Nutshell, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.