?? actiondispatcher.java
字號:
/*
* $Id: ActionDispatcher.java 383720 2006-03-07 00:07:59Z niallp $
*
* Copyright 2005-2006 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.struts.actions;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;
import org.apache.struts.Globals;
/**
* <p>Action <i>helper</i> class that dispatches to a public method in an Action.</p>
* <p/>
* <p>This class is provided as an alternative mechanism to using DispatchAction
* and its various flavours and means <i>Dispatch</i> behaviour can be
* easily implemented into any <code>Action</code> without having to
* inherit from a particular super <code>Action</code>.</p>
* <p/>
* <p>To implement <i>dispatch</i> behaviour</i> in an <code>Action</code> class,
* create your custom Action as follows, along with the methods you
* require (and optionally "cancelled" and "unspecified" methods):</p>
* <p/>
* <pre>
* public class MyCustomAction extends Action {
*
* protected ActionDispatcher dispatcher
* = new ActionDispatcher(this, ActionDispatcher.MAPPING_FLAVOR);
*
* public ActionForward execute(ActionMapping mapping,
* ActionForm form,
* HttpServletRequest request,
* HttpServletResponse response)
* throws Exception {
* return dispatcher.execute(mapping, form, request, response);
* }
* }
* </pre>
* <p/>
* <p>It provides three flavours of determing the name of the method:</p>
* <p/>
* <ul>
* <li><strong>{@link #DEFAULT_FLAVOR}</strong> - uses the parameter specified in
* the struts-config.xml to get the method name from the Request
* (equivalent to <code>DispatchAction</code> <b>except</b> uses "method"
* as a default if the <code>parameter</code> is not specified
* in the struts-config.xml).</li>
* <p/>
* <li><strong>{@link #DISPATCH_FLAVOR}</strong> - uses the parameter specified in
* the struts-config.xml to get the method name from the Request
* (equivalent to <code>DispatchAction</code>).</li>
* <p/>
* <li><strong>{@link #MAPPING_FLAVOR}</strong> - uses the parameter specified in
* the struts-config.xml as the method name
* (equivalent to <code>MappingDispatchAction</code>).</li>
* <p/>
* </ul>
*
* @since Struts 1.2.7
* @version $Revision: 383720 $ $Date: 2006-03-07 00:07:59 +0000 (Tue, 07 Mar 2006) $
*/
public class ActionDispatcher {
// ----------------------------------------------------- Instance Variables
/**
* Indicates "default" dispatch flavor
*/
public static final int DEFAULT_FLAVOR = 0;
/**
* Indicates "mapping" dispatch flavor
*/
public static final int MAPPING_FLAVOR = 1;
/**
* Indicates flavor compatible with DispatchAction
*/
public static final int DISPATCH_FLAVOR = 2;
/**
* The associated Action to dispatch to.
*/
protected Action actionInstance;
/**
* Indicates dispatch <i>flavor</i>
*/
protected int flavor;
/**
* The Class instance of this <code>DispatchAction</code> class.
*/
protected Class clazz;
/**
* Commons Logging instance.
*/
protected static Log log = LogFactory.getLog(ActionDispatcher.class);
/**
* The message resources for this package.
*/
protected static MessageResources messages =
MessageResources.getMessageResources
("org.apache.struts.actions.LocalStrings");
/**
* The set of Method objects we have introspected for this class,
* keyed by method name. This collection is populated as different
* methods are called, so that introspection needs to occur only
* once per method name.
*/
protected HashMap methods = new HashMap();
/**
* The set of argument type classes for the reflected method call. These
* are the same for all calls, so calculate them only once.
*/
protected Class[] types = {
ActionMapping.class,
ActionForm.class,
HttpServletRequest.class,
HttpServletResponse.class};
// ----------------------------------------------------- Constructors
public ActionDispatcher(Action actionInstance) {
this(actionInstance, DEFAULT_FLAVOR);
}
public ActionDispatcher(Action actionInstance, int flavor) {
this.actionInstance = actionInstance;
this.flavor = flavor;
clazz = actionInstance.getClass();
}
// --------------------------------------------------------- Public Methods
/**
* Process the specified HTTP request, and create the corresponding HTTP
* response (or forward to another web component that will create it).
* Return an <code>ActionForward</code> instance describing where and how
* control should be forwarded, or <code>null</code> if the response has
* already been completed.
*
* @param mapping The ActionMapping used to select this instance
* @param form 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 Exception if an exception occurs
*/
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
// Process "cancelled"
if (isCancelled(request)) {
ActionForward af = cancelled(mapping, form, request, response);
if (af != null) {
return af;
}
}
// Identify the request parameter containing the method name
String parameter = getParameter(mapping, form, request, response);
// Get the method's name. This could be overridden in subclasses.
String name = getMethodName(mapping, form, request, response, parameter);
// Prevent recursive calls
if ("execute".equals(name) || "perform".equals(name)) {
String message =
messages.getMessage("dispatch.recursive", mapping.getPath());
log.error(message);
throw new ServletException(message);
}
// Invoke the named method, and return the result
return dispatchMethod(mapping, form, request, response, name);
}
/**
* <p>Dispatches to the target class' <code>unspecified</code> method,
* if present, otherwise throws a ServletException. Classes utilizing
* <code>ActionDispatcher</code> should provide an <code>unspecified</code>
* method if they wish to provide behavior different than
* throwing a ServletException..</p>
*/
protected ActionForward unspecified(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
// Identify if there is an "unspecified" method to be dispatched to
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -