J2EE-13 复习tag自定义标记

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/L1558198727/article/details/83105956

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");
}

猜你喜欢

转载自blog.csdn.net/L1558198727/article/details/83105956