当前位置:编程学习 > JAVA >>

java 自定义标签

1、编写**.tld文件
[html]  
<?xml version="1.0" encoding="UTF-8" ?>  
<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">  
    <!-- 标签库的版本号 -->  
    <tlib-version>1.0</tlib-version>  
    <!-- 标签库的默认前缀 -->  
    <short-name>candy</short-name>  
    <!-- 标签库的默认URI -->  
    <uri>/candy</uri>  
  
    <!-- 带遮罩的网页对话框标签 -->  
    <tag>  
        <description>带遮罩的网页对话框标签</description>  
        <name>msgdialog</name>  
        <tag-class>candy.tld.MsgDialogTag</tag-class>  
        <!-- 标签体可以是静态HTML元素,表达式语言,但不允许出现JSP脚本 -->  
        <body-content>scriptless</body-content>  
        <attribute>  
            <description>对话框标题文字,默认为"温馨提示"</description>  
            <name>title</name>  
            <required>false</required>  
            <!-- 可以使用JSP表达式 -->  
            <rtexprvalue>true</rtexprvalue>  
        </attribute>  
        <attribute>  
            <description>遮罩的高度,默认为屏幕的高度,即100%</description>  
            <name>height</name>  
            <required>false</required>  
            <!-- 可以使用JSP表达式 -->  
            <rtexprvalue>true</rtexprvalue>  
        </attribute>  
        <attribute>  
            <description>对话框的顶部距离,默认为300px</description>  
            <name>top</name>  
            <required>false</required>  
            <!-- 可以使用JSP表达式 -->  
            <rtexprvalue>true</rtexprvalue>  
        </attribute>  
        <attribute>  
            <description>对话框的宽度,默认为500px</description>  
            <name>boxwidth</name>  
            <required>false</required>  
            <!-- 可以使用JSP表达式 -->  
            <rtexprvalue>true</rtexprvalue>  
        </attribute>  
        <attribute>  
            <description>基本URL</description>  
            <name>basepath</name>  
            <required>true</required>  
            <!-- 可以使用JSP表达式 -->  
            <rtexprvalue>true</rtexprvalue>  
        </attribute>  
        <attribute>  
            <description>临时ID后缀,避免ID冲突,默认为系统时间的毫秒数</description>  
            <name>tmpid</name>  
            <required>false</required>  
            <!-- 可以使用JSP表达式 -->  
            <rtexprvalue>true</rtexprvalue>  
        </attribute>  
    </tag>  
</taglib>  
 
2.编写工具类
[java]  
package candy.tld;  
  
import java.io.*;  
import javax.servlet.jsp.JspException;  
import javax.servlet.jsp.tagext.JspFragment;  
import javax.servlet.jsp.tagext.SimpleTagSupport;  
  
/** 带遮罩的网页对话框自定义标签类 */  
public class MsgDialogTag extends SimpleTagSupport {  
  
    String title = "温馨提示"; // 对话框标题文字  
    String height = "100%"; // 遮罩的高度,默认为屏幕的高度,即100%  
    String top = "300px"; // 对话框的顶部距离,默认为100px  
    String boxwidth = "330px";// 对话框的宽度,默认为500px  
    String basepath = ""; // 基本URL  
    String tmpid = null; // 临时ID后缀,避免ID冲突,默认为系统时间的毫秒数  
  
    /** 标签体处理 */  
    public void doTag() throws JspException, IOException {  
        // 规范属性值  
        if (!height.endsWith("%"))  
            height = height + "px";  
        if (!top.endsWith("px"))  
            top = top + "px";  
        if (!boxwidth.endsWith("px"))  
            boxwidth = boxwidth + "px";  
        int titlewidth = Integer.valueOf(boxwidth.replaceAll("px", ""))  
                .intValue() - 22;  
        if (tmpid == null)  
            tmpid = String.valueOf(System.currentTimeMillis());// 临时ID后缀,避免ID冲突  
  
        // 取得现有标签体的内容  
        JspFragment body = this.getJspBody();  
        StringWriter writer = new StringWri
补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,