包含標籤體
到此爲止,你看到的所有定製的標籤都忽略了標籤體,因此以以下形式的獨立標籤使用:
<prefix:tagName />
在這一節中,我們將看到如何定義使用體內容並按以下方法書寫的標籤:
<prefix:tagName> body </prefix:tagName>
1、標籤體:標籤處理程序類
在上一節中,標籤處理程序定義了一個返回SKIP_BODY的doStartTag方法。爲了只是系統以用在新元素的開始和結束標籤之間的標籤體,doStartTag方法應該改爲返回EVAL_BODY_INCLUDE。體內容可以包含JSP的基本元素,指令和動作,就像改業的其餘部分一樣。JSP結果在頁轉換時被轉換成servlet代碼,並且該代碼在請求是調用。
如果你使用了標籤體,那麼可能像載體的後面和前面採取某個動作。可使用doEndTag方法指定該動作。在大多數情況中,你會在完成標籤後繼續做改業的其餘部分,因此doEndTag方法應該返回EVAL_PAGE。如果想要中止對業的其餘部分的處理,可改爲返回值SKIP_PAGE。
程序清單4-1定義了作爲標題元素的標籤,該標籤比標準HTML的H1到H6元素更爲靈活。該新元素允許一個精確的字體尺寸、一個首選字體名的列表(對該客戶機系統有效的第一個向將被使用)、前景顏色、背景顏色、邊框和對齊方式(LEFT,CENTER,RIGHT)。對其功能只能用於H1導H6元素。標題通過使用一個以括在SPAN元素中的已經嵌入樣式表屬性的單組件表實現。doStartTag方法產生TABLE和SPAN的開始標記,然後返回EVAL_BODY_INCLUDE只是系統包含標籤體。DoEndTag方法產生</SPAN></TABLE>標記,然後返回EVAL_PAGE繼續進行常規的頁處理。可使用不同的setAttributeName方法處理像bgColor和fontSize這樣的屬性。
程序清單4-1 HeadingTag.java
package moreservlets.tags;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
/*Generates an html heading whith the specified background
* color,foreground color,alignment,font,and font size,
* You can also turn on a border around it, which normally
* just abrely encloses the heading, but which can also
* stretch wider,All attributes except the background
* color are optional.
*/
publicclass HeadingTag extends TagSupport
{
private String bgColor;
private String color=null;
private String align="CENTER";
private String fontSize="36";
private String fontList="Arial,Helvetica,sans-serif";
private String border="0";
private String width=null;
publicvoid setBgColor(String bgColor)
{
this.bgColor=bgColor;
}
publicvoid setColor(String color)
{
this.color=color;
}
publicvoid setAlign(String align)
{
this.align=align;
}
publicvoid setFontSize(String fontSize)
{
this.fontSize=fontSize;
}
publicvoid setFontList(String fontList)
{
this.fontList=fontList;
}
publicvoid setBorder(String border)
{
this.border=border;
}
publicvoid setWidth(String width)
{
this.width=width;
}
publicint doStartTag()
{
try{
JspWriter out=pageContext.getOut();
out.print("<table bordr="+border+" bgcolor=/""
+bgColor+"/""+" align=/""+align+"/"");
if(width==null)
{
out.print(" width=/""+width+"/"");
}
out.print("><tr><th>");
out.print("<span style=/""+"font-size: "+fontSize+
"px;"+""+fontList+";");
if(color!=null)
{
out.print("color: "+color+";");
}
out.print("/">"); //End of span.
}catch(IOException ioe){
System.out.println("Error in HeadingTag: "+ioe);
}
return(EVAL_BODY_INCLUDE);
}
publicint doEndTag()
{
try{
JspWriter out=pageContext.getOut();
out.print("</span></table>");
}catch(IOException ioe){
System.out.println("Error in HeadingTag: "+ioe);
}
return(EVAL_PAGE); //Continue with rest of JSP page
}
}
2、標籤體:標籤庫描述符文件
在使用體內容的標籤的tag元素中只有唯一一個新功能:body-content元素應該按如下包含JSP值。
<body-content> JSP </body-content>
但是,請記住,body-content是可選的(JSP是缺省值),且主要針對IDE,name,tagclass,description和attribute元素的使用方法與前面描述的相同,見程序清單的黑體部分。
程學清單4-2 msajsp-taglib.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>msajsp-tags</short-name>
<description>
A simple tab for the examples
</description>
<tag>
<name>example</name>
<tag-class>moreservlets.tags.ExampleTag</tag-class>
<body-content>empty</body-content>
<description> Output some strings </description>
</tag>
<tag>
<name>simplePrime</name>
<tag-class>moreservlets.tags.SimplePrimeTag</tag-class>
<body-content>empty</body-content>
<description> Generates primes </description>
</tag>
<tag>
<name>prime</name>
<tag-class>moreservlets.tags.PrimeTag</tag-class>
<body-content>empty</body-content>
<description> Generates some primes </description>
<attribute>
<name>length</name>
<required>false</required>
</attribute>
</tag>
<tag>
<name>heading</name>
<tag-class>moreservlets.tags.HeadingTag</tag-class>
<body-content>JSP</body-content>
<description> Outputs a 1-cell table used as a heading </description>
<attribute>
<name>bgColor</name>
<required>true</required>
</attribute>
<attribute>
<name>color</name>
<required>false</required>
</attribute>
<attribute>
<name>align</name>
<required>false</required>
</attribute>
<attribute>
<name>fontSize</name>
<required>false</required>
</attribute>
<attribute>
<name>fontList</name>
<required>false</required>
</attribute>
<attribute>
<name>border</name>
<required>false</required>
</attribute>
<attribute>
<name>width</name>
<required>false</required>
</attribute>
</tag>
</taglib>
2、 標籤體:jsp文件
程序清單4-3給出一個使用鋼材定義的heading標籤的文檔。由於bgColor屬性定義爲必需的( <required>true</required> ),因此該標籤的所有使用都包含它。
程序清單4-3 headingExample.jsp
<?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>msajsp-tags</short-name>
<description>
A simple tab for the examples
</description>
<tag>
<name>example</name>
<tag-class>moreservlets.tags.ExampleTag</tag-class>
<body-content>empty</body-content>
<description> Output some strings </description>
</tag>
<tag>
<name>simplePrime</name>
<tag-class>moreservlets.tags.SimplePrimeTag</tag-class>
<body-content>empty</body-content>
<description> Generates primes </description>
</tag>
<tag>
<name>prime</name>
<tag-class>moreservlets.tags.PrimeTag</tag-class>
<body-content>empty</body-content>
<description> Generates some primes </description>
<attribute>
<name>length</name>
<required>false</required>
</attribute>
</tag>
<tag>
<name>heading</name>
<tag-class>moreservlets.tags.HeadingTag</tag-class>
<body-content>JSP</body-content>
<description> Outputs a 1-cell table used as a heading </description>
<attribute>
<name>bgColor</name>
<required>true</required>
</attribute>
<attribute>
<name>color</name>
<required>false</required>
</attribute>
<attribute>
<name>align</name>
<required>false</required>
</attribute>
<attribute>
<name>fontSize</name>
<required>false</required>
</attribute>
<attribute>
<name>fontList</name>
<required>false</required>
</attribute>
<attribute>
<name>border</name>
<required>false</required>
</attribute>
<attribute>
<name>width</name>
<required>false</required>
</attribute>
</tag>
</taglib>