Skip to Main Content
Jakarta Struts Cookbook
book

Jakarta Struts Cookbook

by Bill Siggelkow
February 2005
Intermediate to advanced content levelIntermediate to advanced
528 pages
12h 53m
English
O'Reilly Media, Inc.
Content preview from Jakarta Struts Cookbook

6.1. Creating a Base Action

Problem

You want to implement and enforce a common feature or behavior across all of your Actions.

Solution

Implement an abstract base class that incorporates the behavior in its execute( ) method. As shown by the class in Example 6-1, the execute( ) method calls abstract methods that are implemented by subclasses.

Example 6-1. Abstract base action

package com.oreilly.strutsckbk.ch06;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public abstract class BaseAction extends Action {
    
    public ActionForward execute( ActionMapping mapping, 
                                  ActionForm form,
                                  HttpServletRequest request, 
                                  HttpServletResponse response) 
        throws Exception { executeBefore( );

        // call the abstract method
        ActionForward forward = executeAction( mapping, form, 
        request, response );

        executeAfter( );

        return forward;
    }        

    protected abstract ActionForward executeAction( ActionMapping mapping, 
                                                       ActionForm form,
                                               HttpServletRequest request, 
                                              HttpServletResponse response)
                                     throws Exception;
    private void executeBefore( ) {
        //Real stuff goes here
    }

    private void executeAfter( ) {
        //Real stuff goes here
    }
    protected CommonServices getCommonServices( ) {...}
}

Discussion

For some applications, you may want to apply global business rules any time a user accesses the application, submits a form, or ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming Jakarta Struts

Programming Jakarta Struts

Chuck Cavaness
Beginning Spring Framework 2

Beginning Spring Framework 2

Bruce Snyder, Sing Li, Anne Horton, Thomas Van de Velde, Naveen Balani, Christian Dupuis
Java Cookbook

Java Cookbook

Ian F. Darwin
Struts 2 in Action

Struts 2 in Action

J. Scott Stanlick, Chad Michael Davis, Donald J. Brown

Publisher Resources

ISBN: 059600771XSupplemental ContentErrata Page