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

advertisement

AddThis Social Bookmark Button

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

Overview of the Struts Framework

The Struts framework is composed of approximately 300 classes and interfaces organized in about 12 top-level packages. Besides the utility and helper classes, much of the framework consists of classes and interfaces for working with either the controller functionality or the presentation through custom tag libraries. The choice of a model layer is a decision that's left up to you. Some argue that this is a deficiency in the framework, while seasoned Struts developers would say that this freedom is warranted and welcomed. A high-level view of the Struts architecture is shown in Figure 3.



Figure 3
Figure 3. The Struts architecture

In 2002, I put together a presentation for the Atlanta Java Users Group (AJUG) on Struts. In that presentation, I came up with a facetious business model called Beer4All that included an e-commerce web site called beer4all.com. (Back then, I actually owned the domain name. Now it's been taken over by cretins who want to make money by selling the domain name.) You can find the original presentation that included the Beer4All example at www.ajug.org/meetings/download/struts.pdf. I'm going to refer to the Beer4All here for a few of the examples.

The Struts Controller Components

When a request is sent to a Struts application, it's handled by the Struts ActionServlet. The Struts framework includes a concrete ActionServlet that for many users is adequate and requires no customization or additional work.

When the ActionServlet receives a request, it inspects the URL and based on the Struts configuration files, it delegates the handling of the request to an Action class. As you can see from Figure 3, the Action class is part of the controller and is responsible for communicating with the model layer. The Struts framework provides an abstract Action class that you must extend for your own needs.

Example 1 shows you an example of an Action class from the Beer4All Struts application that extends the base Action class.

Example 1. The Beer4All LoginAction class is called by the ActionServlet when users attempt to log in to the site.

public class LoginAction extends Action {
    /**
     * Called by the ActionServlet when the a user attempts to login.
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        /*
         * Get the user's login name and password. They should have been
         * validated by the ActionForm.
         */
        String username = ((LoginForm) form).getUsername();
        String password = ((LoginForm) form).getPassword();

        // Login through the security service
        Beer4AllService serviceImpl = this.getBeer4AllService();

        // Authenticate the user with the backend
        UserView userView = serviceImpl.authenticate(username, password);

        // Invalide the current session and create a new one
        HttpSession session = request.getSession(false);
        session.invalidate();
        session = request.getSession(true);

        // Store the user object into the session
        session.setAttribute("USER", userView);

        return mapping.findForward("success");
    }
}

The Action class in Example 1 is called when a user attempts to log in to the Beer4All website, probably to order their next shipment of ale. The URL that the browser would have sent to the ActionServlet would have looked something like http://beer4all.com/login.do. When the ActionServlet gets this request, it consults its mapping (which was loaded from the Struts configuration files at startup) and discovers that the LoginAction is the Action class that handles the login.do request. The ActionServlet then calls the Action's execute() method to perform whatever task the Action class was meant to perform. Within the execute() method, you can put in whatever logic you need in order to fulfill the request. For example, you could acquire a Hibernate session and query the database, invoke a Stateless Session Bean (SLSB) or maybe use JavaMail to send an email to a registered user, just to name a few.

The LoginAction from Example 1 authenticates the user through some service (the details of which are not shown), presumably to a database. When the Action class is finished performing its logic, it typically stores objects (JavaBeans) into the request, session, or even application scope, and informs the controller servlet whether it was successful or not. Based on the mappings for the LoginAction class, the ActionServlet will determine the next page the user should be shown. Because the JSP has access to the same request, session, or application scope, it can access those stored objects and use them to render the view for the client.

The Struts framework includes several pre-built Actions that make it easier to build out functionality. You don't always have to map a web request to a separate Action class. The org.apache.struts.actions.DispatchAction, for example, is designed to support multiple requests. In the Beer4All application, we have the typical e-commerce shopping cart functionality (view the cart, add items, checkout, etc.). Instead of creating separate Action classes for each action, we can use the DispatchAction to contain all of the methods that work on the user's cart. Using the DispatchAction class keeps the number of Action classes to a minimum, which helps to simplify the maintenance of the application.

Pages: 1, 2, 3, 4, 5

Next Pagearrow