J2EE-13 複習tag自定義標記
阿新 • • 發佈:2018-11-11
simpleTagSupport
//向Jsp中輸出日期
package javaEE;
import javax.servlet.jsp.tagext.*;
public class TimeTag extends SimpleTagSupport{
public void doTag() throws JspException ,IOException{
getJspContext().getOut().print(
new java.util.Date());
}
}
SimpleTagSupport
public class SimpleTagSupport implements SimpleSupport{ }
doStartTag
TagSupport中的執行函式
返回值是int:int常量
SKIP_BODY EVAIL_BODY_INCLUDE
丟擲JspException
不丟擲IOException
在程式裡面用pageContext().getOut()進行輸出的時候要寫try catch
tld 檔案
<tag> <name>time</name> <tag-class>myTags.TimeTag</tag-class> <body-content>JSP</body-content> </tag>
在JSP中呼叫
在dlt中
<taglib>
<uri>mytag</uri>
</taglib>
在JSP中
<%@ taglib uri="mytag" prefix="a"%>
宣告uri為dlt中的uri或者改檔案的路徑
prefix 為標記的字首
JSP向標記處理程式傳遞資訊
<attribute> <name></name> <required></required> <rtexprvalue></rtexprvalue> <type></type> </attribute> <rtexprvalue>:對屬性值進步進行解析 一般是true,即進行動態解析 如果是false,即只對屬性當字串進行解析 eg <a:time place:"${place}">
自定義指令碼變數
<tag>
<name>time</name>
<tag-class>myTags.TimeTag</tag-class>
<body-content>JSP</body-content>
</tag>
public class MyTagExtraInfo extends TagExtraInfo {
public VariableInfo(TagData td){
return new VariableInfo[] {
new VariableInfo("a","java.lang.String",true,VariableInfo.NESTED);
}
}
}
在TimeTag中doStartTag對變數進行賦值
doStartTag(){
setAttrbute("a","someValue");
}