使用自定义简单标签我们可以将相应的控制页面逻辑的代码封装在标签中,在使用时只需引用标签,便可达到与使用java代码同样的效果。
来看几个具体的案例:
一个tld标签库描述文件:
<?xml version="1.0"encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>A taglibrary exercising SimpleTag handlers.</description>
<tlib-version>1.0</tlib-version>
<short-name>c</short-name>
<uri>http://www.hbsi.examplec</uri>
<tag>
<description>OutputsHello, World</description>
<name>if</name>
<tag-class>www.hbsi.web.example.IFSimpleTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>flag</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<description>OutputsHello, World</description>
<name>choice</name>
<tag-class>www.hbsi.web.example.CElseSimpleTag</tag-class>
<body-content>scriptless</body-content>
</tag>
<tag>
<description>OutputsHello, World</description>
<name>If</name>
<tag-class>www.hbsi.web.example.CIFSimpleTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>test</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<description>OutputsHello, World</description>
<name>Else</name>
<tag-class>www.hbsi.web.example.CElseSimpleTag</tag-class>
<body-content>scriptless</body-content>
</tag>
</taglib>
相关的自定义标签处理器类及相应的jsp引用文件:
1、 开发<c:if>标签
publicclass IFSimpleTag extends SimpleTagSupport{
privatebooleanflag ;
publicvoid setFlag(boolean flag) {
this.flag = flag;
}
@Override
publicvoid doTag() throws JspException, IOException {
if(flag){
JspFragment jf= this.getJspBody();
jf.invoke(null);
}
}
}
Jsp引用文件:
<%@taglib uri="http://www.hbsi.examplec" prefix ="c" %>
<body>
<c:if flag="true">
当结果为真时我才会输出!<br/>
</c:if>
不管结果是什么我都会输出!<br/>
</body>
效果:
2、 开发<c:if><c:else>标签
if子类
publicclass CIFSimpleTag1 extends SimpleTagSupport {
privatebooleantest ;
publicvoid setTest(boolean test) {
this.test = test;
}
@Override
publicvoid doTag() throws JspException, IOException {
IFElseSimpleTag cie = (IFElseSimpleTag) this.getParent();
if(test && !cie.isFlag()){
this.getJspBody().invoke(null);;
cie.setFlag(true);
}
}
}
else子类
publicclass CElseSimpleTag1 extends SimpleTagSupport {
@Override
publicvoid doTag() throws JspException, IOException {
IFElseSimpleTag cie = (IFElseSimpleTag) this.getParent();
// if(!cie.isFlag()){
if(!cie.isFlag()){
this.getJspBody().invoke(null);
cie.setFlag(true);
}
}
}
父类:
publicclass IFElseSimpleTag extends SimpleTagSupport {
privatebooleanflag;
publicboolean isFlag() {
returnflag;
}
publicvoid setFlag(boolean flag) {
this.flag = flag;
}
@Override
publicvoid doTag() throws JspException, IOException {
this.getJspBody().invoke(null);
}
}
Jsp引用文件
<%@ taglib uri="http://www.hbsi.c" prefix="c"%>
<body>
<c:choice>
<c:if test ="<%=1==2 %>">
当结果为真时我才会输出!<br/>
</c:if>
<c:else>
当结果为假时我才会输出!<br/>
</c:else>
</c:choice>
</body>
效果:
3、开发迭代标签
publicclass ForEachSimpleTag2 extends SimpleTagSupport {
private Collection collection;
private String var ;
private Object items;
publicvoid setItems(Objectitems) {
this.items = items;
if(itemsinstanceof Collection){
collection = (Collection) items;
}
if(itemsinstanceof Map){
Map map = (Map) items;
collection = map.entrySet();
}
if(items.getClass().isArray()){
collection = new ArrayList();
for(int i=0;i<Array.getLength(items);i++){
collection.add(Array.get(items, i)) ;
}
}
}
publicvoid setVar(String var) {
this.var = var;
}
@Override
publicvoid doTag() throws JspException, IOException {
Iterator it = collection.iterator();
while(it.hasNext()){
Object object = it.next();
this.getJspContext().setAttribute(var, object);
JspFragment jf= this.getJspBody();
jf.invoke(null);
}
}
}
Jsp引用文件
<%@ taglib uri="http://www.hbsi.c"prefix ="c" %>
<body>
<%
List list = new ArrayList();
list.add("aa");
list.add("bb");
list.add("cc");
request.setAttribute("list",list);
%>
<c:ForEach items="${list}" var="l">
${l}
</c:ForEach>
<hr/>
<%
Map map = new LinkedHashMap();
map.put("1","aaa");
map.put("2","aaa");
map.put("3","aaa");
request.setAttribute("map",map);
%>
<c:ForEach items="${map}" var="me">
${me.key}----------${me.value}
</c:ForEach>
<hr/>
<%
String str [] = {"aaaaa","bbbbbb","ccccccc"};
request.setAttribute("str",str);
%>
<c:ForEach items="${str}" var="str">
${str}
</c:ForEach>
<hr/>
<%
int i [] = {1,3,4};
request.setAttribute("in",i);
%>
<c:ForEach items="${in}" var="in">
${in}
</c:ForEach>
<hr/>
</body>
效果如图:
4、开发html转义标签
publicclass HTMLFilter extends SimpleTagSupport {
@Override
publicvoid doTag() throws JspException,IOException {
JspFragment jf = this.getJspBody();
StringWriter sw = newStringWriter();
jf.invoke(sw);
String content = sw.toString();
String contents = filter(content);
this.getJspContext().getOut().write(contents);
}
public Stringfilter(String message) {
if (message == null)
return (null);
char content[] = newchar[message.length()];
message.getChars(0, message.length(), content, 0);
StringBuffer result = new StringBuffer(content.length + 50);
for (int i =0; i < content.length; i++) {
switch (content[i]) {
case'<':
result.append("<");
break;
case'>':
result.append(">");
break;
case'&':
result.append("&");
break;
case'"':
result.append(""");
break;
default:
result.append(content[i]);
}
}
return (result.toString());
}
}
例3的效果图:
Jsp引用文件
<%@ taglib uri="http://www.hbsi.c"prefix="c"%>
<body>
<c:filter>
将文件中的HTML原作用除去!
<a href = "#">Hello!</a>
</c:filter>
</body>
效果如图:
好了赶快自己试试吧!