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