1.一篇不錯的 java自定義標(biāo)簽的文章地址:http://gaoshu2006.blog.sohu.com/113222643.html
2.sun公司java自定義標(biāo)簽原文地址:http://java./developer/technicalArticles/xml/WebAppDev3/
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
jsp自定義標(biāo)簽 Tag文件版
實現(xiàn)一個與上篇文章類似的Select標(biāo)簽功能 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./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目錄,還有一點就是上面標(biāo)紅的部分:不要使用http://java./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./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 自定義標(biāo)簽
jsp標(biāo)簽有兩組api JspTag ->SimpleTag ->SimpleTagSupport JspTag ->Tag ->IterationTag->BodyTag 第二組是classic的,比較早的使用方式,doStartTag(),doEndTag()有N多返回值的那種,使用起來也確實不方便,今天學(xué)到了另一個使用第一組api方式的,讓人大快人心,貼碼 例子是一個Select的標(biāo)簽,支持動態(tài)屬性設(shè)置 1.編寫標(biāo)簽類
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); } } 看到?jīng)],代碼如此的簡潔,動態(tài)屬性配置也十分的方便,不用寫N多個setter與getter方法.
2.編寫tld文件WebRoot/tld/select.tld <?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java./xml/ns/j2ee" xmlns:xsi="http://www./2001/XMLSchema-instance" xsi:schemaLocation="http://java./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./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>
|