📄 requestutils.java
字号:
/*
* $Id: RequestUtils.java 471754 2006-11-06 14:55:09Z husted $
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.ActionServletWrapper;
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.upload.MultipartRequestHandler;
import org.apache.struts.upload.MultipartRequestWrapper;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
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;
/**
* <p>General purpose utility methods related to processing a servlet request
* in the Struts controller framework.</p>
*
* @version $Rev: 471754 $ $Date: 2006-11-06 08:55:09 -0600 (Mon, 06 Nov 2006) $
*/
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
* @throws 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
* @throws ClassNotFoundException if the class cannot be found
*/
public static Class applicationClass(String className)
throws ClassNotFoundException {
return applicationClass(className, null);
}
/**
* <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
* @param classLoader The desired classloader to use
* @return Class object
* @throws ClassNotFoundException if the class cannot be found
*/
public static Class applicationClass(String className,
ClassLoader classLoader)
throws ClassNotFoundException {
if (classLoader == null) {
// Look up the class loader to be used
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
* @throws ClassNotFoundException if the class cannot be found
* @throws IllegalAccessException if the class or its constructor is not
* accessible
* @throws InstantiationException if this class represents an abstract
* class, an interface, an array class, a
* primitive type, or void
* @throws InstantiationException if this class has no zero-arguments
* constructor
*/
public static Object applicationInstance(String className)
throws ClassNotFoundException, IllegalAccessException,
InstantiationException {
return applicationInstance(className, null);
}
/**
* <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
* @param classLoader The desired classloader to use
* @return new instance of class
* @throws ClassNotFoundException if the class cannot be found
* @throws IllegalAccessException if the class or its constructor is not
* accessible
* @throws InstantiationException if this class represents an abstract
* class, an interface, an array class, a
* primitive type, or void
* @throws InstantiationException if this class has no zero-arguments
* constructor
*/
public static Object applicationInstance(String className,
ClassLoader classLoader)
throws ClassNotFoundException, IllegalAccessException,
InstantiationException {
return (applicationClass(className, classLoader).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)?
if ((instance != null) && config.canReuse(instance)) {
return (instance);
}
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>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>Retrieves the servlet mapping pattern for the specified {@link ActionServlet}.</p>
*
* @return the servlet mapping
* @see Globals#SERVLET_KEY
* @since Struts 1.3.6
*/
public static String getServletMapping(ActionServlet servlet) {
ServletContext servletContext = servlet.getServletConfig().getServletContext();
return (String)servletContext.getAttribute(Globals.SERVLET_KEY);
}
/**
* <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();
}
return userLocale;
}
/**
* <p>Populate the properties of the specified JavaBean from the specified
* HTTP request, based on matching each parameter name against the
* corresponding JavaBeans "property setter" methods in the bean's class.
* Suitable conversion is done for argument types as described under
* <code>convert()</code>.</p>
*
* @param bean The JavaBean whose properties are to be set
* @param request The HTTP request whose parameters are to be used to
* populate bean properties
* @throws ServletException if an exception is thrown while setting
* property values
*/
public static void populate(Object bean, HttpServletRequest request)
throws ServletException {
populate(bean, null, null, request);
}
/**
* <p>Populate the properties of the specified JavaBean from the specified
* HTTP request, based on matching each parameter name (plus an optional
* prefix and/or suffix) against the corresponding JavaBeans "property
* setter" methods in the bean's class. Suitable conversion is done for
* argument types as described under <code>setProperties</code>.</p>
*
* <p>If you specify a non-null <code>prefix</code> and a non-null
* <code>suffix</code>, the parameter name must match
* <strong>both</strong> conditions for its value(s) to be used in
* populating bean properties. If the request's content type is
* "multipart/form-data" and the method is "POST", the
* <code>HttpServletRequest</code> object will be wrapped in a
* <code>MultipartRequestWrapper</code object.</p>
*
* @param bean The JavaBean whose properties are to be set
* @param prefix The prefix (if any) to be prepend to bean property names
* when looking for matching parameters
* @param suffix The suffix (if any) to be appended to bean property
* names when looking for matching parameters
* @param request The HTTP request whose parameters are to be used to
* populate bean properties
* @throws ServletException if an exception is thrown while setting
* property values
*/
public static void populate(Object bean, String prefix, String suffix,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -