JSP自訂標籤(2)__JSP

來源:互聯網
上載者:User

四、編寫有標籤體的標籤

需要返回TagSupport.EVAL_BODY_INCLUDE,則可以進入標籤體;

AttributeTag.java

package org.tagext;import javax.servlet.jsp.tagext.*;import javax.servlet.jsp.*;public class AttributeTag extends TagSupport{private String name;public String getName(){return name;}public void setName(String name){this.name = name;}public int doStartTag()throws JspException{Object value = null;value = super.pageContext.getAttribute(name,PageContext.PAGE_SCOPE);if(value==null){return TagSupport.SKIP_BODY;}else{return TagSupport.EVAL_BODY_INCLUDE;}}}


xiazdong.tld

    <tag><name>attribute</name><tag-class>org.tagext.AttributeTag</tag-class><body-content>JSP</body-content><attribute><name>name</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute>  </tag>

AttributeTag.jsp

<%@ page contentType="text/html" pageEncoding="GBK"%><%@ taglib prefix="xiazdong" uri="xiazdong"%><html><head><title></title></head><body><%pageContext.setAttribute("xiazdong","12345");%><h1><xiazdong:attribute name="xiazdong"><h3>${pageScope.xiazdong}</h3></xiazdong:attribute></h1></body></html>

五、迭代標籤

迭代標籤的定義就是重複執列標籤體,經常用在輸出集合;在MVC中經常使用;

代碼執行個體:


一些變化不大的檔案我就不寫了;

IterateTag.java

package org.tagext;import javax.servlet.jsp.tagext.*;import javax.servlet.jsp.*;import java.util.*;public class IterateTag extends TagSupport{private String name;private String id;private Iterator<String> iter;public String getName(){return name;}public void setName(String name){this.name = name;}public String getId(){return id;}public void setId(String id){this.id = id;}public int doStartTag()throws JspException{Object value = null;value = super.pageContext.getAttribute(name,PageContext.PAGE_SCOPE);if(value==null||!(value instanceof List<?>)){return TagSupport.SKIP_BODY;}else{iter = ((List<String>)value).iterator();if(iter.hasNext()){super.pageContext.setAttribute(id,iter.next());return TagSupport.EVAL_BODY_INCLUDE;}else{return TagSupport.SKIP_BODY;}}}public int doAfterBody()throws JspException{if(iter.hasNext()){super.pageContext.setAttribute(id,iter.next());return TagSupport.EVAL_BODY_AGAIN;}else{return TagSupport.SKIP_BODY;}}}

IterateTag.jsp

<%@ page contentType="text/html" pageEncoding="GBK" import="java.util.*"%><%@ taglib prefix="xiazdong" uri="xiazdong"%><html><head><title></title></head><body><%List<String> list = new ArrayList<String>();list.add("A");list.add("B");list.add("C");pageContext.setAttribute("xiazdong",list);%><h1><xiazdong:iterate name="xiazdong" id="iter"><h3>${iter}</h3></xiazdong:iterate></h1></body></html>


六、BodyTagSupport類


BodyTagSupport的特點就是可以將輸出內容存在BodyContent中, 一次全部輸出;

根據上面的迭代輸出的代碼,稍微變化即可,主要變化是在IterateTag.java中,需要extends BodyTagSupport;

BodyTagSupport的常用方法:

(1)BodyTagSupport.EVAL_BODY_BUFFERED;

(2)getPreviousOut();      //獲得輸出到網頁的輸出資料流

(3)bodyContentObject Storage Service資料;

package org.tagext;import javax.servlet.jsp.tagext.*;import javax.servlet.jsp.*;import java.util.*;public class IterateTag extends BodyTagSupport{private String name;private String id;private Iterator<String> iter;public String getName(){return name;}public void setName(String name){this.name = name;}public String getId(){return id;}public void setId(String id){this.id = id;}public int doStartTag()throws JspException{Object value = null;value = super.pageContext.getAttribute(name,PageContext.PAGE_SCOPE);if(value==null||!(value instanceof List<?>)){return TagSupport.SKIP_BODY;}else{iter = ((List<String>)value).iterator();if(iter.hasNext()){super.pageContext.setAttribute(id,iter.next());return TagSupport.EVAL_BODY_BUFFERED;    //存在BodyContent中}else{return TagSupport.SKIP_BODY;}}}public int doAfterBody()throws JspException{if(iter.hasNext()){super.pageContext.setAttribute(id,iter.next());return TagSupport.EVAL_BODY_AGAIN;}else{return TagSupport.SKIP_BODY;}}public int doEndTag()throws JspException{if(super.bodyContent!=null){try{super.bodyContent.writeOut(super.getPreviousOut());    //輸出到頁面}catch(Exception e){}}return BodyTagSupport.EVAL_PAGE;}}

七、TagExtraInfo和VariableInfo的使用

在<jsp:useBean id = "">中的id屬性工作表示對象名稱,並且可以通過這個名稱調用方法;而如果要實現這種效果,必須繼承TagExtraInfo類;

我們需要另外建立一個檔案,用以表明屬性的對象能夠在指令碼中使用;

MyExtraInfo.java

package org.tag;import javax.servlet.jsp.tagext.*;public class MyTagExtraInfo extends TagExtraInfo{public VariableInfo[] getVariableInfo(TagData data){return new VariableInfo[]{new VariableInfo(data.getId(),"java.lang.String",true,VariableInfo.NESTED)};}}

寫完後在xiazdong.tld中添加:

<tei-class>//注意org.tag.MyTagExtraInfo</tei-class>
最後xiazdong.tld中以:

<tag><name>iterate</name><tag-class>org.tagext.IterateTag</tag-class><body-content>JSP</body-content><tei-class>//注意org.tag.MyTagExtraInfo</tei-class><attribute><name>name</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute><attribute><name>id</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute>  </tag>


這樣在JSP中就能夠在Scriptlet中使用;

<%@ page contentType="text/html" pageEncoding="GBK" import="java.util.*"%><%@ taglib prefix="xiazdong" uri="xiazdong"%><html><head><title></title></head><body><%List<String> list = new ArrayList<String>();list.add("A");list.add("B");list.add("C");pageContext.setAttribute("xiazdong",list);%><h1><xiazdong:iterate name="xiazdong" id="iter"><h3><%=iter%></h3>     //注意,原本是${iter}輸出;</xiazdong:iterate></h1></body></html>






相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.