簡單標籤案例

 

案例:

一、開發<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包:標籤處理器以及(META-INF)tld文件打成一個jar包

 

第一種方法:

命令提示狀態下:

D:(選擇任一個盤符)

cd mytag

jar cvf mytag.jar .

要導入到第三方  直接copy到第三方的WEB-INF下的lib下就可以,它會自動進行配製

在第三方中,可以使用此包中的自定義標籤

 

第二種方法:建一個JAVA項目,把源文件(也就是包)直接COPY到此項目的src中,再把所有的tld文件,考備到META-INF中,導出到java中的JAR文件,classpath project不選。

 

第三種方法:(最簡單的方法)直接壓縮。

發佈了66 篇原創文章 · 獲贊 15 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章