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 ,
- 更多JAVA疑问解答:
- java怎么在线读取ftp服务器上的文件内容
- 关于程序员的职业规划
- HTML和JSP矛盾吗?
- java小程序如何打包?
- java怎么split路径文件名?
- 关于Hibernate实体自身多对一的抓取问题
- 关于apache2+tomcat群集出现的问题
- SSH 导入导出excel 谁有这块的资料吗?
- springmvc 加载一个jsp页面执行多个方法 报404
- 关于用jquery 导入 excel
- java对时间进行循环放到List中
- 一个图片的输入输出程序,第一次调用某方法会中断,第二次调用则正常
- 请上过传智播客的人说一下传智播客怎么样呀!是不是像它说的那样好呀!
- spring的schema怎么配置
- 【菜鸟求助】SSH中怎么从JSP页面往后台传值呢