包含标签体
到此为止,你看到的所有定制的标签都忽略了标签体,因此以以下形式的独立标签使用:
<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.
*/
public
class
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
;
public
void
setBgColor(String bgColor)
{
this
.
bgColor
=bgColor;
}
public
void
setColor(String color)
{
this
.
color
=color;
}
public
void
setAlign(String align)
{
this
.
align
=align;
}
public
void
setFontSize(String fontSize)
{
this
.
fontSize
=fontSize;
}
public
void
setFontList(String fontList)
{
this
.
fontList
=fontList;
}
public
void
setBorder(String border)
{
this
.
border
=border;
}
public
void
setWidth(String width)
{
this
.
width
=width;
}
public
int
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
);
}
public
int
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>