📄 requestutils.java
字号:
/*
* $Header: /sfroot/cvs/esimple/src/core/org/apache/struts/util/RequestUtils.java,v 1.1.1.1 2004/09/08 06:38:44 lava Exp $
* $Revision: 1.1.1.1 $
* $Date: 2004/09/08 06:38:44 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Struts", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.struts.util;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.ActionServletWrapper;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.action.DynaActionFormClass;
import org.apache.struts.action.RequestProcessor;
import org.apache.struts.config.ActionConfig;
import org.apache.struts.config.FormBeanConfig;
import org.apache.struts.config.ForwardConfig;
import org.apache.struts.config.ModuleConfig;
import org.apache.struts.taglib.html.Constants;
import org.apache.struts.upload.MultipartRequestHandler;
import org.apache.struts.upload.MultipartRequestWrapper;
import com.esimple.framework.web.action.BaseForm;
/**
* General purpose utility methods related to processing a servlet request
* in the Struts controller framework.
*
* @author Craig R. McClanahan
* @author Ted Husted
* @author James Turner
* @author David Graham
* @version $Revision: 1.1.1.1 $ $Date: 2004/09/08 06:38:44 $
*/
public class RequestUtils {
// ------------------------------------------------------- Static Variables
/**
* Commons Logging instance.
*/
protected static Log log = LogFactory.getLog(RequestUtils.class);
/**
* The message resources for this package.
*/
private static MessageResources messages =
MessageResources.getMessageResources("org.apache.struts.util.LocalStrings");
/**
* The context attribute under which we store our prefixes list.
*/
private static final String PREFIXES_KEY = "org.apache.struts.util.PREFIXES";
/**
* Java 1.4 encode method to use instead of deprecated 1.3 version.
*/
private static Method encode = null;
/**
* Maps lowercase JSP scope names to their PageContext integer constant values.
*/
private static Map scopes = new HashMap();
/**
* Initialize the encode variable with the 1.4 method if available. Also set up the
* scope map values.
*/
static {
try {
// get version of encode method with two String args
Class[] args = new Class[] { String.class, String.class };
encode = URLEncoder.class.getMethod("encode", args);
} catch (NoSuchMethodException e) {
log.debug("Could not find Java 1.4 encode method. Using deprecated version.", e);
}
scopes.put("page", new Integer(PageContext.PAGE_SCOPE));
scopes.put("request", new Integer(PageContext.REQUEST_SCOPE));
scopes.put("session", new Integer(PageContext.SESSION_SCOPE));
scopes.put("application", new Integer(PageContext.APPLICATION_SCOPE));
}
// --------------------------------------------------------- Public Methods
/**
* Create and return an absolute URL for the specified context-relative
* path, based on the server and context information in the specified
* request.
*
* @param request The servlet request we are processing
* @param path The context-relative path (must start with '/')
* @return absolute URL based on context-relative path
* @exception MalformedURLException if we cannot create an absolute URL
*/
public static URL absoluteURL(HttpServletRequest request, String path)
throws MalformedURLException {
return (new URL(serverURL(request), request.getContextPath() + path));
}
/**
* Return the <code>Class</code> object for the specified fully qualified
* class name, from this web application's class loader.
*
* @param className Fully qualified class name to be loaded
* @return Class object
* @exception ClassNotFoundException if the class cannot be found
*/
public static Class applicationClass(String className) throws ClassNotFoundException {
// Look up the class loader to be used
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if (classLoader == null) {
classLoader = RequestUtils.class.getClassLoader();
}
// Attempt to load the specified class
return (classLoader.loadClass(className));
}
/**
* Return a new instance of the specified fully qualified class name,
* after loading the class from this web application's class loader.
* The specified class <strong>MUST</strong> have a public zero-arguments
* constructor.
*
* @param className Fully qualified class name to use
* @return new instance of class
* @exception ClassNotFoundException if the class cannot be found
* @exception IllegalAccessException if the class or its constructor
* is not accessible
* @exception InstantiationException if this class represents an
* abstract class, an interface, an array class, a primitive type,
* or void
* @exception InstantiationException if this class has no
* zero-arguments constructor
*/
public static Object applicationInstance(String className)
throws ClassNotFoundException, IllegalAccessException, InstantiationException {
return (applicationClass(className).newInstance());
}
/**
* Compute a set of query parameters that will be dynamically added to
* a generated URL. The returned Map is keyed by parameter name, and the
* values are either null (no value specified), a String (single value
* specified), or a String[] array (multiple values specified). Parameter
* names correspond to the corresponding attributes of the
* <code><html:link></code> tag. If no query parameters are
* identified, return <code>null</code>.
*
* @param pageContext PageContext we are operating in
* @param paramId Single-value request parameter name (if any)
* @param paramName Bean containing single-value parameter value
* @param paramProperty Property (of bean named by <code>paramName</code>
* containing single-value parameter value
* @param paramScope Scope containing bean named by
* <code>paramName</code>
*
* @param name Bean containing multi-value parameters Map (if any)
* @param property Property (of bean named by <code>name</code>
* containing multi-value parameters Map
* @param scope Scope containing bean named by
* <code>name</code>
*
* @param transaction Should we add our transaction control token?
* @return Map of query parameters
* @exception JspException if we cannot look up the required beans
* @exception JspException if a class cast exception occurs on a
* looked-up bean or property
*/
public static Map computeParameters(
PageContext pageContext,
String paramId,
String paramName,
String paramProperty,
String paramScope,
String name,
String property,
String scope,
boolean transaction)
throws JspException {
// Short circuit if no parameters are specified
if ((paramId == null) && (name == null) && !transaction) {
return (null);
}
// Locate the Map containing our multi-value parameters map
Map map = null;
try {
if (name != null) {
map = (Map) lookup(pageContext, name, property, scope);
}
} catch (ClassCastException e) {
saveException(pageContext, e);
throw new JspException(messages.getMessage("parameters.multi", name, property, scope));
} catch (JspException e) {
saveException(pageContext, e);
throw e;
}
// Create a Map to contain our results from the multi-value parameters
Map results = null;
if (map != null) {
results = new HashMap(map);
} else {
results = new HashMap();
}
// Add the single-value parameter (if any)
if ((paramId != null) && (paramName != null)) {
Object paramValue = null;
try {
paramValue = lookup(pageContext, paramName, paramProperty, paramScope);
} catch (JspException e) {
saveException(pageContext, e);
throw e;
}
if (paramValue != null) {
String paramString = null;
if (paramValue instanceof String) {
paramString = (String) paramValue;
} else {
paramString = paramValue.toString();
}
Object mapValue = results.get(paramId);
if (mapValue == null) {
results.put(paramId, paramString);
} else if (mapValue instanceof String) {
String newValues[] = new String[2];
newValues[0] = (String) mapValue;
newValues[1] = paramString;
results.put(paramId, newValues);
} else /* if (mapValue instanceof String[]) */ {
String oldValues[] = (String[]) mapValue;
String newValues[] = new String[oldValues.length + 1];
System.arraycopy(oldValues, 0, newValues, 0, oldValues.length);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -