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>