📄 basicpropertytag.java
字号:
/*
* WebWork, Web Application Framework
*
* Distributable under Apache license.
* See terms of license at opensource.org
*/
package webwork.view.taglib;
import webwork.util.TextUtil;
import webwork.util.BeanUtil;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.PageContext;
import java.util.Iterator;
import org.apache.commons.logging.*;
/**
* Access the value of a named property. By default (implicitly),
* this tag will escape its contents if it does *not* have a body.
* If it does have a body, the tag will not escape the contents.
* You can explicitly tell the tag to escape or not.
* Quoted text that is escaped will have its quotes stripped off.
*
* @author Rickard 謆erg (rickard@dreambean.com)
* @author Matt Baldree (matt@smallleap.com)
* @version $Revision: 1.7 $
*/
public class BasicPropertyTag
extends WebWorkBodyTagSupport
{
protected static Log log = LogFactory.getLog(BasicPropertyTag.class);
// Attributes ----------------------------------------------------
protected String valueAttr;
protected Boolean escape;
protected boolean hadBody;
// Public --------------------------------------------------------
public void setValue(String inName)
{
valueAttr = inName;
}
public void setEscape(boolean inEscape)
{
escape = new Boolean(inEscape);
}
// BodyTag implementation ----------------------------------------
public int doStartTag() throws JspException
{
hadBody = false;
Object value = findValue(valueAttr);
getStack().pushValue(value);
String id = getId();
if (id != null && value != null) {
pageContext.setAttribute(id, value);
pageContext.setAttribute(id, value, PageContext.REQUEST_SCOPE);
}
return EVAL_BODY_BUFFERED;
}
public int doAfterBody() throws JspException
{
// This is a workaround for a bug in Jasper.
// http://znutar.cortexity.com/BugRatViewer/ShowReport/652
// Resin 1.2.1 also has this bug (i.e. body is called even though it is empty)
// If the bodyContent is null then just return
if (bodyContent == null)
return SKIP_BODY;
String body = null;
try {
body = bodyContent.getString();
} catch (Exception e) {
//do nothing - leave body as null
}
//Added null check - Fixes WW-195 on Resin.
hadBody = body != null && body.length() != 0;
if (hadBody) {
try {
if (shouldEscape()) {
body = TextUtil.escapeHTML(body);
}
bodyContent.getEnclosingWriter().write(body);
} catch (java.io.IOException e) {
throw new JspTagException("Could not show attribute " + valueAttr + ":" + e);
}
}
return SKIP_BODY;
}
public int doEndTag() throws JspException
{
Object value = getStack().popValue();
if (!hadBody) {
// No body used. This means that we want to print the parameter out
try {
//LogFactory.getLog(this.getClass()).debug("Property value:"+value);
if (value != null && getId() == null)
if (value instanceof Iterator) {
Iterator enum = (Iterator) value;
if (enum.hasNext()) {
if (shouldEscape()) {
pageContext.getOut().write(TextUtil.escapeHTML(BeanUtil.toStringValue(enum.next())));
} else {
pageContext.getOut().write(BeanUtil.toStringValue(enum.next()));
}
}
} else if (value instanceof char[]) {
if (shouldEscape()) {
pageContext.getOut().write(TextUtil.escapeHTML(String.valueOf((char[]) value)));
} else {
pageContext.getOut().write(String.valueOf((char[]) value));
}
} else {
if (shouldEscape()) {
pageContext.getOut().write(TextUtil.escapeHTML(BeanUtil.toStringValue(value)));
} else {
pageContext.getOut().write(BeanUtil.toStringValue(value));
}
}
else
pageContext.getOut().write(""); // Printing out null gives no output
} catch (Throwable t) {
String msg = "Could not show value: " + valueAttr;
log.error(msg, t);
throw new JspException(msg + ", throwable: " + t);
}
}
return EVAL_PAGE;
}
/**
* The BasicPropertyTag should escape IF the escape value has been set to true. At
* all other times it will false (ie not escape data).
*
* CHANGE THIS METHOD ON PAIN OF DEATH -mike :)
*
* @see PropertyTag#shouldEscape()
*/
protected boolean shouldEscape()
{
if (escape == null)
{
return false;
}
return escape.booleanValue();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -