当前页面: 开发资料首页 → JSP 专题 → 实现HtmlEncode功能的Tag
摘要: 实现HtmlEncode功能的Tag
详细信息:
以前总在国外的站上看见TagLib,不知道是什么意思,今天总算知道了,它是一种定制的jsp的元素,像
源码如下
//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" ?>
taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
"http://java.sun.com/j2ee/dtds/jsptaglibrary_1_2.dtd"
A tag library from Lovejsp.site
http://lovejsp.com/.
<bodycontent>JSP</body>
使用Tag的encode.jsp文件 下载
HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<head>
</head>
<body>
ok,Let"s Go!!
(Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 4.0))
renders the HTML 4.0 logical character styles:
<table BORDER=1 ALIGN="CENTER">
<tr CLASS="COLORED"><th>Example<th>Result
<tr>
<td>
Some emphasized text.
Some strongly emphasized text.
Some code.
Some sample text.
Some keyboard text.
A term being defined.
A variable.
A citation or reference.
<td>
Some emphasized text.
Some strongly emphasized text.
Some code.
Some sample text.
Some keyboard text.
A term being defined.
A variable.
A citation or reference.
</table>
</body>
↑返回目录
前一篇: 实现HTMLEncode功能的bean
后一篇: ASP与JSP的比较