java--自訂標籤(tag、tld兩種)

來源:互聯網
上載者:User

1.一篇不錯的 java自訂標籤的文章地址:http://gaoshu2006.blog.sohu.com/113222643.html

2.sun公司java自訂標籤原文地址:http://java.sun.com/developer/technicalArticles/xml/WebAppDev3/

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

jsp自訂標籤 Tag檔案版

 

實現一個與上篇文章類似的Select標籤功能
1.在WEB-INF/tags/select.tag

<%@ tag body-content="empty" %>
<%@ tag dynamic-attributes="tagAttrs" %>
<%@ attribute name="optionsList" type="java.util.List" required="true" rtexprvalue="true"%>
<%@ attribute name="name" required="true"%>
<%@ attribute name="size" required="true" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>


<select name="${name }" size="${size }"
   <c:forEach var="attrEntry" items="${tagAttrs }">
     ${attrEntry.key}="${attrEntry.value }"
   </c:forEach>
>
   <c:forEach var="option" items="${optionsList}">
     <option value="${option }">${option}</option>
    </c:forEach>
 </select>
這裡要注意tag檔案只能放在如下位置:
1.WEB-INF/tags
2.WEB-INF/tags的子目錄
3.WEB-INF/lib中jar包的META-INF/tags
4.WEB-INF/lib中jar包的META-INF/tags下的子目錄
5.jar包中的tag檔案需要tld
添加jstl.jar與standard.jar到WEB-INF/lib目錄,還有一點就是上面標紅的部分:不要使用http://java.sun.com/jstl/core這個url,否則會報foreach中的item屬性有問題

2.在jsp中的使用

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ taglib prefix="formTag" tagdir="/WEB-INF/tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
 List<String> colorList = new ArrayList<String>();
    colorList.add("red");
    colorList.add("blue");
    colorList.add("white");
    request.setAttribute("colorList",colorList);
%>

<form action="" method="post">
 <formTag:select name="color" size="1" optionsList="${requestScope.colorList}"  style="width:140px"/>
</form>
</body>
</html>

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

 

jsp 自訂標籤

 

jsp標籤有兩組api
JspTag ->SimpleTag ->SimpleTagSupport
JspTag ->Tag ->IterationTag->BodyTag
第二組是classic的,比較早的使用方式,doStartTag(),doEndTag()有N多傳回值的那種,使用起來也確實不方便,今天學到了另一個使用第一組api方式的,讓人大快人心,貼碼
例子是一個Select的標籤,支援動態屬性設定
1.編寫標籤類

public class SelectTagHandler extends SimpleTagSupport implements DynamicAttributes {
 private static final String ATTR_TEMPLATE = "%s='%s'";
 private static final String OPTION_TEMPLATE = "<option value='%1$s'>%1$s</option>";
 private List optionsList;
 private String name;
 private String size;
 private Map<String, Object> tagAttrs = new HashMap<String, Object>();

 public void setName(String name) {
  this.name = name;
 }

 public void setSize(String size) {
  this.size = size;
 }

 public void setOptionsList(List optionsList) {
  this.optionsList = optionsList;
 }

 @Override
 public void doTag() throws JspException, IOException {
  PageContext pageContext = (PageContext) getJspContext();
  JspWriter out = pageContext.getOut();
  out.print("<select ");
  out.print(String.format(ATTR_TEMPLATE, "name", this.name));
  out.print(String.format(ATTR_TEMPLATE, "size", this.size));
  for (String attrName : tagAttrs.keySet()) {
   String attrDefinition = String.format(ATTR_TEMPLATE, attrName, tagAttrs.get(attrName));
   out.print(attrDefinition);
  }
  out.print(">");

  for (Object option : this.optionsList) {

   String optionTag = String.format(OPTION_TEMPLATE, option.toString());
   out.println(optionTag);
  }
  out.println("</select>");
 }

 @Override
 public void setDynamicAttribute(String uri, String name, Object value) throws JspException {
  tagAttrs.put(name, value);
 }
}
看到沒,代碼如此的簡潔,動態屬性配置也十分的方便,不用寫N多個setter與getter方法. 2.編寫tld檔案WebRoot/tld/select.tld

<?xml version="1.0" encoding="UTF-8" ?>

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3g.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"
version="2.0">
 <tlib-version>1.2</tlib-version>
 <jsp-version>1.2</jsp-version>
 <short-name>Forms Taglib</short-name>
 <uri>http://hi.baidu.com/tags/forms</uri>
 <description>
  An example tab library of replacements for the html form tags.
 </description>
 
 <tag>
  <name>select</name>
  <tag-class>com.baidu.hi.tag.SelectTagHandler</tag-class>
  <body-content>empty</body-content>
  
  <attribute>
   <name>optionsList</name>
   <required>true</required>
   <rtexprvalue>true</rtexprvalue>
   <type>java.util.List</type>
  </attribute>
  
  <attribute>
   <name>name</name>
   <required>true</required>
  </attribute>
  
  <attribute>
   <name>size</name>
   <required>true</required>
  </attribute>
  
  <dynamic-attributes>true</dynamic-attributes>
 </tag>
</taglib>
3.在jsp中的使用

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page import="java.util.*" %>
<%@ taglib  prefix="formTags"  uri="/tld/select.tld"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="java.util.ArrayList"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
 List<String> colorList = new ArrayList<String>();
    colorList.add("red");
    colorList.add("blue");
    colorList.add("white");
    request.setAttribute("colorList",colorList);
%>
<form action="" method="post">
 <formTags:select name="color" size="1" optionsList="${requestScope.colorList}" style="width:140px"/>
</form>
</body>
</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.