📄 tagutils.java
字号:
Object bean = lookup(pageContext, name, scope);
if (bean == null) {
JspException e = null;
if (scope == null) {
e = new JspException(messages.getMessage("lookup.bean.any", name));
} else {
e =
new JspException(
messages.getMessage("lookup.bean", name, scope));
}
saveException(pageContext, e);
throw e;
}
if (property == null) {
return bean;
}
// Locate and return the specified property
try {
return PropertyUtils.getProperty(bean, property);
} catch (IllegalAccessException e) {
saveException(pageContext, e);
throw new JspException(
messages.getMessage("lookup.access", property, name));
} catch (IllegalArgumentException e) {
saveException(pageContext, e);
throw new JspException(
messages.getMessage("lookup.argument", property, name));
} catch (InvocationTargetException e) {
Throwable t = e.getTargetException();
if (t == null) {
t = e;
}
saveException(pageContext, t);
throw new JspException(
messages.getMessage("lookup.target", property, name));
} catch (NoSuchMethodException e) {
saveException(pageContext, e);
throw new JspException(
messages.getMessage("lookup.method", property, name));
}
}
/**
* Look up and return a message string, based on the specified parameters.
*
* @param pageContext The PageContext associated with this request
* @param bundle Name of the servlet context attribute for our
* message resources bundle
* @param locale Name of the session attribute for our user's Locale
* @param key Message key to be looked up and returned
* @return message string
*
* @exception JspException if a lookup error occurs (will have been
* saved in the request already)
*/
public String message(
PageContext pageContext,
String bundle,
String locale,
String key)
throws JspException {
return message(pageContext, bundle, locale, key, null);
}
/**
* Look up and return a message string, based on the specified parameters.
*
* @param pageContext The PageContext associated with this request
* @param bundle Name of the servlet context attribute for our
* message resources bundle
* @param locale Name of the session attribute for our user's Locale
* @param key Message key to be looked up and returned
* @param args Replacement parameters for this message
* @return message string
* @exception JspException if a lookup error occurs (will have been
* saved in the request already)
*/
public String message(
PageContext pageContext,
String bundle,
String locale,
String key,
Object args[])
throws JspException {
MessageResources resources =
retrieveMessageResources(pageContext, bundle, false);
Locale userLocale = getUserLocale(pageContext, locale);
String message = null;
if (args == null) {
message = resources.getMessage(userLocale, key);
} else {
message = resources.getMessage(userLocale, key, args);
}
if ((message == null) && log.isDebugEnabled()) {
// log missing key to ease debugging
log.debug(resources.getMessage("message.resources", key, bundle, locale));
}
return message;
}
/**
* <p>Return the context-relative URL that corresponds to the specified
* <code>page</code> attribute value, calculated based on the
* <code>pagePattern</code> property of the current module's
* {@link ModuleConfig}.</p>
*
* @param request The servlet request we are processing
* @param page The module-relative URL to be substituted in
* to the <code>pagePattern</code> pattern for the current module
* (<strong>MUST</strong> start with a slash)
* @return context-relative URL
*/
public String pageURL(HttpServletRequest request, String page, ModuleConfig moduleConfig) {
StringBuffer sb = new StringBuffer();
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 'M':
sb.append(moduleConfig.getPrefix());
break;
case 'P':
sb.append(page);
break;
case '$':
sb.append('$');
break;
default :
; // Silently swallow
}
dollar = false;
continue;
} else if (ch == '$') {
dollar = true;
} else {
sb.append(ch);
}
}
}
return sb.toString();
}
/**
* Return true if a message string for the specified message key
* is present for the specified Locale.
*
* @param pageContext The PageContext associated with this request
* @param bundle Name of the servlet context attribute for our
* message resources bundle
* @param locale Name of the session attribute for our user's Locale
* @param key Message key to be looked up and returned
* @return true if a message string for message key exists
* @exception JspException if a lookup error occurs (will have been
* saved in the request already)
*/
public boolean present(
PageContext pageContext,
String bundle,
String locale,
String key)
throws JspException {
MessageResources resources =
retrieveMessageResources(pageContext, bundle, true);
Locale userLocale = getUserLocale(pageContext, locale);
return resources.isPresent(userLocale, key);
}
/**
* Returns the appropriate MessageResources object for the current module and
* the given bundle.
*
* @param pageContext Search the context's scopes for the resources.
* @param bundle The bundle name to look for. If this is <code>null</code>, the
* default bundle name is used.
* @return MessageResources The bundle's resources stored in some scope.
* @throws JspException if the MessageResources object could not be found.
*/
private MessageResources retrieveMessageResources(
PageContext pageContext,
String bundle,
boolean checkPageScope)
throws JspException {
MessageResources resources = null;
if (bundle == null) {
bundle = Globals.MESSAGES_KEY;
}
if (checkPageScope) {
resources =
(MessageResources) pageContext.getAttribute(
bundle,
PageContext.PAGE_SCOPE);
}
if (resources == null) {
resources =
(MessageResources) pageContext.getAttribute(
bundle,
PageContext.REQUEST_SCOPE);
}
if (resources == null) {
ModuleConfig moduleConfig = getModuleConfig(pageContext);
resources =
(MessageResources) pageContext.getAttribute(
bundle + moduleConfig.getPrefix(),
PageContext.APPLICATION_SCOPE);
}
if (resources == null) {
resources =
(MessageResources) pageContext.getAttribute(
bundle,
PageContext.APPLICATION_SCOPE);
}
if (resources == null) {
JspException e =
new JspException(messages.getMessage("message.bundle", bundle));
saveException(pageContext, e);
throw e;
}
return resources;
}
/**
* Save the specified exception as a request attribute for later use.
*
* @param pageContext The PageContext for the current page
* @param exception The exception to be saved
*/
public void saveException(PageContext pageContext, Throwable exception) {
pageContext.setAttribute(
Globals.EXCEPTION_KEY,
exception,
PageContext.REQUEST_SCOPE);
}
/**
* Write the specified text as the response to the writer associated with
* this page. <strong>WARNING</strong> - If you are writing body content
* from the <code>doAfterBody()</code> method of a custom tag class that
* implements <code>BodyTag</code>, you should be calling
* <code>writePrevious()</code> instead.
*
* @param pageContext The PageContext object for this page
* @param text The text to be written
*
* @exception JspException if an input/output error occurs (already saved)
*/
public void write(PageContext pageContext, String text)
throws JspException {
JspWriter writer = pageContext.getOut();
try {
writer.print(text);
} catch (IOException e) {
TagUtils.getInstance().saveException(pageContext, e);
throw new JspException
(messages.getMessage("write.io", e.toString()));
}
}
/**
* Write the specified text as the response to the writer associated with
* the body content for the tag within which we are currently nested.
*
* @param pageContext The PageContext object for this page
* @param text The text to be written
*
* @exception JspException if an input/output error occurs (already saved)
*/
public void writePrevious(PageContext pageContext, String text)
throws JspException {
JspWriter writer = pageContext.getOut();
if (writer instanceof BodyContent) {
writer = ((BodyContent) writer).getEnclosingWriter();
}
try {
writer.print(text);
} catch (IOException e) {
TagUtils.getInstance().saveException(pageContext, e);
throw new JspException
(messages.getMessage("write.io", e.toString()));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -