?? tagutils.java
字號:
/*
* $Id: TagUtils.java 384235 2006-03-08 15:18:11Z niallp $
*
* Copyright 1999-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.taglib;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyContent;
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.ActionErrors;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.config.ForwardConfig;
import org.apache.struts.config.ModuleConfig;
import org.apache.struts.taglib.html.Constants;
import org.apache.struts.util.MessageResources;
import org.apache.struts.util.ModuleUtils;
import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.ResponseUtils;
/**
* Provides helper methods for JSP tags.
*
* @version $Rev: 384235 $
* @since Struts 1.2
*/
public class TagUtils {
/**
* The Singleton instance.
*/
private static final TagUtils instance = new TagUtils();
/**
* Commons logging instance.
*/
private static final Log log = LogFactory.getLog(TagUtils.class);
/**
* The message resources for this package.
* TODO We need to move the relevant messages out of this properties file.
*/
private static final MessageResources messages =
MessageResources.getMessageResources("org.apache.struts.taglib.LocalStrings");
/**
* Maps lowercase JSP scope names to their PageContext integer constant
* values.
*/
private static final Map scopes = new HashMap();
/**
* Initialize the scope names map.
*/
static {
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));
}
/**
* Constructor for TagUtils.
*/
protected TagUtils() {
super();
}
/**
* Returns the Singleton instance of TagUtils.
*/
public static TagUtils getInstance() {
return instance;
}
/**
* 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 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) TagUtils.getInstance().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 =
TagUtils.getInstance().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 oldValues[] = (String[]) mapValue;
String newValues[] = new String[oldValues.length + 1];
System.arraycopy(oldValues, 0, newValues, 0, oldValues.length);
newValues[oldValues.length] = paramString;
results.put(paramId, newValues);
} else {
String newValues[] = new String[2];
newValues[0] = mapValue.toString();
newValues[1] = paramString;
results.put(paramId, newValues);
}
}
}
// Add our transaction control token (if requested)
if (transaction) {
HttpSession session = pageContext.getSession();
String token = null;
if (session != null) {
token = (String) session.getAttribute(Globals.TRANSACTION_TOKEN_KEY);
}
if (token != null) {
results.put(Constants.TOKEN_KEY, token);
}
}
// Return the completed Map
return (results);
}
public String computeURL(
PageContext pageContext,
String forward,
String href,
String page,
String action,
String module,
Map params,
String anchor,
boolean redirect)
throws MalformedURLException {
return this.computeURLWithCharEncoding(
pageContext,
forward,
href,
page,
action,
module,
params,
anchor,
redirect,
false);
}
/**
* Compute a hyperlink URL based on the <code>forward</code>,
* <code>href</code>, <code>action</code> or <code>page</code> parameter
* that is not null.
* The returned URL will have already been passed to
* <code>response.encodeURL()</code> for adding a session identifier.
*
* @param pageContext PageContext for the tag making this call
*
* @param forward Logical forward name for which to look up
* the context-relative URI (if specified)
* @param href URL to be utilized unmodified (if specified)
* @param page Module-relative page for which a URL should
* be created (if specified)
* @param action Logical action name for which to look up
* the context-relative URI (if specified)
*
* @param params Map of parameters to be dynamically included (if any)
* @param anchor Anchor to be dynamically included (if any)
*
* @param redirect Is this URL for a <code>response.sendRedirect()</code>?
* @return URL with session identifier
* @exception java.net.MalformedURLException if a URL cannot be created
* for the specified parameters
*/
public String computeURLWithCharEncoding(
PageContext pageContext,
String forward,
String href,
String page,
String action,
String module,
Map params,
String anchor,
boolean redirect,
boolean useLocalEncoding)
throws MalformedURLException {
return computeURLWithCharEncoding(
pageContext,
forward,
href,
page,
action,
module,
params,
anchor,
redirect,
true,
useLocalEncoding);
}
public String computeURL(
PageContext pageContext,
String forward,
String href,
String page,
String action,
String module,
Map params,
String anchor,
boolean redirect,
boolean encodeSeparator)
throws MalformedURLException {
return computeURLWithCharEncoding(
pageContext,
forward,
href,
page,
action,
module,
params,
anchor,
redirect,
encodeSeparator,
false
);
}
/**
* Compute a hyperlink URL based on the <code>forward</code>,
* <code>href</code>, <code>action</code> or <code>page</code> parameter
* that is not null.
* The returned URL will have already been passed to
* <code>response.encodeURL()</code> for adding a session identifier.
*
* @param pageContext PageContext for the tag making this call
*
* @param forward Logical forward name for which to look up
* the context-relative URI (if specified)
* @param href URL to be utilized unmodified (if specified)
* @param page Module-relative page for which a URL should
* be created (if specified)
* @param action Logical action name for which to look up
* the context-relative URI (if specified)
*
* @param params Map of parameters to be dynamically included (if any)
* @param anchor Anchor to be dynamically included (if any)
*
* @param redirect Is this URL for a <code>response.sendRedirect()</code>?
* @param encodeSeparator This is only checked if redirect is set to false (never
* encoded for a redirect). If true, query string parameter separators are encoded
* as >amp;, else & is used.
* @param useLocalEncoding If set to true, urlencoding is done on the bytes of
* character encoding from ServletResponse#getCharacterEncoding. Use UTF-8
* otherwise.
* @return URL with session identifier
* @exception java.net.MalformedURLException if a URL cannot be created
* for the specified parameters
*/
public String computeURLWithCharEncoding(
PageContext pageContext,
String forward,
String href,
String page,
String action,
String module,
Map params,
String anchor,
boolean redirect,
boolean encodeSeparator,
boolean useLocalEncoding)
throws MalformedURLException {
String charEncoding = "UTF-8";
if(useLocalEncoding){
charEncoding = pageContext.getResponse().getCharacterEncoding();
}
// TODO All the computeURL() methods need refactoring!
// Validate that exactly one specifier was included
int n = 0;
if (forward != null) {
n++;
}
if (href != null) {
n++;
}
if (page != null) {
n++;
}
if (action != null) {
n++;
}
if (n != 1) {
throw new MalformedURLException(messages.getMessage("computeURL.specifier"));
}
// Look up the module configuration for this request
ModuleConfig moduleConfig = instance.getModuleConfig(module, pageContext);
// Calculate the appropriate URL
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -