java入门以及基本自定义标签
java吧
全部回复
仅看楼主
level 3
xmvip01 楼主
http://click.aliyun.com/m/27924 名师 java入门课堂
1、编写**.tld文件
  [html]
  <?xml version="1.0" encoding="UTF-8" ?>
  <taglib xmlns="java.sun./xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="java.sun./xml/ns/j2ee java.sun./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>");
  sb.append("#msgbox"
  + tmpid
  + "{width:"
  + boxwidth
  + ";background: #D6D3CE; border:1px #848284 solid;padding:0px;margin:0 auto;}");
  sb.append(".msgicon{float:left;background-image:url("
  + basepath
  + "/img/msgtitle_icon.jpg); background-repeat:no-repeat; height:20px;width:20px;}");
  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;}");
  // sb.append(".msgclose{float:left;background-image:url("
  // + basepath
  // + "/img/btn_close.jpg); background-repeat:no-repeat; height:20px; width:20px; cursor:pointer;}");
  sb.append(".msgmainbox{clear:both; border-top:1px #848284 solid;text-align:left;padding:20px;overflow: auto;}");
  sb.append("</style>");
  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>");
  sb.append("<div id='msgprompt"
  + tmpid
  + "' style='clear:both;top:"
  + top
  + ";left:0;position: absolute; z-index: 10002; width:100%;text-align:center'>");
  sb.append("<div id='msgbox" + tmpid + "'>");
  sb.append("<div class='msgicon'></div>");
  sb.append("<div class='msgtilte'>" + title + "</div>");
  // sb.append("<div class='msgclose' onClick='closemask" + tmpid
  // + "()'></div>");
  sb.append("<div class='msgmainbox'>");
  // 插入标签体中的提示信息内容
  sb.append(writer.toString()。trim() + "");
  sb.append("</div>");
  sb.append("</div>");
  sb.append("</div>");
  // sb.append("<script language='javascript'>");
  // sb.append("function closemask" + tmpid + "(){");
  // sb.append("document.getElementById('mask" + tmpid
  // + "')。style.display='none';");
  // sb.append("document.getElementById('msgprompt" + tmpid
  // + "')。style.display='none';");
  // sb.append("}");
  // sb.append("</script>");
  sb.append("</div>");
  // 输出处理结果到页面上
  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://click.aliyun.com/m/27924/ 名师 java入门课堂
2018年04月18日 07点04分 1
1