>“山高人为峰”,这句话也可以用在技术的研究上。
在jsp规范里,标签具有比javabean更丰富的运行时协议。它可以非常机密的和jsp的表示逻辑联系在一起,同时,又具有javabean相同业务处理能力。所以,标签的学习成为迫切的需要,但为了满足实际项目的开发,自定义标签的学习是不容错过的。
通过实现接口或者继承现有的类,我们就可以开发自定义的标签。
常用的接口有: JspTag
Tag SimpleTag<----SimpleTagSupport
IterationTag <----TagSupport
BodyTag<----BodyTagSupport
自定义标签的开发包括两个部分的开发:
(1)、开发标签的处理程序(java类)
(2)、标签描述文件(.tld文件)
自定义标签的种类有许多,可以根据实际项目的需要进行编写。但为了不重复的开发轮子,jsp标准推出JSTL(标准标签库)。
开始我们的旅行吧,只要是快乐的,谁都想干,做的好与不好就另当别论了。
(1)从Hello World开始吧,自定义一个类它实现Tag接口,Tag接口主要定义的是标签声明周期的方法,比如:doStartTag(),doEndTag()等,通过PageContext对象来访问Jsp页面的上下文:
package com.xmddl.tag.demo;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;
public class HelloTag implements Tag {
private PageContext context;
private Tag parent;
public void setPageContext(PageContext ctx) {
this.context=ctx;
}
public void setParent(Tag tag) {
this.parent=tag;
}
public Tag getParent() {
return this.parent;
}
public int doStartTag() throws JspException {
System.out.println("doStartTag");
return Tag.SKIP_BODY;
}
public int doEndTag() throws JspException {
System.out.println("doStartTag");
try {
this.context.getOut().write("Hello World");
} catch (Exception e) {
e.printStackTrace();
}
return Tag.EVAL_PAGE;
}
public void release() {
// TODO Auto-generated method stub
}
}
Tag接口中静态常量:
Tag.SKIP_BODY
Tag.EVAL_PAGE
Tag.EVAL_BODY_INCLUDE
Tag.SKIP_PAGE
(2)编写标签描述文件(.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>xmddl 1.1 hello library</description>
<display-name>xmddl hello</display-name>
<tlib-version>1.1</tlib-version>
<short-name>hello</short-name>
<uri>http://www.xmddl.com/tag</uri>
<tag>
<description>我的Hello World</description>
<name>hello</name>
<tag-class>com.xmddl.tag.demo.HelloTag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
(3)、在web.xml中对标签的引用:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "
http://java.sun.com/dtd/web-app_2_3.dtd
">
<web-app>
<taglib>
<taglib-uri>http://www.xmddl.com/tag</taglib-uri>
<taglib-location>/WEB-INF/mytag.tld</taglib-location>
</taglib>
</web-app>
(4)、编写测试页面:
<%@ page language="java" pageEncoding="UTF-8" import="java.util.*"%>
<%@ taglib uri="http://www.xmddl.com/tag" prefix="lu"%>
<html>
<head>
<title>MyJsp.jsp</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<h1>This is a test tag page for person.</h1> <br>
<lu:hello/>
</body>
</html>
下面看看标签中的属性是如何使用的,我们直接继承TagSupport类来编写:
package com.xmddl.tag.demo;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;
public class HelloSupport extends TagSupport {
private String value;
private static final long serialVersionUID = 1L;
public HelloSupport(){
super();
}
public int doStartTag() throws JspException {
System.out.println("doStartTag");
return TagSupport.EVAL_BODY_INCLUDE;
}
public int doEndTag() throws JspException {
System.out.println("doEndTag");
try {
this.pageContext.getOut().write("标签的属性值:"+this.value);
} catch (Exception e) {
e.printStackTrace();
}
return Tag.EVAL_PAGE;
}
public int doAfterBody() throws JspException{
System.out.println("doAfterBody");
try {
this.pageContext.getOut().write("<br>以上为标签的内容<br>");
} catch (Exception e) {
e.printStackTrace();
}
return TagSupport.EVAL_BODY_INCLUDE;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
======================
...
<tag>
<name>hellosupport</name>
<tag-class>com.xmddl.tag.demo.HelloSupport</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
...
=======================
<%@ page language="java" pageEncoding="gb2312" import="java.util.*"%>
<%@ taglib prefix="lu" uri="
http://www.xmddl.com/tag
" %>
<html>
<head>
</head>
<body>
<h1>This is a test tag page for person.</h1> <br>
<lu:hellosupport value="red">xmddl</lu:hellosupport>
</body>
</html>
利用TagSupport带来很大的方便,只要重写其中的部分方法即可实现一个标签处理程序,以上开发的标签程序和JSTL1.1标签库一起使用时出现一些疑问,原来标签的uri="
http://java.sun.com/jsp/jstl/core
",但是,在如下的测试中会发现c:out中的值输不出来,所以,只能改为uri="
http://java.sun.com/jstl/core
",
<%@ page language="java" pageEncoding="gb2312" import="java.util.*"%>
<%@ taglib prefix="lu" uri="
http://www.xmddl.com/tag
" %>
<%@ taglib prefix="c" uri="
http://java.sun.com/jstl/core
" %>
<html>
<head>
</head>
<body>
<h1>This is a test tag page for person.</h1> <br>
<%request.setAttribute("name","xmddl"); %>
<%session.setAttribute("names","xmddls"); %>
<lu:hellosupport value="red">xmddl</lu:hellosupport><hr>
<c:forEach begin="1" end="5" step="1">
name=<c:out value="${name}"></c:out>
names=<c:out value="${names}"></c:out><br>
</c:forEach>
</body>
</html>
输出如下:
This is a test tag page for person.
xmddl
以上为标签的内容
标签的属性值:red
name=xmddl names=xmddls
name=xmddl names=xmddls
name=xmddl names=xmddls
name=xmddl names=xmddls
name=xmddl names=xmddls
接着看看迭代标签的开发,首先,我们关注的是接口IterationTag,还有它的实现类TagSupport,接口BodyTag对IterationTag的扩展,还有BodyTag的实现类BodyTagSupport。
package com.xmddl.tag.demo;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class MyIteratorSupport extends BodyTagSupport {
private static final long serialVersionUID = 1L;
public String count;
public String strColor;
private int cou;
public void setCount(String count) {
this.count = count;
cou=Integer.parseInt(count);
}
public int doAfterBody() throws JspException {
try {
this.pageContext.getOut().write("<br>");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(cou>1){
cou--;
return MyIteratorTag.EVAL_BODY_AGAIN;
}else{
return MyIteratorTag.SKIP_BODY;
}
}
public int doStartTag() throws JspException {
try {
this.pageContext.getOut().write("<font color='"+this.strColor+"'>");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return MyIteratorTag.EVAL_BODY_INCLUDE;
}
public int doEndTag() throws JspException {
try {
this.pageContext.getOut().write("</font>");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return MyIteratorTag.EVAL_PAGE;
}
public int getCou() {
return cou;
}
public void setCou(int cou) {
this.cou = cou;
}
public String getCount() {
return count;
}
public String getStrColor() {
return strColor;
}
public void setStrColor(String strColor) {
this.strColor = strColor;
}
}
==============
。。。
<tag>
<description>对颜色的设置</description>
<name>myColor</name>
<tag-class>com.xmddl.tag.demo.MyIteratorSupport</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>count</name>
<required>true</required>
</attribute>
<attribute>
<name>strColor</name>
<required>false</required>
</attribute>
</tag>
。。。
==============
<%@ page language="java" pageEncoding="gb2312" import="java.util.*"%>
<%@ taglib prefix="lu" uri="http://www.xmddl.com/tag" %>
<html>
<head>
</head>
<body>
<h1>This is a test tag page for person.</h1> <br>
<lu:myColor count="3" strColor="blue">xmddl</lu:myColor>
</body>
</html>
输出如下
This is a test tag page for person.
xmddl
xmddl
xmddl