📄 requestutils.java
字号:
StringBuffer sb = new StringBuffer();
if(forward.getContextRelative())
{
if(!path.startsWith("/"))
sb.append("/");
sb.append(path);
return sb.toString();
}
ModuleConfig moduleConfig = (ModuleConfig)request.getAttribute("org.apache.struts.action.MODULE");
String forwardPattern = moduleConfig.getControllerConfig().getForwardPattern();
if(forwardPattern == null)
{
sb.append(moduleConfig.getPrefix());
if(!path.startsWith("/"))
sb.append("/");
sb.append(path);
}
else
{
boolean dollar = false;
for(int i = 0; i < forwardPattern.length(); i++)
{
char ch = forwardPattern.charAt(i);
if(dollar)
{
switch(ch)
{
default:
break;
case 77: // 'M'
sb.append(moduleConfig.getPrefix());
break;
case 80: // 'P'
if(!path.startsWith("/"))
sb.append("/");
sb.append(path);
break;
case 36: // '$'
sb.append('$');
break;
}
dollar = false;
}
else
if(ch == '$')
dollar = true;
else
sb.append(ch);
}
}
return sb.toString();
}
public static String pageURL(HttpServletRequest request, String page)
{
StringBuffer sb = new StringBuffer();
ModuleConfig moduleConfig = (ModuleConfig)request.getAttribute("org.apache.struts.action.MODULE");
String pagePattern = moduleConfig.getControllerConfig().getPagePattern();
if(pagePattern == null)
{
sb.append(moduleConfig.getPrefix());
sb.append(page);
}
else
{
boolean dollar = false;
for(int i = 0; i < pagePattern.length(); i++)
{
char ch = pagePattern.charAt(i);
if(dollar)
{
switch(ch)
{
case 77: // 'M'
sb.append(moduleConfig.getPrefix());
break;
case 80: // 'P'
sb.append(page);
break;
case 36: // '$'
sb.append('$');
break;
}
dollar = false;
}
else
if(ch == '$')
dollar = true;
else
sb.append(ch);
}
}
return sb.toString();
}
public static URL requestURL(HttpServletRequest request)
throws MalformedURLException
{
StringBuffer url = new StringBuffer();
String scheme = request.getScheme();
int port = request.getServerPort();
if(port < 0)
port = 80;
url.append(scheme);
url.append("://");
url.append(request.getServerName());
if(scheme.equals("http") && port != 80 || scheme.equals("https") && port != 443)
{
url.append(':');
url.append(port);
}
url.append(request.getRequestURI());
return new URL(url.toString());
}
public static URL serverURL(HttpServletRequest request)
throws MalformedURLException
{
StringBuffer url = new StringBuffer();
String scheme = request.getScheme();
int port = request.getServerPort();
if(port < 0)
port = 80;
url.append(scheme);
url.append("://");
url.append(request.getServerName());
if(scheme.equals("http") && port != 80 || scheme.equals("https") && port != 443)
{
url.append(':');
url.append(port);
}
return new URL(url.toString());
}
public static void saveException(PageContext pageContext, Throwable exception)
{
pageContext.setAttribute("org.apache.struts.action.EXCEPTION", exception, 2);
}
public static void selectApplication(String prefix, HttpServletRequest request, ServletContext context)
{
selectModule(prefix, request, context);
}
public static void selectModule(String prefix, HttpServletRequest request, ServletContext context)
{
ModuleConfig config = (ModuleConfig)context.getAttribute("org.apache.struts.action.MODULE" + prefix);
if(config != null)
request.setAttribute("org.apache.struts.action.MODULE", config);
else
request.removeAttribute("org.apache.struts.action.MODULE");
MessageResources resources = (MessageResources)context.getAttribute("org.apache.struts.action.MESSAGE" + prefix);
if(resources != null)
request.setAttribute("org.apache.struts.action.MESSAGE", resources);
else
request.removeAttribute("org.apache.struts.action.MESSAGE");
}
public static void selectApplication(HttpServletRequest request, ServletContext context)
{
selectModule(request, context);
}
public static void selectModule(HttpServletRequest request, ServletContext context)
{
String prefix = getModuleName(request, context);
selectModule(prefix, request, context);
}
public static String getModuleName(HttpServletRequest request, ServletContext context)
{
String matchPath = (String)request.getAttribute("javax.servlet.include.servlet_path");
if(matchPath == null)
matchPath = request.getServletPath();
return getModuleName(matchPath, context);
}
public static String getModuleName(String matchPath, ServletContext context)
{
if(log.isDebugEnabled())
log.debug("Get module name for path " + matchPath);
String prefix = "";
String prefixes[] = getModulePrefixes(context);
for(int lastSlash = 0; prefix.equals("") && (lastSlash = matchPath.lastIndexOf("/")) > 0;)
{
matchPath = matchPath.substring(0, lastSlash);
for(int i = 0; i < prefixes.length; i++)
{
if(!matchPath.equals(prefixes[i]))
continue;
prefix = prefixes[i];
break;
}
}
if(log.isDebugEnabled())
log.debug("Module name found: " + (prefix.equals("") ? "default" : prefix));
return prefix;
}
public static ModuleConfig getRequestModuleConfig(HttpServletRequest request)
{
return (ModuleConfig)request.getAttribute("org.apache.struts.action.MODULE");
}
public static ModuleConfig getModuleConfig(HttpServletRequest request, ServletContext context)
{
ModuleConfig moduleConfig = (ModuleConfig)request.getAttribute("org.apache.struts.action.MODULE");
if(moduleConfig == null)
moduleConfig = (ModuleConfig)context.getAttribute("org.apache.struts.action.MODULE");
return moduleConfig;
}
public static ModuleConfig getModuleConfig(PageContext pageContext)
{
return getModuleConfig((HttpServletRequest)pageContext.getRequest(), pageContext.getServletContext());
}
public static String[] getApplicationPrefixes(ServletContext context)
{
return getModulePrefixes(context);
}
public static synchronized String[] getModulePrefixes(ServletContext context)
{
String prefixes[] = (String[])context.getAttribute("org.apache.struts.util.PREFIXES");
if(prefixes != null)
return prefixes;
ArrayList list = new ArrayList();
for(Enumeration names = context.getAttributeNames(); names.hasMoreElements();)
{
String name = (String)names.nextElement();
if(name.startsWith("org.apache.struts.action.MODULE"))
{
String prefix = name.substring("org.apache.struts.action.MODULE".length());
if(prefix.length() > 0)
list.add(prefix);
}
}
prefixes = (String[])list.toArray(new String[list.size()]);
context.setAttribute("org.apache.struts.util.PREFIXES", prefixes);
return prefixes;
}
public static ActionMessages getActionMessages(PageContext pageContext, String paramName)
throws JspException
{
ActionMessages am = new ActionMessages();
Object value = pageContext.findAttribute(paramName);
try
{
if(value != null)
if(value instanceof String)
am.add("org.apache.struts.action.GLOBAL_MESSAGE", new ActionMessage((String)value));
else
if(value instanceof String[])
{
String keys[] = (String[])value;
for(int i = 0; i < keys.length; i++)
am.add("org.apache.struts.action.GLOBAL_MESSAGE", new ActionMessage(keys[i]));
}
else
if(value instanceof ErrorMessages)
{
String keys[] = ((ErrorMessages)value).getErrors();
if(keys == null)
keys = new String[0];
for(int i = 0; i < keys.length; i++)
am.add("org.apache.struts.action.GLOBAL_ERROR", new ActionError(keys[i]));
}
else
if(value instanceof ActionMessages)
am = (ActionMessages)value;
else
throw new JspException(messages.getMessage("actionMessages.errors", value.getClass().getName()));
}
catch(JspException e)
{
throw e;
}
catch(Exception e) { }
return am;
}
public static ActionErrors getActionErrors(PageContext pageContext, String paramName)
throws JspException
{
ActionErrors errors = new ActionErrors();
Object value = pageContext.findAttribute(paramName);
try
{
if(value != null)
if(value instanceof String)
errors.add("org.apache.struts.action.GLOBAL_ERROR", new ActionError((String)value));
else
if(value instanceof String[])
{
String keys[] = (String[])value;
for(int i = 0; i < keys.length; i++)
errors.add("org.apache.struts.action.GLOBAL_ERROR", new ActionError(keys[i]));
}
else
if(value instanceof ErrorMessages)
{
String keys[] = ((ErrorMessages)value).getErrors();
if(keys == null)
keys = new String[0];
for(int i = 0; i < keys.length; i++)
errors.add("org.apache.struts.action.GLOBAL_ERROR", new ActionError(keys[i]));
}
else
if(value instanceof ActionErrors)
errors = (ActionErrors)value;
else
throw new JspException(messages.getMessage("actionErrors.errors", value.getClass().getName()));
}
catch(JspException e)
{
throw e;
}
catch(Exception e)
{
log.debug(e, e);
}
return errors;
}
public static String encodeURL(String url)
{
if(encode != null)
return (String)encode.invoke(null, new Object[] {
url, "UTF-8"
});
break MISSING_BLOCK_LABEL_66;
IllegalAccessException e;
e;
log.debug("Could not find Java 1.4 encode method. Using deprecated version.", e);
break MISSING_BLOCK_LABEL_66;
InvocationTargetException e;
e;
log.debug("Could not find Java 1.4 encode method. Using deprecated version.", e);
return URLEncoder.encode(url);
}
public static boolean isXhtml(PageContext pageContext)
{
String xhtml = (String)pageContext.getAttribute("org.apache.struts.globals.XHTML", 1);
return "true".equalsIgnoreCase(xhtml);
}
static Class class$(String x0)
{
return Class.forName(x0);
ClassNotFoundException x1;
x1;
throw new NoClassDefFoundError(x1.getMessage());
}
static
{
log = LogFactory.getLog(class$org$apache$struts$util$RequestUtils != null ? class$org$apache$struts$util$RequestUtils : (class$org$apache$struts$util$RequestUtils = class$("org.apache.struts.util.RequestUtils")));
scopes = new HashMap();
try
{
Class args[] = {
class$java$lang$String != null ? class$java$lang$String : (class$java$lang$String = class$("java.lang.String")), class$java$lang$String != null ? class$java$lang$String : (class$java$lang$String = class$("java.lang.String"))
};
encode = (class$java$net$URLEncoder != null ? class$java$net$URLEncoder : (class$java$net$URLEncoder = class$("java.net.URLEncoder"))).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(1));
scopes.put("request", new Integer(2));
scopes.put("session", new Integer(3));
scopes.put("application", new Integer(4));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -