📄 basehandlertag.java
字号:
// ------------------------------------------------------ Protected Methods
/**
* Return the text specified by the literal value or the message resources
* key, if any; otherwise return <code>null</code>.
*
* @param literal Literal text value or <code>null</code>
* @param key Message resources key or <code>null</code>
* @throws JspException if both arguments are non-null
*/
protected String message(String literal, String key)
throws JspException {
if (literal != null) {
if (key != null) {
JspException e =
new JspException(messages.getMessage("common.both"));
TagUtils.getInstance().saveException(pageContext, e);
throw e;
} else {
return (literal);
}
} else {
if (key != null) {
return TagUtils.getInstance().message(pageContext, getBundle(),
getLocale(), key);
} else {
return null;
}
}
}
private Integer getJstlLoopIndex() {
if (!triedJstlInit) {
triedJstlInit = true;
try {
loopTagClass =
RequestUtils.applicationClass(
"javax.servlet.jsp.jstl.core.LoopTag");
loopTagGetStatus =
loopTagClass.getDeclaredMethod("getLoopStatus", null);
loopTagStatusClass =
RequestUtils.applicationClass(
"javax.servlet.jsp.jstl.core.LoopTagStatus");
loopTagStatusGetIndex =
loopTagStatusClass.getDeclaredMethod("getIndex", null);
triedJstlSuccess = true;
} catch (ClassNotFoundException ex) {
// These just mean that JSTL isn't loaded, so ignore
} catch (NoSuchMethodException ex) {
}
}
if (triedJstlSuccess) {
try {
Object loopTag =
findAncestorWithClass(this, loopTagClass);
if (loopTag == null) {
return null;
}
Object status = loopTagGetStatus.invoke(loopTag, null);
return (Integer) loopTagStatusGetIndex.invoke(status, null);
} catch (IllegalAccessException ex) {
log.error(ex.getMessage(), ex);
} catch (IllegalArgumentException ex) {
log.error(ex.getMessage(), ex);
} catch (InvocationTargetException ex) {
log.error(ex.getMessage(), ex);
} catch (NullPointerException ex) {
log.error(ex.getMessage(), ex);
} catch (ExceptionInInitializerError ex) {
log.error(ex.getMessage(), ex);
}
}
return null;
}
/**
* Appends bean name with index in brackets for tags with 'true' value in
* 'indexed' attribute.
*
* @param handlers The StringBuffer that output will be appended to.
* @throws JspException if 'indexed' tag used outside of iterate tag.
*/
protected void prepareIndex(StringBuffer handlers, String name)
throws JspException {
if (name != null) {
handlers.append(name);
}
handlers.append("[");
handlers.append(getIndexValue());
handlers.append("]");
if (name != null) {
handlers.append(".");
}
}
/**
* Returns the index value for tags with 'true' value in 'indexed'
* attribute.
*
* @return the index value.
* @throws JspException if 'indexed' tag used outside of iterate tag.
*/
protected int getIndexValue()
throws JspException {
// look for outer iterate tag
IterateTag iterateTag =
(IterateTag) findAncestorWithClass(this, IterateTag.class);
if (iterateTag != null) {
return iterateTag.getIndex();
}
// Look for JSTL loops
Integer i = getJstlLoopIndex();
if (i != null) {
return i.intValue();
}
// this tag should be nested in an IterateTag or JSTL loop tag, if it's not, throw exception
JspException e =
new JspException(messages.getMessage("indexed.noEnclosingIterate"));
TagUtils.getInstance().saveException(pageContext, e);
throw e;
}
/**
* Prepares the style attributes for inclusion in the component's HTML
* tag.
*
* @return The prepared String for inclusion in the HTML tag.
* @throws JspException if invalid attributes are specified
*/
protected String prepareStyles()
throws JspException {
StringBuffer styles = new StringBuffer();
boolean errorsExist = doErrorsExist();
if (errorsExist && (getErrorStyleId() != null)) {
prepareAttribute(styles, "id", getErrorStyleId());
} else {
prepareAttribute(styles, "id", getStyleId());
}
if (errorsExist && (getErrorStyle() != null)) {
prepareAttribute(styles, "style", getErrorStyle());
} else {
prepareAttribute(styles, "style", getStyle());
}
if (errorsExist && (getErrorStyleClass() != null)) {
prepareAttribute(styles, "class", getErrorStyleClass());
} else {
prepareAttribute(styles, "class", getStyleClass());
}
prepareAttribute(styles, "title", message(getTitle(), getTitleKey()));
prepareAttribute(styles, "alt", message(getAlt(), getAltKey()));
prepareInternationalization(styles);
return styles.toString();
}
/**
* Determine if there are errors for the component.
*
* @return Whether errors exist.
*/
protected boolean doErrorsExist()
throws JspException {
boolean errorsExist = false;
if ((getErrorStyleId() != null) || (getErrorStyle() != null)
|| (getErrorStyleClass() != null)) {
String actualName = prepareName();
if (actualName != null) {
ActionMessages errors =
TagUtils.getInstance().getActionMessages(pageContext,
errorKey);
errorsExist = ((errors != null)
&& (errors.size(actualName) > 0));
}
}
return errorsExist;
}
/**
* Prepares the actual name of the component.
*
* @return The actual component name.
*/
protected String prepareName()
throws JspException {
return null;
}
/**
* Prepares the event handlers for inclusion in the component's HTML tag.
*
* @return The prepared String for inclusion in the HTML tag.
*/
protected String prepareEventHandlers() {
StringBuffer handlers = new StringBuffer();
prepareMouseEvents(handlers);
prepareKeyEvents(handlers);
prepareTextEvents(handlers);
prepareFocusEvents(handlers);
return handlers.toString();
}
/**
* Prepares the mouse event handlers, appending them to the the given
* StringBuffer.
*
* @param handlers The StringBuffer that output will be appended to.
*/
protected void prepareMouseEvents(StringBuffer handlers) {
prepareAttribute(handlers, "onclick", getOnclick());
prepareAttribute(handlers, "ondblclick", getOndblclick());
prepareAttribute(handlers, "onmouseover", getOnmouseover());
prepareAttribute(handlers, "onmouseout", getOnmouseout());
prepareAttribute(handlers, "onmousemove", getOnmousemove());
prepareAttribute(handlers, "onmousedown", getOnmousedown());
prepareAttribute(handlers, "onmouseup", getOnmouseup());
}
/**
* Prepares the keyboard event handlers, appending them to the the given
* StringBuffer.
*
* @param handlers The StringBuffer that output will be appended to.
*/
protected void prepareKeyEvents(StringBuffer handlers) {
prepareAttribute(handlers, "onkeydown", getOnkeydown());
prepareAttribute(handlers, "onkeyup", getOnkeyup());
prepareAttribute(handlers, "onkeypress", getOnkeypress());
}
/**
* Prepares the text event handlers, appending them to the the given
* StringBuffer.
*
* @param handlers The StringBuffer that output will be appended to.
*/
protected void prepareTextEvents(StringBuffer handlers) {
prepareAttribute(handlers, "onselect", getOnselect());
prepareAttribute(handlers, "onchange", getOnchange());
}
/**
* Prepares the focus event handlers, appending them to the the given
* StringBuffer.
*
* @param handlers The StringBuffer that output will be appended to.
*/
protected void prepareFocusEvents(StringBuffer handlers) {
prepareAttribute(handlers, "onblur", getOnblur());
prepareAttribute(handlers, "onfocus", getOnfocus());
// Get the parent FormTag (if necessary)
FormTag formTag = null;
if ((doDisabled && !getDisabled()) || (doReadonly && !getReadonly())) {
formTag =
(FormTag) pageContext.getAttribute(Constants.FORM_KEY,
PageContext.REQUEST_SCOPE);
}
// Format Disabled
if (doDisabled) {
boolean formDisabled =
(formTag == null) ? false : formTag.isDisabled();
if (formDisabled || getDisabled()) {
handlers.append(" disabled=\"disabled\"");
}
}
// Format Read Only
if (doReadonly) {
boolean formReadOnly =
(formTag == null) ? false : formTag.isReadonly();
if (formReadOnly || getReadonly()) {
handlers.append(" readonly=\"readonly\"");
}
}
}
/**
* Prepares the internationalization attribtes, appending them to the the given
* StringBuffer.
*
* @param handlers The StringBuffer that output will be appended to.
* @since Struts 1.3.6
*/
protected void prepareInternationalization(StringBuffer handlers) {
prepareAttribute(handlers, "lang", getLang());
prepareAttribute(handlers, "dir", getDir());
}
/**
* 'Hook' to enable tags to be extended and additional attributes added.
*
* @param handlers The StringBuffer that output will be appended to.
*/
protected void prepareOtherAttributes(StringBuffer handlers) {
}
/**
* Prepares an attribute if the value is not null, appending it to the the
* given StringBuffer.
*
* @param handlers The StringBuffer that output will be appended to.
*/
protected void prepareAttribute(StringBuffer handlers, String name,
Object value) {
if (value != null) {
handlers.append(" ");
handlers.append(name);
handlers.append("=\"");
handlers.append(value);
handlers.append("\"");
}
}
/**
* Allows HTML tags to find out if they're nested within an
* %lt;html:html> tag that has xhtml set to true.
*
* @return true if the tag is nested within an html tag with xhtml set to
* true, false otherwise.
* @since Struts 1.1
*/
protected boolean isXhtml() {
return TagUtils.getInstance().isXhtml(this.pageContext);
}
/**
* Returns the closing brace for an input element depending on xhtml
* status. The tag must be nested within an %lt;html:html> tag that
* has xhtml set to true.
*
* @return String - > if xhtml is false, /> if xhtml is true
* @since Struts 1.1
*/
protected String getElementClose() {
return this.isXhtml() ? " />" : ">";
}
/**
* Searches all scopes for the bean and calls BeanUtils.getProperty() with
* the given arguments and converts any exceptions into JspException.
*
* @param beanName The name of the object to get the property from.
* @param property The name of the property to get.
* @return The value of the property.
* @throws JspException
* @since Struts 1.1
*/
protected String lookupProperty(String beanName, String property)
throws JspException {
Object bean =
TagUtils.getInstance().lookup(this.pageContext, beanName, null);
if (bean == null) {
throw new JspException(messages.getMessage("getter.bean", beanName));
}
try {
return BeanUtils.getProperty(bean, property);
} catch (IllegalAccessException e) {
throw new JspException(messages.getMessage("getter.access",
property, beanName));
} catch (InvocationTargetException e) {
Throwable t = e.getTargetException();
throw new JspException(messages.getMessage("getter.result",
property, t.toString()));
} catch (NoSuchMethodException e) {
throw new JspException(messages.getMessage("getter.method",
property, beanName));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -