?? abststrutsactionbase.java
字號:
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 from. 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
* @see org.apache.struts.action.Action
* @author Sue Spielman Switchback Software LLC www.switchbacksoftware.com
*
*/
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";
protected Context jndiContext = null;
/**
* Default constructor
*/
public AbstStrutsActionBase()
{
}
/*
* 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
* seperate of tier's. 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 provide as just a sample of what is needed.
*@param String containing JNDI name to lookup.
*@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 expceptions will just be thrown
// and we'll return the home object.
return (jndiContext.lookup(jndiName));
}
/**
* 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 where control will be forwarded to after this request is processed
*/
public abstract ActionForward perform(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException;
/*
{
// If you wanted to have another abstract method here, it is useful so that
// you can do things such as logging, as shown below.
ActionForward forward = null;
// Simple log to the servlet log for informational purposes
getServlet().log("AbstStrutsActionBase.perform() [Action Class: "+this.getClass().getName()+" ]");
getServlet().log("AbstStrutsActionBase.perform() [Form Class : "+(form == null ? "null" : form.getClass().getName())+" ]");
}
*/
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -