1、編寫**.tld文件
<?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.編寫工具類
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 StringWriter();
body.invoke(writer);
// 構造帶遮罩的網頁對話框
StringBuffer sb = new StringBuffer();
sb.append("<style>\n");
sb.append("#msgbox"
+ tmpid
+ "{width:"
+ boxwidth
+ ";background: #D6D3CE; border:1px #848284 solid;padding:0px;margin:0 auto;}\n");
sb.append(".msgicon{float:left;background-image:url("
+ basepath
+ "/img/msgtitle_icon.jpg); background-repeat:no-repeat; height:20px;width:20px;}\n");
sb.append(".msgtilte{float:left; text-align:left;background-image:url("
+ basepath
+ "/img/msgtitle_back.jpg); background-repeat:repeat-x;line-height:20px;letter-spacing:2px; height:20px; width:"
+ titlewidth + "px;}\n");
// sb.append(".msgclose{float:left;background-image:url("
// + basepath
// + "/img/btn_close.jpg); background-repeat:no-repeat; height:20px; width:20px; cursor:pointer;}\n");
sb.append(".msgmainbox{clear:both; border-top:1px #848284 solid;text-align:left;padding:20px;overflow: auto;}\n");
sb.append("</style>\n");
sb.append("<div id='mask"
+ tmpid
+ "' style='clear:both;top:0;left:0;position:absolute;z-index:10001;filter:alpha(opacity=70);background:#000;opacity: 0.7;-moz-opacity: 0.7;height:"
+ height + ";width:100%;'></div>\n");
sb.append("<div id='msgprompt"
+ tmpid
+ "' style='clear:both;top:"
+ top
+ ";left:0;position: absolute; z-index: 10002; width:100%;text-align:center'>\n");
sb.append("<div id='msgbox" + tmpid + "'>\n");
sb.append("<div class='msgicon'></div>\n");
sb.append("<div class='msgtilte'>" + title + "</div>\n");
// sb.append("<div class='msgclose' onClick='closemask" + tmpid
// + "()'></div>\n");
sb.append("<div class='msgmainbox'>\n");
// 插入標籤體中的提示信息內容
sb.append(writer.toString().trim() + "\n");
sb.append("</div>\n");
sb.append("</div>\n");
sb.append("</div>\n");
// sb.append("<script language='javascript'>\n");
// sb.append("function closemask" + tmpid + "(){\n");
// sb.append("document.getElementById('mask" + tmpid
// + "').style.display='none';\n");
// sb.append("document.getElementById('msgprompt" + tmpid
// + "').style.display='none';\n");
// sb.append("}\n");
// sb.append("</script>\n");
sb.append("</div>\n");
// 輸出處理結果到頁面上
getJspContext().getOut().println(sb);
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getBasepath() {
return basepath;
}
public void setBasepath(String basepath) {
this.basepath = basepath;
}
public String getHeight() {
return height;
}
public void setHeight(String height) {
this.height = height;
}
public String getTop() {
return top;
}
public void setTop(String top) {
this.top = top;
}
public String getBoxwidth() {
return boxwidth;
}
public void setBoxwidth(String boxwidth) {
this.boxwidth = boxwidth;
}
public String getTmpid() {
return tmpid;
}
public void setTmpid(String tmpid) {
this.tmpid = tmpid;
}
}
3.頁面導入標籤
<%@ taglib prefix="candy" uri="/candy" %>
4.頁面調用標籤
<candy:msgdialog basepath="<%=request.getContextPath()%>">
請輸入登錄密碼解鎖:<br><br>
<input type="text" id="passText" >
<input type="button" onclick="valSystemLock()" value="解鎖" class="btn" style="margin-bottom: 8px;" />
<br>
<span id="valmsg" style="color: red;"></span>
</candy:msgdialog>
本例子以系統鎖定爲例實現自定義標籤的實現及使用。