心得13--jsp简单标签案例分析.doc

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="example"  prefix="example"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>My JSP '4.jsp' starting page</title>
    
  </head>
  
  <body>
  <%
    List list = new ArrayList();
    list.add(1);
    list.add("aa");
    list.add("bb");
    list.add(2);
    request.setAttribute("list",list);
   %>
    <example:forEach var="str" items="${list}">
    ${ str }<br>
    </example:forEach>
  </body>
</html>
开发choose标签
标签控制类:
ChooseTag.java
package com.csdn.web.example;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class ChooseTag extends SimpleTagSupport {
	private boolean isOk;

	public boolean isOk() {
		return isOk;
	}

	public void setOk(boolean isOk) {
		this.isOk = isOk;
	}

	@Override
	public void doTag() throws JspException, IOException {
		this.getJspBody().invoke(null);
	}
}
WhenTag.java
package com.csdn.web.example;

import java.io.IOException;

import javax.servlet.jsp.JspException;
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();
		if(test && !parent.isOk()){
			this.getJspBody().invoke(null);
			parent.setOk(true);
		}
	}
}
OtherwiseTag.java
package com.csdn.web.example;

import java.io.IOException;

import javax.servlet.jsp.JspException;
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.isOk()){
			this.getJspBody().invoke(null);
			parent.setOk(true);
		}
	}
}
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 '3.jsp' starting page</title>
    
  </head>
  
  <body>
    <example:choose>
    <example:when test="<%=false %>">asd</example:when><!-- 这里,标签体中如果没有内容会报错(空指针异常) -->
    <example:otherwise>fghhjj</example:otherwise>
    </example:choose>
  </body>
</html>
2.  其他案例
设置带属性的标签:
标签控制类
package com.csdn.web.simple;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class Demo5 extends SimpleTagSupport{

	private int count;
	public void setCount(int count) {
		this.count = count;
	}
	@Override
	public void doTag() throws JspException, IOException {
		JspFragment jf = this.getJspBody();
		for(int i=0;i<=count;i++){			
			jf.invoke(null);
		}
	}	
}
Tld描述文档
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
                        "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
 <tlib-version>1.0</tlib-version>
 <jsp-version>1.2</jsp-version>
 <short-name>simple</short-name>
 <uri>http://www.csdn.com</uri>
 
 <tag>
 <name>demo1</name>
 <tag-class>com.csdn.web.simple.Demo1</tag-class>
 <body-content>scriptless</body-content>
 </tag>
 
 <tag>
 <name>demo2</name>
 <tag-class>com.csdn.web.simple.Demo2</tag-class>
 <body-content>scriptless</body-content>
 </tag>
 
 <tag>
 <name>demo3</name>
 <tag-class>com.csdn.web.simple.Demo3</tag-class>
 <body-content>scriptless</body-content>
 </tag>
 
 <tag>
 <name>demo4</name>
 <tag-class>com.csdn.web.simple.Demo4</tag-class>
 <body-content>scriptless</body-content>
 </tag>
 
 <tag>
 <name>demo5</name>
 <tag-class>com.csdn.web.simple.Demo5</tag-class>
 <body-content>scriptless</body-content>
 <attribute>
 <name>count</name>
 <required>true</required>
 <rtexprvalue>true</rtexprvalue>
 </attribute>
 </tag>
</taglib>
JSP文件
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.csdn.com"  prefix="simple"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>设置带属性的标签</title>
    
  </head>
  
  <body>
    <simple:demo5 count="10">
       <h3>dddddddddd</h3>
    </simple:demo5>
  </body>
</html>
修改标签体内容:
标签控制类
package com.csdn.web.simple;

import java.io.IOException;
import java.io.StringWriter;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class Demo4 extends SimpleTagSupport {

	@Override
	public void doTag() throws JspException, IOException {
		JspFragment jf = this.getJspBody();
		StringWriter sw = new StringWriter();
		jf.invoke(sw);
		String body = sw.toString().toUpperCase();
		this.getJspContext().getOut().write(body);
	}
}
JSP文件
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.csdn.com"  prefix="simple"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>修改标签体内容</title>
    
  </head>
  
  <body>
    <simple:demo4>
    <h3>nnnnnnnnnnnn</h3>
    </simple:demo4>
  </body>
</html>


posted @ 2012-11-19 17:19  yangkai_keven  阅读(159)  评论(0编辑  收藏  举报