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

advertisement

AddThis Social Bookmark Button JSP and Servlets

Learning Jakarta Struts, Part 3

11/14/2001

This is the final article in a three-part series on the Struts framework.

In the first article, Introduction to Jakarta Struts Framework, I defined the Struts framework, discussed what it can accomplish, and provided an overview of the various components used throughout the framework. In the second article, Learning Jakarta Struts, I described building a simple sample application from scratch using Struts 1.0. This article will show you how to use the Struts tags to access the ApplicationResource file from a JSP.

The action classes are the link between the Struts framework and your business application logic. You tend to want to keep action classes as thin as possible, primarily because the true logic in the application should be in a separate logic tier. If you are doing n-tier application development, you want to keep the interfaces as clean as possible between the tiers. The fact that the main method in the action class is called "perform" indicates that the action classes should do something. Every action needs to be extended from org.apache.struts.action.Action. For a small application, it is possible to do just that, extend action; however, I've found that there are usually common features for your action classes in a given application. It is therefore a better design (in my opinion) to create a base class used by all actions in the application. I've set the StrutsSample application up this way, with a typical method that might be of use in an application action base class as well as some samples of general forward definitions.

package com.oreilly.actions;
import java.io.IOException;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.MissingResourceException;
import java.util.Enumeration;
import java.util.Properties;
import java.rmi.RemoteException;
import javax.ejb.EJBHome;
import javax.ejb.CreateException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

This is the base class from which all action classes that use Struts can be derived. It takes into consideration some general architecture that would most likely be needed in a real application. For the purpose of this article, those methods that are not directly related to the Struts framework will be black-boxed and commented so that you can use this as a skeleton and fill in those methods as you see fit while you are doing development. All action classes need to be derived from org.apache.struts.action.Action.

public abstract class AbstStrutsActionBase extends Action
{
/* Generic typical returns used so that the structs-config.xml can
* default global forwards.
*/


protected static final String SUCCESS = "success";
protected static final String FAILURE = "failure";
protected static final String ERROR = "error";
protected static final String LOGIN = "login";
protected static final String CONFIRM = "confirm";h
protected Context jndiContext = null;


/**
* Default constructor
*/

public AbstStrutsActionBase()
{
}

/**

This is a blackbox method -- lookup the EJB instance necessary. Typically, action classes work with EJB session beans (or just javabeans) that contain the business logic for your application. In large scale projects, you want to keep a clean seperation of tiers. You would get the JNDI context, get an instance of the context, and then do a lookup on the given JNDI name for the EJB you are interested in to retrieve the home interface. This is provided as just a sample of what is needed.

  • Param String containing JNDI name to look up.
  • Return Object containing the home object looked up.
  • Throws NamingException if the name lookup fails.
  • Throws MissingResourceException if can't get the resource bundle.

*/

public Object lookup(String jndiName) throws NamingException, MissingResourceException
{
//Set up the JNDI properties to get the initial context for calls to EJB objects
if (jndiContext == null) {
ResourceBundle resource = ResourceBundle.getBundle("strutssample.properties");
Properties properties = new Properties();
properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, resource.getString(Context.INITIAL_CONTEXT_FACTORY));
properties.setProperty(Context.PROVIDER_URL, resource.getString(Context.PROVIDER_URL));
properties.setProperty(Context.SECURITY_PRINCIPAL, resource.getString(Context.SECURITY_PRINCIPAL));
properties.setProperty(Context.SECURITY_CREDENTIALS, resource.getString(Context.SECURITY_CREDENTIALS));
jndiContext = new InitialContext(properties);
}

Note: For production, you probably want a robust try/catch block here to log any errors or important information. For this sample, any exceptions will just be thrown and we'll return the home object.

return (jndiContext.lookup(jndiName));
}

Also in JSP and Servlets:

Learning the New Jakarta Struts 1.1, Part 2

Learning the New Jakarta Struts 1.1, Part 1

JSP Standard Tag Libraries, Part 2

This is the main action called from the Struts framework. You can have this method call an abstract performAction method that is then implemented by each action class, and do any specifics in this method that might be common to all actions, like logging. For this example, we are simply using the perform as the asbstract class.

  • Param mapping: The ActionMapping used to select this instance.
  • Param actionForm: The optional ActionForm bean for this request (if any).
  • Param request: The HTTP request we are processing.
  • Param response: The HTTP response we are creating.
  • Throws IOException if an input/output error occurs.
  • Throws ServletException if a servlet exception occurs.
  • Return location where control will be forwarded after this request is processed.

public abstract ActionForward perform(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException;
}

/*


Pages: 1, 2, 3

Next Pagearrow