自定义标签在有时候是非常管用的 ,固然jstl,struts,spring的标签很强大,但是,有些时候还是想自己写个。例如分页就可以用自定义标签实现呢。闲话少说了,看看怎么写吧。
1 web.xml 声明
<taglib>
<taglib-uri>/core</taglib-uri>
<taglib-location>/lib/tld/c.tld</taglib-location>
</taglib>
其中uri是在jsp页面上显示的时候用到的,location是标签存放的位置,以webapps为根目录
2 自己写的*.tld文件,简简单单的。看看都能明白哦
<?xml version="1.0" encoding="UTF-8"?>
<!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>cms</short-name>
<description><![CDATA[Custom tag library for this application]]></description>
<tag>
<name>wolf</name>
<tag-class>jp.co.softbrain.bpm2.core.taglib.bpmitem.common.BpmItemFileTag</tag-class>
<attribute>
<name>fileId</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
3 实现类 就是*.tld里面声明的tag-class
继承一个类,覆盖一个方法就ok ,简单的就是这样了,再深了我也不会啦
public class BpmItemErrorsTag extends TagSupport {
public int doStartTag() throws JspException {
StringBuffer sb = new StringBuffer();
pageContext.getOut().write(sb.toString());//在页面上显示
return super.doStartTag();
}
}
4 jsp页面
<%@ taglib uri="/core" prefix="aaa" %>
<aaa:wolf fileId="123">
就这样了,试试看吧,相信很快就会了。