Jsp_demo:自訂標籤

來源:互聯網
上載者:User

標籤:

Jsp自訂標籤:

      1.繼承SimpleTagSupport,重寫doTag()。

      2.在WEB-INF/ 下配置**.tld檔案

      3.Jsp頁面引入自訂標籤:<%@ taglib uri="***" prefix="s"%>

自訂標籤demo實現:在jsp頁面將對象存入pageContext.setAttribute(),使用標籤輸出該對象的所有方法,成員變數及值。

TestTag.java:

package mytag;import java.io.IOException;import java.io.Writer;import java.lang.reflect.Field;import java.lang.reflect.Method;import javax.servlet.jsp.tagext.SimpleTagSupport;public class TestTag extends SimpleTagSupport{    private String obj;//帶屬性標籤必須為每個屬性提供對應的 setter 和 getter 方法    public String getObj() {        return obj;    }    public void setObj(String obj) {        this.obj = obj;    }    public void doTag() throws IOException{        Writer out = getJspContext().getOut();        Object object=(Object)getJspContext().getAttribute(obj);        Class<?> c=object.getClass();        Field fields[]=c.getDeclaredFields();        for(Field f:fields ){                        f.setAccessible(true);//私人成員也能訪問            try {                out.write(f.getName()+":"+f.get(object)+"</br>");            } catch (IllegalArgumentException | IllegalAccessException e) {                e.printStackTrace();            }        }        Method methods[]=c.getMethods();        for(Method m:methods){            out.write(m.toString()+"</br>");        }    }}

mytaglib.tld:

<?xml version="1.0" encoding="ISO-8859-1" ?><!DOCTYPE taglib        PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"        "http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd"><taglib>  <tlib-version>1.0</tlib-version>  <jsp-version>1.2</jsp-version>  <short-name>mytaglib</short-name>  <uri>/mytaglib</uri>  <tag>    <!-- 定義標籤名 -->    <name>info</name>    <!-- 定義標籤處理類 -->    <tag-class>mytag.TestTag</tag-class>    <!-- 定義標籤體為空白 -->    <body-content>empty</body-content>    <!-- 配置標籤屬性:driver -->    <attribute>        <name>obj</name>         <required>true</required>        <fragment>true</fragment>    </attribute>     </tag></taglib>

test.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ page import="mytag.User,java.util.*" %>    <%@ taglib uri="/mytaglib" prefix="s" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><%    pageContext.setAttribute("date", new Date());%><b><s:hello/></b><b><s:info obj="date"/></b></body></html

 

Jsp_demo:自訂標籤

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.