黑马程序员技术交流社区
标题:
java 自定义标签
[打印本页]
作者:
周志龙
时间:
2013-10-10 10:34
标题:
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 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.页面导入标签
[html]
<%@ taglib prefix="candy" uri="/candy" %>
复制代码
4.页面调用标签
[html]
<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>
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2