包含标签体
到此为止,你看到的所有定制的标签都忽略了标签体,因此以以下形式的独立标签使用:
<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>
部署服务器文件,在浏览器中输入:http://localhost:8080/simpletag/HeadingExample.jsp。