標(biāo)簽庫(JSP)
實(shí)例三:有標(biāo)簽主體的標(biāo)簽庫... 10
JSP是一種功能強(qiáng)大的WEB應(yīng)用服務(wù)器端編程技術(shù),與傳統(tǒng)CGI編程語言響應(yīng)請求再輸出靜態(tài)HTML不同的是,JSP從靜態(tài)HTML開始並把JAVA代碼散佈在HTML中。當(dāng)編寫小型的應(yīng)用程序時,HTML標(biāo)記與JAVA代碼數(shù)量都不大,程序的可讀性和維護(hù)不是問題。但是建立中大型應(yīng)用程序時,HTML標(biāo)記與JAVA代碼大量的交替夾雜一起,使程序變得不可維護(hù)。程序員不得不在HTML中尋找JAVA代碼,而界面設(shè)計(jì)師要修改網(wǎng)頁界面變得不可能。這樣效率將非常低下。
標(biāo)簽庫的運(yùn)用將以上的問題很好地得到解決,它更清晰地分離了業(yè)務(wù)邏輯和表示細(xì)節(jié)。Servlet和JavaBean處理業(yè)務(wù)邏輯,JSP頁面作為一個顯示組件。標(biāo)簽庫就成了業(yè)處邏輯處理到JSP頁面的橋梁。標(biāo)簽庫的使用程序員更專注於編寫程序代碼,而界面設(shè)計(jì)師專員於界面布局及美工。
● 標(biāo)記定義
● taglib指令
● 標(biāo)記庫描述文件(*.tld)
● 標(biāo)記處理程序
標(biāo)簽庫的定義包括二種可能的格式:
● <prefix:tagName [attr=”value”] *>tagbody</prefix:tagName>
● <prefix:tagName [attr=”value”] *></prefix:tagName>或<prefix:tagName [attr=”value”] */>
perfix表示標(biāo)簽前綴,tagName標(biāo)簽名,attr=”value”屬性及屬性值,tagbody標(biāo)簽主體。
前一格式包含主體,後一種不包含。
<%@ taglib uri=”URI TO TAG” prefix=”tag prefis”%>
taglib指令通知JSP容器頁面使用了一個標(biāo)簽庫。該指今在JSP頁面的頂部指定標(biāo)簽庫描述文件的uri,prefix定義標(biāo)簽的前綴。
如:
<%@ taglib uri="/taglib" prefix="test"%>
在頁面的其它位置即可這樣使用:
<test:testtaglib name="testString" type="String"/>
name、type是標(biāo)簽的二個屬性。
標(biāo)記描述文件(Tag Library Descriptor,tld)是一個XML文檔。格式如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
版本信息 |
<jspversion>1.1</jspversion>
<shortname>Application Tag Library</shortname>
標(biāo)簽處理類 |
標(biāo)簽名 |
<tag>
<name>testtaglib</name>
<tagclass>com.shingwai.taglib.TestTag</tagclass>
<teiclass>com.shingwai.taglib.TestTEI</teiclass>
說明信息 |
<info></info>
<attribute>
標(biāo)簽屬性 |
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>type</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
</taglib>
該文件以.tld為後綴,與程序的web.xml存放在一起。
Web.xml聲明標(biāo)簽庫
<taglib>
<taglib-uri>/hello</taglib-uri>
<taglib-location>/WEB-INF/hello_tag.tld</taglib-location>
</taglib>
你可以在標(biāo)簽庫描述文件中使用<tagclass>元素來指定標(biāo)簽的處理程序。
標(biāo)簽處理程序是實(shí)現(xiàn)了JAVA接口的JavaBean組件,可以使用下面兩種接口來定義標(biāo)簽處理程序:
javax.servlet.jsp.tagext.Tag
處接口處理不含標(biāo)簽主體的簡單標(biāo)簽。
javax.servlet.jsp.tagext.BodyTag
可以訪問標(biāo)簽主體。
(注意:一個標(biāo)簽處理器僅定義一個標(biāo)簽;一個標(biāo)簽庫是幾個處理相同任務(wù)的標(biāo)簽處理器的集合)
Tag接口中的主要方法
doStratTag() 該方法處理標(biāo)簽的開始標(biāo)記
它有三個可能的返回值:
EVAL_BODY_INCLUED 處理標(biāo)記主體,但不允許標(biāo)簽處理程序操縱它。這樣的處理程序?qū)崿F(xiàn)Tag接口,並且不能實(shí)現(xiàn)BodyTag接口。
EVAL_BODY_TAG 處理標(biāo)記主體並且允許處理程序通過一個新的對象來操縱它。必須實(shí)現(xiàn)BodyTag接口。
SKIP_BODY 不處理標(biāo)記主體
doEndTag() 該方法處理標(biāo)簽的結(jié)束標(biāo)記
它有二個可能的返回值:
EVAL_PAGE 繼續(xù)計(jì)算JSP頁面的其余部分。
SKIP_PAGE 跳過JSP頁面的其余部分,以完成請求。
getParent() 獲得直接的父標(biāo)記
release()釋放標(biāo)簽處理程序的當(dāng)前實(shí)例
BodyTag接口中的主要方法
doInitBody()初始化主體處理,可能是為了獲得數(shù)據(jù)庫,如果doStartTag方法返回EVAL_BODY_AGAIN,那麼程序調(diào)用一次該方法,如果doStartTag方法返回SKIP_BODY,那麼程序不調(diào)用該方法。
doAfterBody()如果返回值是EVAL_BODY_AGAIN,程序重新調(diào)用該方法,直到返回SKIP_BODY。
在tld文件中你使用<teiclass>元素來指定該類,用來得到標(biāo)簽中的屬性和值。
JSP容器在TagExtraInfo類上調(diào)用getVariableInfo()方法,當(dāng)調(diào)用這個方法時,它將傳遞一個javax.servler.jsp.tagext.TagData對象,這個對象包含可識別的[屬性、值]對。這個屬性、值即是JSP頁面定義標(biāo)簽庫時的定義的標(biāo)簽屬性和值(attr=”value”)。
getVariableInfo()方法返回一個VariableInfo對象組,每個對象需要四個參數(shù)。TagData對象中屬性的值、變量的類型、boolean值、作用域。
三個作用域
1、NESTED 定義變量的操作在開始標(biāo)記和結(jié)束標(biāo)記使用該變量
2、AT_BEGIN D 操作在開始標(biāo)記到頁面結(jié)束使用該變量
3、AT_END 操作在結(jié)束標(biāo)記到頁面結(jié)束使用該變量
目的:用標(biāo)簽庫在JSP頁面顯示一個字符串“你好!”。
實(shí)現(xiàn)步驟:
1、Jbuilder新建工程。
2、建立包名:com.shingwai.taglib
3、編寫標(biāo)簽處理程序 HelloTag.java
代碼如下:
package com.shingwai.taglib;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class HelloTag extends TagSupport{
private String str="你好!";
doStartTag()方法 |
JspWriter writer=pageContext.getOut();
try{
writer.write(str);
}
catch(Exception e){
e.printStackTrace();
}
return TagSupport.SKIP_BODY;
}
}
4、編寫標(biāo)簽庫描術(shù)文件 hello_tag.tld 保存於程序的WEB-INF文件夾。
代碼如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>Application Tag Library</shortname>
操作標(biāo)識 |
<tag>
操作類,即標(biāo)簽處理程序 |
<tagclass>com.shingwai.taglib.HelloTag</tagclass>
</tag>
</taglib>
5、編寫應(yīng)用程序初始化文件web.xml
加入如下代碼:
<taglib>
<taglib-uri>/hello</taglib-uri>
<taglib-location>/WEB-INF/hello_tag.tld</taglib-location>
</taglib>
6、建立JSP頁面:hello.jsp
代碼如下:
<%@ page contentType="text/html; charset=Big5" %>
Taglib指令通知JSP容器使用了一個標(biāo)簽庫 |
<html>
<head>
<title>hello</title>
</head>
<body bgcolor="#ffffff">
定義標(biāo)簽庫 |
<test:hello/>
</h1>
</body>
</html>
7、發(fā)佈程序訪問顯示如下:
目的:當(dāng)屬性color的為“red”時輸出紅色的“你好!”,當(dāng)屬性color的值為“blue”時輸了為藍(lán)色的“你好!”。
1、編寫TEI類。HelloColorTEI.java
代碼如下:
package com.shingwai.taglib;
import javax.servlet.jsp.tagext.*;
public class HelloColorTEI extends TagExtraInfo{
public VariableInfo[] getVariableIfob(TagData data){
VariableInfo info1=new VariableInfo(data.getAttributeString("color"),"String",true,VariableInfo.NESTED);
VariableInfo[] info={info1};
return info;
}
}
2、編寫標(biāo)簽處理程序HelloColorTag.java
代碼如下:
package com.shingwai.taglib;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class HelloColorTag extends TagSupport{
private String c;
private String color=null;
public void setColor(String color){
this.color=color;
}
public int doEndTag()throws JspException{
JspWriter writer=pageContext.getOut();
c=this.color;
try{
writer.write("<font color=‘"+c+"‘>你好!</font>");
}
catch(Exception e){
e.printStackTrace();
}
return TagSupport.EVAL_PAGE;
}
}
3、在標(biāo)簽庫描術(shù)文件 hello_tag.tld 中加入XML標(biāo)識如下:
<tag>
<name>hellocolor</name>
<tagclass>com.shingwai.taglib.HelloColorTag</tagclass>
<teiclass>com.shingwai.taglib.HelloColorTEI</teiclass>
<attribute>
<name>color</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
4、hello.jsp代碼如下:
<%@ page contentType="text/html; charset=Big5" %>
<%@ taglib uri="/hello" prefix="test"%>
<html><head>
<title>hello</title>
</head>
<body bgcolor="#ffffff">
<h1>
<test:hellocolor color="red"/><br>
<test:hellocolor color="blue"/>
</h1>
</body>
</html>
5、發(fā)佈訪問文件hello.jsp
目的:把標(biāo)簽開始標(biāo)記到結(jié)束標(biāo)記內(nèi)(即標(biāo)簽主體的內(nèi)容)的文字顏色設(shè)為紅色輸出。
1、標(biāo)簽處理程序BodyTag.java
代碼如下:
package com.shingwai.taglib;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
public class BodyTag extends BodyTagSupport{
private String b="";
public int doStartTag()throws JspException{
return TagSupport.EVAL_BODY_AGAIN;//處理標(biāo)記主體並可操縱它
}
public int doAfterBody()throws JspException{
BodyContent body=getBodyContent();//得到主體內(nèi)容
String b=body.getString();
try{
JspWriter out=body.getEnclosingWriter();//返回BodyContent的JspWriter方法
out.print("<font color=‘red‘>"+b+"</font>");//寫出數(shù)據(jù)
}
catch(Exception e){
e.printStackTrace();
}
return TagSupport.SKIP_BODY;
}
public int doEndTag()throws JspException{
return TagSupport.EVAL_PAGE;
}
public void release(){
}
}
2、編寫描述文檔 body_tag.tld
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>Application Tag Library</shortname>
<info></info>
<tag>
<name>boby</name>
<tagclass>com.shingwai.taglib.BodyTag</tagclass>
<bodycontent>jsp</bodycontent>
<info></info>
</tag>
</taglib>
3、在web.xml 中聲明標(biāo)簽庫,加入如下代碼。
<taglib>
<taglib-uri>/body</taglib-uri>
<taglib-location>/WEB-INF/body_tag.tld</taglib-location>
</taglib>
4、Jsp文件 bodytag.jsp
<%@ page contentType="text/html; charset=Big5" %>
<%@ taglib uri="/body" prefix="body"%>
<html><head>
<title>bodytag</title>
</head>
<body bgcolor="#ffffff">
<h1>
<body:boby>
電腦部同仁好!
</body:boby>
</h1>
</body>
</html>
5、發(fā)布訪問bodytag.jsp,輸出紅色文字。
說明:可以對多個JSP標(biāo)記進(jìn)行嵌套引用,這樣子標(biāo)記就可以訪問和存儲父標(biāo)記的數(shù)據(jù)和方法。子標(biāo)記訪問父標(biāo)記需要使用BodyTagSupport類中的 findAccetorWithClass方法。注意它只能查找臨近的父標(biāo)記。
目的:父標(biāo)簽裡定義的字符串變量在子標(biāo)簽中輸出。
父標(biāo)簽 |
<p:parent>
子標(biāo)簽 |
</p:parent>
1、編寫父標(biāo)簽處理程序ParentTag.java
package com.shingwai.taglib;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
public class ParentTag extends BodyTagSupport{
private String name="";
private String email="";
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setEmail(String email){
this.email=email;
}
public String getEmail(){
return email;
}
public int doStartTag()throws JspException{
this.setName("李小明");
this.setEmail("lixiaomeng@163.com");
return TagSupport.EVAL_BODY_INCLUDE;
}
public int doEndTag()throws JspException{
return TagSupport.EVAL_PAGE;
}
}
2、編寫子標(biāo)簽處理程序SonTag.java
package com.shingwai.taglib;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class SonTag extends TagSupport{
public int doStartTag()throws JspException{//處理開始標(biāo)記
ParentTag parent=(ParentTag)findAncestorWithClass(this,com.shingwai.taglib.ParentTag.class);//取父標(biāo)簽的對象,等同ParentTag parent=(ParentTag)getParent();
JspWriter writer = pageContext.getOut();
try {
writer.write(parent.getName());//訪問父標(biāo)簽的方法,輸出值
}
catch (Exception e) {
e.printStackTrace();
}
return TagSupport.EVAL_BODY_AGAIN;
}
public int doEndTag()throws JspException{//處理結(jié)束標(biāo)記
ParentTag parent=(ParentTag)findAncestorWithClass(this,com.shingwai.taglib.ParentTag.class);
JspWriter writer = pageContext.getOut();
try {
writer.write(parent.getEmail());
}
catch (Exception e) {
e.printStackTrace();
}
return TagSupport.EVAL_PAGE;
}
}
3、描述符文件 parent_tag.tld
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>Application Tag Library</shortname>
<info></info>
<tag>
<name>parent</name>
<tagclass>com.shingwai.taglib.ParentTag</tagclass>
</tag>
<tag>
<name>son</name>
<tagclass>com.shingwai.taglib.SonTag</tagclass>
</tag>
</taglib>
4、明標(biāo)簽庫 web.xml 增加如下代碼:
<taglib>
<taglib-uri>/parent</taglib-uri>
<taglib-location>/WEB-INF/parent_tag.tld</taglib-location>
</taglib>
5、JSP頁面編寫:parent.jsp
<%@ page contentType="text/html; charset=Big5" %>
<%@ taglib uri="/parent" prefix="p"%>
<html><head>
<title>parent</title>
</head>
<body bgcolor="#ffffff">
<h1>
<p:parent>
<p:son><br> </p:son>
</p:parent>
</h1>
</body>
</html>
6、發(fā)佈該問parent.jsp