案例:
一、开发<c:if>标签
IfTag.java
package com.hbsi.web.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 {/ if(test){
//处理标签体
JspFragment jf=this.getJspBody();
jf.invoke(null);
}
}
}
TestIf.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/c" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'TestIf.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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
session.setAttribute("user","zhangsan");
%>
<c:if test="${user!=null}">
aaa---aaa <br>
</c:if>
</body>
</html>
c.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>c</short-name>
<uri>/c</uri>
<tag>
<name>if</name>
<tag-class>com.hbsi.web.tag.IfTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>test</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
二、开发<c:if><c:else>标签
案例分析:
<c:if test=””>aaa</c:if>------IfTag.java
<c:else>bbb</c:else>--------ElseTag.java
共享一个boolean变量
<c:choose>---------ChooseTag.java
<c:when test=””>aaa</c:when>---------WhenTag.java
<c:otherwise>bbb</c:otherwise>---------OtherTag.java
</c:choose>
TestIf.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/c" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'TestIf.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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
session.setAttribute("user","zhangsan");
%>
<c:if test="${user!=null}">
aaa---aaa <br>
</c:if>
<br/>----------------------------------------<br/>
<c:choose>
<c:when test="${user!=null}">aaa</c:when>
<c:otherwise>bbb</c:otherwise>
</c:choose>
</body>
</html>
IfTag.java
package com.hbsi.web.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 {
if(test){
//处理标签体
JspFragment jf=this.getJspBody();
jf.invoke(null);
}
}
}
ChooseTag.java
package com.hbsi.web.tag;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class ChooseTag extends SimpleTagSupport {
private boolean flag=false;
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
@Override
public void doTag() throws JspException, IOException {
JspFragment jf=this.getJspBody();
jf.invoke(null);
}
}
WhenTag.java
package com.hbsi.web.tag;
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.isFlag()){
//处理该标签体
this.getJspBody().invoke(null);
parent.setFlag(true);
}
}
}
OtherwiseTag.java
package com.hbsi.web.tag;
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.isFlag()){
this.getJspBody().invoke(null);
parent.setFlag(true);
}
}
}
c.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>c</short-name>
<uri>/c</uri>
<tag>
<name>if</name>
<tag-class>com.hbsi.web.tag.IfTag</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.hbsi.web.tag.ChooseTag</tag-class>
<body-content>scriptless</body-content>
</tag>
<tag>
<name>when</name>
<tag-class>com.hbsi.web.tag.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.hbsi.web.tag.OtherwiseTag</tag-class>
<body-content>scriptless</body-content>
</tag>
</taglib>
三、开发迭代标签
List list=new ArrayList();
list.add(“aaa”);
list.add(“bbb”);
list.add(“ccc”);
list.add(“ddd”);
list.add(“eee”);
request.setAttribute(“list”,list);
%>
Jsp
<c:foreach items=”${list}” var=”str”> -----ForEachTag.java-----对象赋值---doTag() pageContext对象中设置了一个属性:str=”aaa”
${str}
</c:foreach>
ForEachTag.java
package com.hbsi.web.tag;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.jsp.JspException;
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 {
// TODO Auto-generated method stub
//List list=(List)items;
//Iterator it=list.iterator();
Collection collection=null;
if(items instanceof Map){
Map map=(Map)items;
//两列得集合转换成单列
collection=map.entrySet();
}else if(items instanceof Collection){
collection=(Collection)items;
}/*else if(items instanceof Object[]){
Object[] objs=(Object[])items;
collection=Arrays.asList(objs);
}else if(items instanceof int[]){
}*/
else if(items.getClass().isArray()){
collection=new ArrayList();
int length=Array.getLength(items);
for(int i=0;i<length;i++){
collection.add(Array.get(items, i));
}
}
Iterator it=collection.iterator();
while(it.hasNext()){
Object obj=it.next();//一个元素
this.getJspContext().setAttribute(var,obj);
this.getJspBody().invoke(null);
}
}
}
TextForEach.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/c" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'TestForEach.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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
List list=new ArrayList();
list.add("aaa");
list.add("bbb");
list.add("ccc");
list.add("ddd");
list.add("eee");
request.setAttribute("list",list);
%>
<c:foreach items="${list}" var="str">
${str }
</c:foreach>
<br/>----------------------------------<br/>
<%
Map map=new HashMap();
map.put("aa","zhangsan");
map.put("bb","lisi");
map.put("cc","wangwu");
request.setAttribute("map",map);
%>
<c:foreach items="${map}" var="name" >
${name }
</c:foreach>
<br/>---------------------------------<br/>
<%
Integer[] num={1,2,3,4,5};
request.setAttribute("num",num);
%>
<c:foreach items="${num}" var="i">
${i}
</c:foreach>
<br/>----------------------------------------<br/>
<%
int[] arr={6,7,8,9,10};
request.setAttribute("arr",arr);
%>
<c:foreach items="${arr}" var="index">
${index}
</c:foreach>
</body>
</html>
c.tld
<tag>
<name>foreach</name>
<tag-class>com.hbsi.web.tag.ForEachTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
打jar包
Jar包: 标签处理器以及tld文件打成一个包,打包分两种情况:
1.在命令提示符下打包:
选择路径 d:
Jar cvf mytag.jar .
Mytag是打包的包名
2.在MyElipe下打包
创建一个java工程,把创建的java类考到src下,在工程下参加一个文件夹 META-INF,把tld文件考到此文件夹下。
在工程下右击选择Export(导出),选择java下的JAR file,选择打包打包后的路径,finsh即可
四、