📄 requestutils.java
字号:
/*
* $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/util/RequestUtils.java,v 1.151 2004/07/31 05:52:43 niallp Exp $
* $Revision: 1.151 $
* $Date: 2004/07/31 05:52:43 $
*
* Copyright 1999-2004 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.util;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
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.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.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
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.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.TagUtils;
import org.apache.struts.upload.MultipartRequestHandler;
import org.apache.struts.upload.MultipartRequestWrapper;
/**
* <p>General purpose utility methods related to processing a servlet request
* in the Struts controller framework.</p>
*
* @version $Revision: 1.151 $ $Date: 2004/07/31 05:52:43 $
*/
public class RequestUtils {
// ------------------------------------------------------- Static Variables
/**
* <p>Commons Logging instance.</p>
*/
protected static Log log = LogFactory.getLog(RequestUtils.class);
// --------------------------------------------------------- Public Methods
/**
* <p>Create and return an absolute URL for the specified context-relative
* path, based on the server and context information in the specified
* request.</p>
*
* @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));
}
/**
* <p>Return the <code>Class</code> object for the specified fully qualified
* class name, from this web application's class loader.</p>
*
* @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));
}
/**
* <p>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.</p>
*
* @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());
}
/**
* <p>Create (if necessary) and return an <code>ActionForm</code> instance appropriate
* for this request. If no <code>ActionForm</code> instance is required, return
* <code>null</code>.</p>
*
* @param request The servlet request we are processing
* @param mapping The action mapping for this request
* @param moduleConfig The configuration for this module
* @param servlet The action servlet
*
* @return ActionForm instance associated with this request
*/
public static ActionForm createActionForm(
HttpServletRequest request,
ActionMapping mapping,
ModuleConfig moduleConfig,
ActionServlet servlet) {
// Is there a form bean associated with this mapping?
String attribute = mapping.getAttribute();
if (attribute == null) {
return (null);
}
// Look up the form bean configuration information to use
String name = mapping.getName();
FormBeanConfig config = moduleConfig.findFormBeanConfig(name);
if (config == null) {
log.warn("No FormBeanConfig found under '" + name + "'");
return (null);
}
ActionForm instance = lookupActionForm(request, attribute, mapping.getScope());
// Can we recycle the existing form bean instance (if there is one)?
try {
if (instance != null && canReuseActionForm(instance, config)) {
return (instance);
}
} catch(ClassNotFoundException e) {
log.error(servlet.getInternal().getMessage("formBean", config.getType()), e);
return (null);
}
return createActionForm(config, servlet);
}
private static ActionForm lookupActionForm(HttpServletRequest request, String attribute, String scope)
{
// Look up any existing form bean instance
if (log.isDebugEnabled()) {
log.debug(
" Looking for ActionForm bean instance in scope '"
+ scope
+ "' under attribute key '"
+ attribute
+ "'");
}
ActionForm instance = null;
HttpSession session = null;
if ("request".equals(scope)) {
instance = (ActionForm) request.getAttribute(attribute);
} else {
session = request.getSession();
instance = (ActionForm) session.getAttribute(attribute);
}
return (instance);
}
/**
* <p>Determine whether <code>instance</code> of <code>ActionForm</code> is
* suitable for re-use as an instance of the form described by
* <code>config</code>.</p>
* @param instance an instance of <code>ActionForm</code> which was found,
* probably in either request or session scope.
* @param config the configuration for the ActionForm which is needed.
* @return true if the instance found is "compatible" with the type required
* in the <code>FormBeanConfig</code>; false if not, or if <code>instance</code>
* is null.
* @throws ClassNotFoundException if the <code>type</code> property of
* <code>config</code> is not a valid Class name.
*/
private static boolean canReuseActionForm(ActionForm instance, FormBeanConfig config)
throws ClassNotFoundException
{
if (instance == null) {
return (false);
}
boolean canReuse = false;
String formType = null;
String className = null;
if (config.getDynamic()) {
className = ((DynaBean) instance).getDynaClass().getName();
canReuse = className.equals(config.getName());
formType = "DynaActionForm";
} else {
Class configClass = applicationClass(config.getType());
className = instance.getClass().getName();
canReuse = configClass.isAssignableFrom(instance.getClass());
formType = "ActionForm";
}
if (log.isDebugEnabled()) {
log.debug(
" Can recycle existing "
+ formType
+ " instance "
+ "of type '"
+ className
+ "'?: "
+ canReuse);
log.trace(" --> " + instance);
}
return (canReuse);
}
/**
* <p>Create and return an <code>ActionForm</code> instance appropriate
* to the information in <code>config</code>.</p>
*
* <p>Does not perform any checks to see if an existing ActionForm exists
* which could be reused.</p>
*
* @param config The configuration for the Form bean which is to be created.
* @param servlet The action servlet
*
* @return ActionForm instance associated with this request
*/
public static ActionForm createActionForm(FormBeanConfig config, ActionServlet servlet)
{
if (config == null)
{
return (null);
}
ActionForm instance = null;
// Create and return a new form bean instance
try {
instance = config.createActionForm(servlet);
if (log.isDebugEnabled()) {
log.debug(
" Creating new "
+ (config.getDynamic() ? "DynaActionForm" : "ActionForm")
+ " instance of type '"
+ config.getType()
+ "'");
log.trace(" --> " + instance);
}
} catch(Throwable t) {
log.error(servlet.getInternal().getMessage("formBean", config.getType()), t);
}
return (instance);
}
/**
* <p>Look up and return current user locale, based on the specified parameters.</p>
*
* @param request The request used to lookup the Locale
* @param locale Name of the session attribute for our user's Locale. If this is
* <code>null</code>, the default locale key is used for the lookup.
* @return current user locale
* @since Struts 1.2
*/
public static Locale getUserLocale(HttpServletRequest request, String locale) {
Locale userLocale = null;
HttpSession session = request.getSession(false);
if (locale == null) {
locale = Globals.LOCALE_KEY;
}
// Only check session if sessions are enabled
if (session != null) {
userLocale = (Locale) session.getAttribute(locale);
}
if (userLocale == null) {
// Returns Locale based on Accept-Language header or the server default
userLocale = request.getLocale();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -