📄 tagutils.java
字号:
/**
* Retrieves the value from request scope and if it isn't already an
* <code>ErrorMessages</code> some classes are converted to one.
*
* @param pageContext The PageContext for the current page
* @param paramName Key for parameter value
* @return ActionErrors from request scope
* @exception JspException
* @deprecated Use getActionMessages() instead. This will be removed
* after Struts 1.2.
*/
public ActionErrors getActionErrors(PageContext pageContext, String paramName)
throws JspException {
ActionErrors errors = new ActionErrors();
Object value = pageContext.findAttribute(paramName);
if (value != null) {
try {
if (value instanceof String) {
errors.add(
ActionMessages.GLOBAL_MESSAGE,
new ActionMessage((String) value));
} else if (value instanceof String[]) {
String keys[] = (String[]) value;
for (int i = 0; i < keys.length; i++) {
errors.add(
ActionMessages.GLOBAL_MESSAGE,
new ActionMessage(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;
}
/**
* Return the form action converted into an action mapping path. The
* value of the <code>action</code> property is manipulated as follows in
* computing the name of the requested mapping:
* <ul>
* <li>Any filename extension is removed (on the theory that extension
* mapping is being used to select the controller servlet).</li>
* <li>If the resulting value does not start with a slash, then a
* slash is prepended.</li>
* </ul>
*/
public String getActionMappingName(String action) {
String value = action;
int question = action.indexOf("?");
if (question >= 0) {
value = value.substring(0, question);
}
int slash = value.lastIndexOf("/");
int period = value.lastIndexOf(".");
if ((period >= 0) && (period > slash)) {
value = value.substring(0, period);
}
return value.startsWith("/") ? value : ("/" + value);
}
/**
* Return the form action converted into a server-relative URL.
*/
public String getActionMappingURL(String action, PageContext pageContext) {
return getActionMappingURL(action,null,pageContext,false);
}
/**
* Return the form action converted into a server-relative URL.
*/
public String getActionMappingURL(String action, String module, PageContext pageContext, boolean contextRelative) {
HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
StringBuffer value = new StringBuffer(request.getContextPath());
ModuleConfig moduleConfig = ModuleUtils.getInstance().getModuleConfig(module, request, pageContext.getServletContext());
if ((moduleConfig != null) && (!contextRelative)) {
value.append(moduleConfig.getPrefix());
}
// Use our servlet mapping, if one is specified
String servletMapping =
(String) pageContext.getAttribute(
Globals.SERVLET_KEY,
PageContext.APPLICATION_SCOPE);
if (servletMapping != null) {
String queryString = null;
int question = action.indexOf("?");
if (question >= 0) {
queryString = action.substring(question);
}
String actionMapping = getActionMappingName(action);
if (servletMapping.startsWith("*.")) {
value.append(actionMapping);
value.append(servletMapping.substring(1));
} else if (servletMapping.endsWith("/*")) {
value.append(
servletMapping.substring(0, servletMapping.length() - 2));
value.append(actionMapping);
} else if (servletMapping.equals("/")) {
value.append(actionMapping);
}
if (queryString != null) {
value.append(queryString);
}
}
// Otherwise, assume extension mapping is in use and extension is
// already included in the action property
else {
if (!action.startsWith("/")) {
value.append("/");
}
value.append(action);
}
return value.toString();
}
/**
* Retrieves the value from request scope and if it isn't already an
* <code>ActionMessages</code>, some classes are converted to one.
*
* @param pageContext The PageContext for the current page
* @param paramName Key for parameter value
* @return ActionErrors in page context.
* @throws JspException
*/
public ActionMessages getActionMessages(
PageContext pageContext,
String paramName)
throws JspException {
ActionMessages am = new ActionMessages();
Object value = pageContext.findAttribute(paramName);
if (value != null) {
try {
if (value instanceof String) {
am.add(
ActionMessages.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(
ActionMessages.GLOBAL_MESSAGE,
new ActionMessage(keys[i]));
}
} else if (value instanceof ActionErrors) {
ActionMessages m = (ActionMessages) value;
am.add(m);
} 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) {
log.warn("Unable to retieve ActionMessage for paramName : "+paramName,e);
}
}
return am;
}
/**
* Return the ModuleConfig object if it exists, null if otherwise.
* @param pageContext The page context.
* @return the ModuleConfig object
*/
public ModuleConfig getModuleConfig(PageContext pageContext) {
return getModuleConfig(
null,
pageContext);
}
/**
* Return the ModuleConfig object for the given prefix if it exists, null if otherwise.
* @param module The module prefix
* @param pageContext The page context.
* @return the ModuleConfig object
*/
public ModuleConfig getModuleConfig(String module, PageContext pageContext) {
return ModuleUtils.getInstance().getModuleConfig(
module,
(HttpServletRequest) pageContext.getRequest(),
pageContext.getServletContext());
}
/**
* Converts the scope name into its corresponding PageContext constant value.
* @param scopeName Can be "page", "request", "session", or "application" in any
* case.
* @return The constant representing the scope (ie. PageContext.REQUEST_SCOPE).
* @throws JspException if the scopeName is not a valid name.
*/
public int getScope(String scopeName) throws JspException {
Integer scope = (Integer) scopes.get(scopeName.toLowerCase());
if (scope == null) {
throw new JspException(messages.getMessage("lookup.scope", scope));
}
return scope.intValue();
}
/**
* Look up and return current user locale, based on the specified parameters.
*
* @param pageContext The PageContext associated with this request
* @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
*/
public Locale getUserLocale(PageContext pageContext, String locale) {
return RequestUtils.getUserLocale(
(HttpServletRequest) pageContext.getRequest(),
locale);
}
/**
* Returns true if the custom tags are in XHTML mode.
*/
public boolean isXhtml(PageContext pageContext) {
String xhtml =
(String) pageContext.getAttribute(
Globals.XHTML_KEY,
PageContext.PAGE_SCOPE);
return "true".equalsIgnoreCase(xhtml);
}
/**
* Locate and return the specified bean, from an optionally specified
* scope, in the specified page context. If no such bean is found,
* return <code>null</code> instead. If an exception is thrown, it will
* have already been saved via a call to <code>saveException()</code>.
*
* @param pageContext Page context to be searched
* @param name Name of the bean to be retrieved
* @param scopeName Scope to be searched (page, request, session, application)
* or <code>null</code> to use <code>findAttribute()</code> instead
* @return JavaBean in the specified page context
* @exception JspException if an invalid scope name
* is requested
*/
public Object lookup(PageContext pageContext, String name, String scopeName)
throws JspException {
if (scopeName == null) {
return pageContext.findAttribute(name);
}
try {
return pageContext.getAttribute(name, instance.getScope(scopeName));
} catch (JspException e) {
saveException(pageContext, e);
throw e;
}
}
/**
* Locate and return the specified property of the specified bean, from
* an optionally specified scope, in the specified page context. If an
* exception is thrown, it will have already been saved via a call to
* <code>saveException()</code>.
*
* @param pageContext Page context to be searched
* @param name Name of the bean to be retrieved
* @param property Name of the property to be retrieved, or
* <code>null</code> to retrieve the bean itself
* @param scope Scope to be searched (page, request, session, application)
* or <code>null</code> to use <code>findAttribute()</code> instead
* @return property of specified JavaBean
*
* @exception JspException if an invalid scope name
* is requested
* @exception JspException if the specified bean is not found
* @exception JspException if accessing this property causes an
* IllegalAccessException, IllegalArgumentException,
* InvocationTargetException, or NoSuchMethodException
*/
public Object lookup(
PageContext pageContext,
String name,
String property,
String scope)
throws JspException {
// Look up the requested bean, and return if requested
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -