JSP——JSTL定制标签 - 递归标签显示属性结构
编写定制标签分为三个步骤:编写标签处理器、配置标签、使用标签。
1.标签处理器
标签处理器和标签是一一对应的关系
package com.oolong.utils.customtags; import java.io.IOException; import java.util.List; import javax.servlet.jsp.JspContext; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.SimpleTagSupport; import com.oolong.personal.model.KLPointModel; import com.oolong.utils.tree.TreeModel; public class ShowTreeTag extends SimpleTagSupport { private List<TreeModel<KLPointModel>> items; public void setItems(List<TreeModel<KLPointModel>> items) { this.items = items; } @Override public void doTag() throws JspException, IOException { JspContext jspContext = getJspContext(); JspWriter writer = jspContext.getOut(); writer.print(generateHTML(items)); } private String generateHTML(List<TreeModel<KLPointModel>> items) { StringBuilder sb = new StringBuilder(); sb.append("<ul>"); for(TreeModel<KLPointModel> treeModel : items) { sb.append("<li value='"); sb.append(treeModel.getNode().getWid()); sb.append("' >"); sb.append(treeModel.getNode().getName()); if (treeModel.getChildren() != null && treeModel.getChildren().size() > 0) { sb.append(generateHTML(treeModel.getChildren())); } sb.append("</li>"); } sb.append("</ul>"); return sb.toString(); } }
对于需要在标签中使用的属性,此处给定这个属性getter和setter方法即可,程序在执行时,这些属性会被自动注入:
private List<TreeModel<KLPointModel>> items; public void setItems(List<TreeModel<KLPointModel>> items) { this.items = items; }
2.配置标签
此处配置标签的名称、属性以及对应的标签处理器等
<?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/web-jsptaglibrary_2_1.xsd" version="2.1"> <description> showTreeTag </description> <tlib-version>1.0</tlib-version> <short-name>ShowTreeTag</short-name> <tag> <name>showTreeTag</name> <tag-class>com.oolong.utils.customtags.ShowTreeTag</tag-class> <body-content>scriptless</body-content> <attribute> <name>items</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
配置标签对应的解析类:
<tag-class>com.oolong.utils.customtags.ShowTreeTag</tag-class>
配置标签属性可以介绍解析对象:
<rtexprvalue>true</rtexprvalue>
3.使用标签
这个标签的作用是迭代的显示一个树形结构
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="f" uri="http://java.sun.com/jsp/jstl/fmt"%> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> <%@ taglib uri="/WEB-INF/ShowTreeTag.tld" prefix="Oolong"%> <div class="col-sm-5 col-md-4 sidebar" id="treepanel"> <div class="easy-tree"> <c:forEach items="${pointTree}" var="item"> <ul> <li value="${item.domain.wid}">${item.domain.name} <c:if test="${item.children != null && fn:length(item.children) > 0}"> <Oolong:showTreeTag items="${item.children}" /> </c:if> </li> </ul> </c:forEach> </div> </div>
在头部引入自定义标签的配置文件:
<%@ taglib uri="/WEB-INF/ShowTreeTag.tld" prefix="Oolong"%>
使用标签:
<Oolong:showTreeTag items="${item.children}" />
如果标签的属性值想要像这里这样接受对象作为参数,那就需要在配置标签时设置可以解析对象