<?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 http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>潭州学院中级班jsp的自定义函数标签</description>
<display-name>tz functions</display-name>
<tlib-version>1.0</tlib-version>
<short-name>tz</short-name><!-- prefix="tz" -->
<uri></uri>
<!-- ****************************************************Tag自定义标签开始************************************** -->
<tag>
<!-- 标签的描述 -->
<description>自定循环标签</description>
<!-- 标签的名称 -->
<name>loop</name>
<!-- 对应标签的类 -->
<tag-class>com.tz.tag.TzLoopTag </tag-class>
<!--
它决定标签的格式,(就是决定是否有标签体 )
empty:没有标签体<tz:loop />
scriptless:你的标签体可以用EL表达式或者JSP的动作元素,但是不能用JSP脚本<%%>
JSP:可以支持JSP动作代表 解析El表达和JSP所有的动作部分
tagdependent:它忽略动作部分,打印纯文本
-->
<body-content>scriptless</body-content>
<attribute>
<!-- 属性的名称 -->
<name>items</name>
<!-- 属性是否是必须,如果不写,会报错,默认false -->
<required>true</required>
<!-- 是否在运行期间支持el表达 默认是:false-->
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<!-- 属性的名称 -->
<name>var</name>
<!-- 属性是否是必须,如果不写,会报错,默认false -->
<required>true</required>
<!-- 是否在运行期间支持el表达 默认是:false-->
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
下面是java代码
package com.lbm.tag;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import com.lbm.dao.DeskDao;
public class Ttag extends TagSupport{
private List<DeskDao> items;
private String var;
Iterator iterator =null;
public void setItems(List<DeskDao> items) {
this.items = items;
}
public void setVar(String var) {
this.var = var;
}
@Override
public int doStartTag() throws JspException {
if(items == null || !(items instanceof Collection))
//返回SKIP_BODY(默认),执行doEndTag()
return SKIP_BODY;
else {
iterator = items.iterator();
if(iterator.hasNext()){
this.pageContext.setAttribute(var, iterator.next());
return EVAL_BODY_INCLUDE;
}else{
return SKIP_BODY;
}
}
}
@Override
public int doAfterBody() throws JspException {
if(iterator.hasNext()){
System.out.println("3======================>doAfterBody-for");
this.pageContext.setAttribute(var, iterator.next());
return EVAL_BODY_AGAIN;
}else{
return SKIP_BODY;
}
}
@Override
public int doEndTag() throws JspException {
return super.doEndTag();
}
@Override
public void release() {
super.release();
}
}
<%@page import="com.tz.dao.CommentDao"%>
<%@page import="com.tz.model.Comment"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/WEB-INF/tld/tz.tld" prefix="tz"%>
<!DOCTYPE HTML>
<html>
<head>
<title>My JSP 'tag.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<%
List<Comment> comments = CommentDao.findComments(1, 0, 10);
pageContext.setAttribute("comments", comments);
%>
<h1>dsfsdfsdfsdf</h1>
<tz:loop items="${comments}" var="comment">
<div>${comment.content}</div><br/>
</tz:loop>
自定义,bodyTagSupport的讲解和分析,和业务的自定义标签(织梦CMS)标签的形态
标签的进阶,if elseif else/if else
做项目是
<h1>是的发士大夫士大夫士大夫 士大夫</h1>
<h1>是的发士大夫士大夫士大夫 士大夫</h1>
<h1>是的发士大夫士大夫士大夫 士大夫</h1>
<h1>是的发士大夫士大夫士大夫 士大夫</h1>
<h1>是的发士大夫士大夫士大夫 士大夫</h1>
<h1>是的发士大夫士大夫士大夫 士大夫</h1>
</body>
</html>