給出一個Taglib的BODY_BUFFERED,bodyContent的例子
BODY_BUFFERED,bodyContent的例子:
馬克- to-win:馬克 java社區(qū):防盜版實名手機尾號: 73203。
有時你標簽對應的java代碼會從數(shù)據(jù)庫或其他網(wǎng)絡渠道獲取數(shù)據(jù)。這些數(shù)據(jù)在最終返回jsp顯示之前,需要一個過濾修改的過程。比如去掉某些政治敏感的詞語或像本例一樣加入一個詞語“馬克-to-win”。這時就需要你用BODY_BUFFERED技術。顧名思義就是要把你返回jsp顯示的body先 buffer一下,放在BodyTagSupport的bodyContent里,你可以隨意修改之,最后再返回jsp,但是前提條件是在 doStartTag中的返回值要寫成EVAL_BODY_BUFFERED,這樣系統(tǒng)就會開辟bodyContent這個Buffer,供你運作。
例 1.2.6:
<%@ page contentType="text/html; charset=GBK" %>
<%@ taglib uri="/WEB-INF/tagExampleLib.tld" prefix="greeter" %>
<html>
<body>
<greeter:Hello>
Now 時間 is: <%=new java.util.Date() %> <br>
</greeter:Hello>
結束
</body>
</html>
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>mark-to-win</shortname>
<tag>
<name>Hello</name>
<tagclass>com.marktowin.HelloWorldTag</tagclass>
</tag>
</taglib>
/*順序是: 4)doStartTag
4.5) doAfterBody
5)doEndTag*/
package com.marktowin;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
public class HelloWorldTag extends BodyTagSupport {
int count = 2;
public int doStartTag() {
System.out.println("doStartTag");
return EVAL_BODY_BUFFERED;
}
public int doAfterBody() {
System.out.println("doAfterBody.");
while (count > 0) {
count--;
/* EVAL_BODY_AGAIN requires that the body of the tag is evaluated. */
return EVAL_BODY_AGAIN;
}
return SKIP_BODY;
}
public int doEndTag() {
System.out.println("doEndTagqqq");
/*the following try catch will be no use if you use return BodyTagSupport.EVAL_BODY_INCLUDE,
but if you use return BodyTagSupport.EVAL_BODY_BUFFERED;, you must use this try catch,
otherwise, there will be no output from IE, because the former won't use bodyContent. the
former use "Evaluate body into existing out stream" */
try{
if(bodyContent != null) {
System.out.println("body content qixy is not null");
/*public JspWriter getEnclosingWriter() Get the enclosing JspWriter.
writeOut(Writer out) Write the contents of this BodyContent into a Writer.
*/
System.out.println("current bodyContent is "+bodyContent.getString());
/*假如你用下句,原來的東西就沒了,但后面的馬克-to-win還是能輸出出來*/
// bodyContent.clearBody();
/*write的作用和append附加一樣,write沒有先清除過去body的作用*/
bodyContent.write("馬克-to-win");
//bodyContent.writeOut(bodyContent.getEnclosingWriter());
/*writeOut(Writer out)
Write the contents of this BodyContent into a Writer.
不寫下句瀏覽器中沒有輸出 */
bodyContent.writeOut(pageContext.getOut());
}
}catch(IOException e){
e.printStackTrace();
}
return EVAL_PAGE;
}
}
運行jsp后,瀏覽器中輸出結果是:
Now 時間 is: Sun Apr 30 22:01:13 CST 2017
Now 時間 is: Sun Apr 30 22:01:13 CST 2017
Now 時間 is: Sun Apr 30 22:01:13 CST 2017
馬克-to-win 結束
運行jsp后,console中輸出結果是:
doStartTag
doAfterBody.
doAfterBody.
doAfterBody.
doEndTagqqq
body content qixy is not null
current bodyContent is
Now 時間 is: Sun Apr 30 22:01:13 CST 2017 <br>
Now 時間 is: Sun Apr 30 22:01:13 CST 2017 <br>
Now 時間 is: Sun Apr 30 22:01:13 CST 2017 <br>