前段时间看到关于taglib的一些应用,感觉很神秘,刚开始的时候还弄不明白taglib到底有什么作用,在图书馆查了几次资料,终于搞明白了,今天学习taglib的编写及配置,现在写出今天学习的总结。
taglib的主要作用就是:对一些需要重复利用的代码段进行封装,并设置该代码段可能用到的属性,提高代码的利用率。taglib主要有三个部分构成:
- 实现代码段的.java文件;
- 标签库描述文件.tld;
- web.xml的配置。
下面首先介绍标签实现文件,这里以DecorTag.java实现的标签为例:
//DecorTag.java
/*
* DecorTag.java
*
* Created on 2007年3月17日, 上午8:25
*/package com.wlmzfx.servlet;
import javax.servlet.jsp.*;//jsp类
import javax.servlet.jsp.tagext.*;//标签类库
import java.io.IOException;//这里实现一个用html表格显示一个装饰框
public class DecorTag extends TagSupport{
//这些字段用来控制外观
String align;//对齐方式
String title;//标题
String titleColor;//标题前景色
String titleAlign;//标题对齐方式
String color;//方框背景色
String borderColor;//边框颜色
String margin;//边框与内容之间的像素
String borderWidth;//边框宽度的像素值
//以下方法用来设置各种属性值,是必不可少的,在jsp页面使用标签时,属性将转化为方法调用
public void setAlign(String value){
this.align = value;
}
public void setTitle(String value){
this.title = value;
}
public void setTitleColor(String value){
this.titleColor = value;
}
public void setTitleAlign(String value){
this.titleAlign = value;
}
public void setColor(String value){
this.color = value;
}
public void setBorderColor(String value) {
this.borderColor = value;
}
public void setMargin(String value){
this.margin = value;
}
public void setBorderWidth(String value){
this.borderWidth = value;
}
public void setPageContext(PageContext context){ //以超类保存页面环境对象,下面的Tag()要用到,这很重要
super.setPageContext(context);
//设置属性的默认值
align = “cente”;
title = null;
titleColor = “White”;
titleAlign = “left”;
color = “lightblue”;
borderColor = “black”;
margin = “20″;
borderWidth = “4″;
}
/**
*此方法再遇到<decor:box>标签时调用
**/
public int doStartTag() throws JspException{
try{
//从PageContext对象获取输出流并传给setPageContext()方法
JspWriter out = pageContext.getOut();
out.print(”<div align=’”+align+”‘>”+”<table bgcolor=’”+borderColor+”‘”+”<border=’0′ cellspacing=’0′”+”cellpadding=’”+borderWidth+”‘”);
if(title != null)
out.print(”<tr><td align=’”+titleAlign+”‘>”+”<font face=’helvetica’ size=’+1′ “+”color=’”+titleColor+”‘><br>”+title+”</b></font></td><td>”);
out.print(”<tr><td<table bgcolor=’”+color+”‘”+”border=’0′ cellspacing=’0′”+”cellpadding=’”+margin+”‘><tr><td>”);
}catch(IOException e){
throw new JspException(e.getMessage());
}
//返回值告诉Jsp类处理标签主题
return EVAL_BODY_INCLUDE;
}
//在遇到</decor:box>结束标签时调用
public int doEndTag()throws JspException{
try{
JspWriter out = pageContext.getOut();
out.println(”</td></tr></table><td><tr><table></div>”);
}catch(IOException e){
throw new JspException(e.getMessage());
}
//返回值指示继续处理Jsp页面
return EVAL_PAGE;
}
}//decor_1_0.tld(放在WEB-INF/tids/decor_1_0.tld
//一般以后缀_1_0来表示tld文件的版本,实际上这也是个xml文件<?xml version=”1.0″ encoding=”UTF-8″?>
<taglib version=”2.0″ 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 web-jsptaglibrary_2_0.xsd”>
<tlib-version>1.0</tlib-version><!–文件版本–>
<short-name>decor</short-name><!–库名–>
<uri>http://www.wlmzfx.com/tlds/decor_1_0.tld</uri><!–唯一标识符–>
<tag><!–首先定义了标签库的一个标签–>
<name>box</name><!–首先定义标签名,实现类–>
<tagclass>com.wlmzfx.servlet.DecorTag</tagclass>
<!–定义标签库的每一个属性–>
<attribute>
<name>align</name><!–属性–>
<required>false</required><!–不是必须–>
<rtexprvalue>true</rtexprvalue><!–应有<%= %>值–>
</attribute>
<attribute>
<name>color</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>bordercolor</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>margin</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>borderWidth</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>title</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>titleColor</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>titleAlign</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<!– A validator verifies that the tags are used correctly at JSP
translation time. Validator entries look like this:
<validator>
<validator-class>com.mycompany.TagLibValidator</validator-class>
<init-param>
<param-name>parameter</param-name>
<param-value>value</param-value>
</init-param>
</validator>
–>
<!– A tag library can register Servlet Context event listeners in
case it needs to react to such events. Listener entries look
like this:
<listener>
<listener-class>com.mycompany.TagLibListener</listener-class>
</listener>
–>
</taglib>
然后需要在web.xml中配置该标签,在web.xml中添加下面的内容:
<taglib>
<!–看到标签库唯一标识符时–>
<taglib-uri>http://www.wlmzfx.com/tlds/decor_1_0.tld</taglib-uri>
<!–使用标签库描述文件的本地副本–>
<taglib-location>tlds/decor_1_0.tld</taglib-location>
</taglib>
下面以一个例子来说明该标签库的用法:
在任何jsp页面中加入下面的代码:
<!–这里仅仅定义了title,color,margin属性–>
<decor:box title=”LogOut when Done” color=”red” margin=”100″>
<!–这里添加表格的内容,这里可以继续使用<decor:box>标签,但是同样必须以</decor:box>结束–>
内容在这里
</decor:box>