jsp中最简单的JSTL制作

第一步:标签控制类
package tags;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;

public class HelloWorldTag extends TagSupport {

/**
*
*/

private static final long serialVersionUID = 1L;

public int doStartTag() throws JspException {
try {
JspWriter out = pageContext.getOut();
out.println(
"hello world!");
} catch (IOException ioExc) {
throw new JspException(ioExc.toString());
}
return SKIP_BODY;
}

public int doEndTag() {
return EVAL_PAGE;
}

}
第二步:生成标记库描述文件(hello.tld)
<?xml version=
"1.0"?>

<!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>hello</shortname>
<uri></uri>
<info>
An example Hello World tag
</info>

<tag>
<name>hello</name>
<tagclass>tags.HelloWorldTag</tagclass>
</tag>
</taglib>
第三步:标签使用(TestHello.jsp)
<%@ page language=
"java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib uri=
"/WEB-INF/tld/hello.tld" prefix = "mytag" %>
<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>My JSP 'TestHello.jsp' starting page</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">
<!--
<link rel=
"stylesheet" type="text/css" href="styles.css">
-->

  </head>
  
  <body>
    <mytag:hello/>
  </body>
</html>

此代码一在tomcat 5.0.28下测试通过.


lunzi   2006-11-11 13:21:10 评论:0   阅读:773   引用:0

发表评论>>

署名发表(评论可管理,不必输入下面的姓名)

姓名:

主题:

内容: 最少15个,最长1000个字符

认证码: (如不清楚,请刷新)


Copyright@2004-2010 powered by YuLog