1. 程式人生 > >Taglib原理和實現 第六章:標籤內常用方法總結

Taglib原理和實現 第六章:標籤內常用方法總結

1。支援el表示式:
import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
private Object value = null;
this.value = ExpressionEvaluatorManager.evaluate("value", value.toString(), Object.class, this, pageContext); 

2.用BeanUtil取屬性值
import org.apache.commons.beanutils.PropertyUtils;
private String property=null;
Object propertyValue = PropertyUtils.getProperty(value, property);

3.設定request裡的值
pageContext.setAttribute("var",propertyValue);

4。列印
pageContext.getOut().print(outputString);

5。取得父標籤,取得想要的標籤,即使它非父
getParent()
findAncestorWithClass(this,ancestorTag.class);

6。標籤自帶方法和常量,方法按照容器的呼叫順序排列。示例  
 <c:if test="...">
  <c:out value="..."/>
 </c:if>


doStartTag : 容器解析到c:if左尖括號(“<”)時呼叫
doInitBody : 容器解析到c:if右尖括號(“>”)和c:out左尖括號(“<”)時呼叫
doAfterBody : 容器解析到c:out結束標記(“/>”)時呼叫
doEndTag :容器解析到c:if結束標記(“/>”)時呼叫

EVAL_BODY_SKIP : 通常在 doStartTag 方法裡呼叫,忽略標籤包括的內容,假如返回這個值,上面的c:if忽略c:out
EVAL_BODY_INCLUDE :通常在 doAfterBody 方法裡呼叫,再次執行body,假如返回這個值,上面的c:out被執行多次
EVAL_PAGE :可在任何方法裡呼叫,返回jsp頁面