详细信息: 以前总在国外的站上看见TagLib,不知道是什么意思,今天总算知道了,它是一种定制的jsp的元素,像 <jsp:xxxxx> 就是一个tag,我试着写了一个HtmlEncode的Tag,还不错,用起来很方便,大家可以先看一下,不久我会写一篇tag教程。 源码如下 //HtmlEncode.java 下载 package lovejsp.tags; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.io.*; import lovejsp.*; /** 这个Tag可以使它的Body中的Html元素按原样显示,即可以实现 * HtmlEncode功能。 * http://lovejsp.com/. */ public class HtmlEncode extends BodyTagSupport { public int doAfterBody() { BodyContent body = getBodyContent(); String filteredBody = LovejspTools.encode(body.getString()); try { JspWriter out = body.getEnclosingWriter(); out.print(filteredBody); } catch(IOException ioe) { System.out.println("在HtmlEncode Tag中出现如下错误: " + ioe); } return(SKIP_BODY); } } 定义TagLib的lovejsp-taglib.tld文件,是XML格式的 下载 <?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd"> <!-- a tag library descriptor --> <taglib> <!-- after this the default space is "http://java.sun.com/j2ee/dtds/jsptaglibrary_1_2.dtd" --> <tlibversion>1.0</tlibversion> <jspversion>1.1</jspversion> <shortname>lovejsp</shortname> <uri></uri> <info> A tag library from Lovejsp.site http://lovejsp.com/. </info> <tag> <name>HtmlEncode</name> <tagclass>lovejsp.tags.HtmlEncode</tagclass> <info>HtmlEcode the TagBody</info> <!-- TOMCAT 3.1 DOES NOT SUPPORT BODYCONTENT <bodycontent>JSP</bodycontent> --> </tag> </taglib> 使用Tag的encode.jsp文件 下载 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>Example for lovejsp.tags.HtmlEncode Tag From LoveJSP.site(lovejsp.com)</TITLE> </HEAD> <BODY> <H1>ok,Let"s Go!!</H1> (Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 4.0)) renders the HTML 4.0 logical character styles: <P> <TABLE BORDER=1 ALIGN="CENTER"> <TR CLASS="COLORED"><TH>Example<TH>Result <TR> <TD><PRE> <EM>Some emphasized text.</EM><BR> <STRONG>Some strongly emphasized text.</STRONG><BR> <CODE>Some code.</CODE><BR> <SAMP>Some sample text.</SAMP><BR> <KBD>Some keyboard text.</KBD><BR> <DFN>A term being defined.</DFN><BR> <VAR>A variable.</VAR><BR> <CITE>A citation or reference.</CITE> </PRE> <TD> <EM>Some emphasized text.</EM><BR> <STRONG>Some strongly emphasized text.</STRONG><BR> <CODE>Some code.</CODE><BR> <SAMP>Some sample text.</SAMP><BR> <KBD>Some keyboard text.</KBD><BR> <DFN>A term being defined.</DFN><BR> <VAR>A variable.</VAR><BR> <CITE>A citation or reference.</CITE> </TABLE> </BODY> </HTML> |