IfTag.java文件
package com.rk.tag; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.JspFragment; import javax.servlet.jsp.tagext.SimpleTagSupport; public class IfTag extends SimpleTagSupport { private boolean test; public void setTest(boolean test) { this.test = test; } @Override public void doTag() throws JspException, IOException { //根据test的返回值决定是否输出标签体内容 if(test == true) { //JspFragment jspBody = getJspBody(); //jspBody.invoke(null); this.getJspBody().invoke(null); } } }
.tld文件配置:
<tag> <name>if</name> <tag-class>com.rk.tag.IfTag</tag-class> <body-content>scriptless</body-content> <attribute> <name>test</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <type>boolean</type> </attribute> </tag>
jsp文件
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>控制重复输出标签体内容</title> </head> <body> 标签前的内容==========================<br> <rk:if test="true">条件成立</rk:if> <br/> <% boolean value = true; pageContext.setAttribute("flag", value); %> <rk:if test="${flag }">flag为true</rk:if> <rk:if test="${not flag }">flag为false</rk:if><br/> 标签后的内容========================== <br> </body> </html>
ChooseTag.java文件
package com.rk.tag; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.SimpleTagSupport; public class ChooseTag extends SimpleTagSupport { //不是属性,而是临时变量,用于在子标签中间传递“状态” private boolean executed = false; public boolean isExecuted() { return executed; } public void setExecuted(boolean executed) { this.executed = executed; } @Override public void doTag() throws JspException, IOException { this.getJspBody().invoke(null); } }
.tld文件配置:
<tag> <name>choose</name> <tag-class>com.rk.tag.ChooseTag</tag-class> <body-content>scriptless</body-content> </tag>
WhenTag.java文件
package com.rk.tag; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.JspTag; import javax.servlet.jsp.tagext.SimpleTagSupport; public class WhenTag extends SimpleTagSupport { private boolean test; public void setTest(boolean test) { this.test = test; } @Override public void doTag() throws JspException, IOException { //获取父标签 ChooseTag parent = (ChooseTag)this.getParent(); //如果父标签中的内容还没有执行过,并且当前的判断条件(test)为true,那么就执行输出 if(!parent.isExecuted() && test==true) { this.getJspBody().invoke(null); parent.setExecuted(true); } } }
.tld文件配置:
<tag> <name>when</name> <tag-class>com.rk.tag.WhenTag</tag-class> <body-content>scriptless</body-content> <attribute> <name>test</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <type>boolean</type> </attribute> </tag>
OtherWiseTag.java文件
package com.rk.tag; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.JspTag; import javax.servlet.jsp.tagext.SimpleTagSupport; public class OtherWiseTag extends SimpleTagSupport { @Override public void doTag() throws JspException, IOException { //获取父标签 ChooseTag parent = (ChooseTag)this.getParent(); if(!parent.isExecuted()) { this.getJspBody().invoke(null); } } }
.tld文件配置:
<tag> <name>otherwise</name> <tag-class>com.rk.tag.OtherWiseTag</tag-class> <body-content>scriptless</body-content> </tag>
jsp文件
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@taglib uri="http://www.lsieun.com/rk" prefix="rk" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>控制重复输出标签体内容</title> </head> <body> 标签前的内容==========================<br/> <rk:choose> <rk:when test="${10>5 }"> 10>5 </rk:when> <rk:when test="${10>6 }"> 10>6 </rk:when> <rk:otherwise> otherwise </rk:otherwise> </rk:choose><br/> 标签后的内容========================== <br/> </body> </html>
ForEachTag.java文件
package com.rk.tag; import java.io.IOException; import java.util.Collection; import java.util.List; import java.util.Map; import javax.servlet.jsp.JspException; import javax.servlet.jsp.PageContext; import javax.servlet.jsp.tagext.SimpleTagSupport; public class ForEachTag extends SimpleTagSupport { private Object items; private String var; public void setItems(Object items) { this.items = items; } public void setVar(String var) { this.var = var; } @Override public void doTag() throws JspException, IOException { PageContext pageContext = (PageContext)this.getJspContext(); Collection colls = null; if(items instanceof List){ colls = (List)items; } if(items instanceof Map){ Map map = (Map)items; colls = map.entrySet(); } for(Object object:colls){ //把每个对象放入域对象中(pageContext) pageContext.setAttribute(var, object); //显示标签体内容 this.getJspBody().invoke(null); } } }
.tld文件配置:
<tag> <name>forEach</name> <tag-class>com.rk.tag.ForEachTag</tag-class> <body-content>scriptless</body-content> <attribute> <name>items</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <type>java.lang.Object</type> </attribute> <attribute> <name>var</name> <required>true</required> <rtexprvalue>false</rtexprvalue> <type>java.lang.String</type> </attribute> </tag>
jsp文件
<%@ page language="java" import="java.util.*,com.rk.entity.Student" pageEncoding="utf-8"%> <%@taglib uri="http://www.lsieun.com/rk" prefix="rk" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>控制重复输出标签体内容</title> </head> <body> 标签前的内容==========================<br/> <% List<Student> listOfStudent = new ArrayList<Student>(); listOfStudent.add(new Student("lucy",10)); listOfStudent.add(new Student("lily",10)); listOfStudent.add(new Student("tom",10)); pageContext.setAttribute("list", listOfStudent); Map<String,Student> mapOfStudent = new HashMap<String,Student>(); mapOfStudent.put("100",new Student("lucy",11)); mapOfStudent.put("101",new Student("lily",11)); mapOfStudent.put("102",new Student("tom",12)); pageContext.setAttribute("map", mapOfStudent); %> <rk:forEach items="${list }" var="stu"> 姓名:${stu.name }--年龄:${stu.age }<br/> </rk:forEach> <br/> <hr/> <rk:forEach items="${map }" var="entry"> 编号:${entry.key}--姓名:${entry.value.name }--年龄:${entry.value.age }<br/> </rk:forEach> <br/> 标签后的内容========================== <br/> </body> </html>