借助于jstl,我们可以方便的开发自定义标签,而不需要使用sun的jsp tag api
本文以开发一个简单的if标签为例
首先编写标签执行类,最重要的一点,继承javax.servlet.jsp.jstl.core.ConditionalTagSupport;
代码简单的返回了true.大家可以加入自己的业务逻辑代码
package
ttt;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.jstl.core.ConditionalTagSupport;
public class ConditionTest extends ConditionalTagSupport {
protected boolean condition() throws JspTagException {
return true ;
}
}
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.jstl.core.ConditionalTagSupport;
public class ConditionTest extends ConditionalTagSupport {
protected boolean condition() throws JspTagException {
return true ;
}
}
编写tld文件
<?
xml version="1.0" encoding="ISO-8859-1"
?>
<! DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd" >
< taglib >
< tlib-version > 1.0 </ tlib-version >
< jsp-version > 1.2 </ jsp-version >
< short-name > book </ short-name >
< uri > http://jstlbook.com/tld/weekday.tld </ uri >
< tag >
< name > if </ name >
< tag-class > ttt.ConditionTest </ tag-class >
</ tag >
</ taglib >
<! DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd" >
< taglib >
< tlib-version > 1.0 </ tlib-version >
< jsp-version > 1.2 </ jsp-version >
< short-name > book </ short-name >
< uri > http://jstlbook.com/tld/weekday.tld </ uri >
< tag >
< name > if </ name >
< tag-class > ttt.ConditionTest </ tag-class >
</ tag >
</ taglib >
JSP:
<% @ taglib prefix = " cc " uri = " http://jstlbook.com/tld/weekday.tld " %>
< html >
< head >
< title > Currency Formatting </ title >
</ head >
< body >
< cc:if >
this is tld test
</ cc:if >
</ body >
</ html >
运行,页面显示this is tld test,如果标签执行类改成reutrn false,则无任何输出