自定义标签

test.jsp
<%@ page contentType="text/html;charset=GB2312" language="java" %>
<%@ taglib prefix="mytag" uri="/WEB-INF/jsp2/jsp2-example-taglib.tld" %>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>
<%--<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>--%>
<html>
<head><title>Simple jsp page</title></head>

<body>

<p><B>My first tag prints</B>:
<mytag:hello/>
<br>
</p>


<H2>News Portal: Another Tag File Example</H2>
<TABLE border="0">
<TR valign="top">
<TD><tags:display color="#ff0000" bgcolor="#ffc0c0" title="Travel"> Last
French Concorde Arrives in NY<br/> Another Travel Headline<br/> Yet
Another Travel Headline<br/> </tags:display></TD>
<TD><tags:display color="#00fc00" bgcolor="#c0ffc0" title="Technology">
Java for in-flight entertainment<BR> Another Technology Headline<BR>
Another Technology Headline<BR> </tags:display></TD>
<TD><tags:display color="#ffcc11" bgcolor="#ffffcc" title="Sports">
American Football<BR/> NBA<BR/> Soccer<BR/> </tags:display></TD>
</TR>
</TABLE>

<HR>
<%--<c:set var="times" value="5"/>--%>
<p><B>Reminder:</B></p><br>
<%--<mytag:iteration times="${times}">
This is the ${sequence} Of ${times} times of reminder<br>
</mytag:iteration>--%>

<br>
<mytag:loop counts="5">
现在时间是:<%=new java.util.Date().toString()%><br>
</mytag:loop>
<br>

<mytag:HTMLEncode>
<Hello , Simple sample>
</mytag:HTMLEncode>

</body>
</html>

BodyTagExample.java
public class BodyTagExample
extends BodyTagSupport
{
private int counts;

public BodyTagExample()
{
super();
}

public void setCounts( int counts )
{
this.counts = counts;
}

public int doStartTag() throws JspTagException
{
System.out.println( "doStartTag..." );
if ( counts > 0 )
{
return EVAL_BODY_INCLUDE;
}
else
{
return SKIP_BODY;
}
}

public void setBodyContent( BodyContent bodyContent )
{
System.out.println( "setBodyContent..." );
this.bodyContent = bodyContent;
}

public void doInitBody() throws JspTagException
{
System.out.println( "doInitBody...." );
}

public int doAfterBody() throws JspTagException
{
System.out.println( "do After body..." + counts );
if ( counts > 1 )
{
counts--;
return EVAL_BODY_AGAIN; //EVAL_PAGE;
}
else
{
return SKIP_BODY;
}
}

public int doEndTag() throws JspTagException
{
System.out.println( "do end Tag..." );
try
{
if ( bodyContent != null )
{
bodyContent.writeOut( bodyContent.getEnclosingWriter() );
}
}
catch ( java.io.IOException e )
{
throw new JspTagException( "IO Error:" + e.getMessage() );
}
return EVAL_PAGE;
}
}

HelloTag.java
public class HelloTag extends SimpleTagSupport
{
public void doTag() throws JspException, IOException
{
getJspContext().getOut().write( "This is my first tag!" );
}
}

jsp2-example-taglib.tld
<?xml version="1.0" encoding="GB2312" ?>

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">

<!--
整个标签库标记信息
tlibversion 标签库版本号。是一个点式十进制数,最多为4组小数点分隔的数字组成。
jspversion 标签库所需的JSP规范最低版本。例如JSP1.1
shortname 标签库的缩写名。JSP可以使用该名字作为库中标签的缺省前缀。
uri 标签库唯一URI的元素。典型URL位置来自可下载taglib的位置。
info 标签库描述信息。
每个标签处理器及其属性
tag 在TLD中加入标签,描述组成库的每个标签。
name 与标签库的名字前缀一起使用的标签的名字, 是JSP容器唯一的标签标识。
tagclass 实现标签的标签处理器类的全名。
teiclass 标签附加信息(TEI)类的全名。TEI类给出关于标签处理器创建变量及对标签司性执行的任意有效性验证的信息。
bodycontent 描述标签处理器如何使用标签体的内容。有三种取值:
empty:表示标签体必须为空;
JSP:表示脚本元素和模板及其它标签一样被评估。
tagdependent:体内容被原封不动写入BodyContent,其它脚本元素以源码形式出现,而不被JSP容器解释。
info 标签的人工可读描述性信息。
attribute 使用标签时被编码的属性信息。用于定义标签的属性。
属性名:属性的名字。
true|false:属性在标签用到的位置是否要被编码。
true|false:属性值能否用表达式指定。

-->

<description>jsp2 Simpletag Library</description>
<display-name>Simpletag</display-name>
<tlib-version>1.1</tlib-version>
<short-name>tag</short-name>
<!--<uri>http://community.ceci.com/news</uri>-->

<tag>
<description>Prints this is my first tag</description>
<name>hello</name>
<tag-class>jsp2.examples.simpletag.HelloTag</tag-class>
<body-content>empty</body-content>
</tag>


<tag>
<name>iteration</name>
<tag-class>jsp2.examples.simpletag.IterationSimpleTag</tag-class>
<!--<body-content>scriptless</body-content>-->
<body-content>JSP</body-content>
<!--<description>Iteration Tag</description>-->
<!-- 如果在标签定义文件中用了variable,会生成变量定义和变量赋值的代码 -->
<variable>
<description>Current iterationnumber</description>
<name-given>sequence</name-given>
</variable>
<attribute>
<name>times</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>

<!-- 迭代标签 -->
<!--
tag中的name属性:定义了我们的tag名称,在后面会用到。
tag中的tagclass属性:指定了我们这个tag的实现类。
tage中的bodycontent属性:指定我们的页面内容是什么性质的。(注意:在jsp开发中这里必须写JSP)
tage中的attribute属性:定义了我们的这个tag可能有的属性。
attribute中的name属性:指定了属性的名称。它和我们类中定义的“int counts;”必须一样,并且在类中还必须包含一个setCounts(int counts)方法,否则这个属性就不能设置。
attribute中的required属性:表示这个属性是否是必须的。
attribute中的rtexprvalue属性:表示这个属性是否可以用JSP的程序段的结果输出。
-->

<tag>
<name>loop</name>
<tag-class>jsp2.examples.simpletag.BodyTagExample</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>counts</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>

<!-- HTMLEncode -->
<tag>
<name>HTMLEncode</name>
<tag-class>TransformerTag.HTML_FormatTag</tag-class>
<body-content>JSP</body-content>
</tag>

</taglib>

display.tag
<%@ attribute name="color" %>
<%@ attribute name="bgcolor" %>
<%@ attribute name="title" %>
<TABLE border="0" bgcolor="${color}">
<TR>
<TD><B>${title}</B></TD>
</TR>
<TR>
<TD bgcolor="${bgcolor}">
<jsp:doBody/>
</TD>
</TR>
</TABLE>

greetings.tag
Hello there. How are you doing?

web.xml
<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">


<display-name>tag-jsp</display-name>

<jsp-config>
<jsp-property-group>
<description>JSP Configurations</description>
<display-name>JSPConfig</display-name>
<url-pattern>*.jsp</url-pattern>
<el-ignored>false</el-ignored>
<page-encoding>GBK</page-encoding>
<scripting-invalid>false</scripting-invalid>
</jsp-property-group>
</jsp-config>

<welcome-file-list>
<welcome-file>/test.jsp</welcome-file>
</welcome-file-list>
</web-app>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值