日期:2014-05-16 浏览次数:20329 次
1. 使用标签控制页面逻辑案例: 模拟sun公司开发的标签 – 开发<c:if>标签 – 开发<c:if><c:else>标签 – 开发迭代标签 开发<c:if>标签案例: 标签控制类: package com.csdn.web.example; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.JspFragment; import javax.servlet.jsp.tagext.SimpleTagSupport; public class Demo1 extends SimpleTagSupport { private boolean test; public void setTest(boolean test) { this.test = test; } @Override public void doTag() throws JspException, IOException { if(test){ JspFragment jf = this.getJspBody(); jf.invoke(null); } } } Tld描述文档: <?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"> <tlib-version>1.0</tlib-version> <short-name>example</short-name> <uri>example</uri> <tag> <name>if</name> <tag-class>com.csdn.web.example.Demo1</tag-class> <body-content>scriptless</body-content> <attribute> <name>test</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <tag> <name>choose</name> <tag-class>com.csdn.web.example.ChooseTag</tag-class> <body-content>scriptless</body-content> </tag> <tag> <name>when</name> <tag-class>com.csdn.web.example.WhenTag</tag-class> <body-content>scriptless</body-content> <attribute> <name>test</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <tag> <name>otherwise</name> <tag-class>com.csdn.web.example.OtherwiseTag</tag-class> <body-content>scriptless</body-content> </tag> <tag> <name>forEach</name> <tag-class>com.csdn.web.example.ForEach</tag-class> <body-content>scriptless</body-content> <attribute> <name>var</name> <required>true</required> </attribute> <attribute> <name>items</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib> JSP文件: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="example" prefix="example"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP '1.jsp' starting page</title> </head> <body> <example:if test="${ user==null}"> <h5>aaaaaaaaaaa</h5> </example:if> </body> </html> 开发迭代(forEach)标签 标签控制类: package com.csdn.web.example; import java.io.IOException; import java.util.Iterator; import java.util.List; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.SimpleTagSupport; public class ForEach extends SimpleTagSupport { private String var; private List items; public void setVar(String var) { this.var = var; } public void setItems(List items) { this.items = items; } @Override public void doTag() throws JspException, IOException { Iterator it = items.iterator(); while(it.hasNext()){ Object obj = it.next(); this.getJspContext().setAttribute(var,obj); this.getJspBody().invoke(null); } } } JSP文件: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="examp