本文主要是关于jap中“自定义标签”,主要包含:
1.使用jsp编写simple tag handler
2.使用java代码实现simple tag handler
1.使用jsp编写simple tag handler
1.HelloWorld.tag
编写HelloWorld.tag,如下,注意需要将HelloWorld.tag文件放置在/WEB-INF/tags/目录中。
Hello World!
HelloWorld.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="my" tagdir="/WEB-INF/tags" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'HelloWorld.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"> </head> <body> <my:HelloWorld></my:HelloWorld> </body> </html>
2.标签属性colorTable.tag
colorTable.tag
<!-- 设置编码的方式 --> <%@ tag pageEncoding="gb2312" %> <!-- 设置标签属性 --> <%@ attribute name="head" %> <%@ attribute name="headColor" required="true" %> <%@ attribute name="dataColor" required="true" %> <table bgcolor="${headColor}"> <tr> <th>${head}</th> </tr> <tr bgcolor="${dataColor}"> <td> <jsp:doBody /> </td> </tr> </table>
如何使用colorTable.tag?
<%@ taglib prefix="my" tagdir="/WEB-INF/tags" %> ...... <body> <my:colorTable head="header" headColor="#FF0000" dataColor="#FFC0C0">新闻<br/>音乐<br/>体育</my:colorTable> </body>
3.返回标签的值
photoRow.tag
<%@ tag pageEncoding="gb2312" %> <!-- 传入属性的值 --> <%@ attribute name="dir" required="true" %> <%@ attribute name="fileName" required="true" %> <!-- 返回值 --> <%@ variable name-given="fileName" %> <!-- tag基本内容 --> <c:set var="fileName" value="someValue"></c:set>
如何使用photoRow.tag?
<c:set var="fileName" value="someValue" />
2..使用java代码实现simple tag handler
1.编写HelloWorldTag.java
/** * */ package tag; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.SimpleTagSupport; /** * @author jefferyxu * */ public class HelloWorldTag extends SimpleTagSupport { /* (non-Javadoc) * @see javax.servlet.jsp.tagext.SimpleTagSupport#doTag() */ @Override public void doTag() throws JspException, IOException { // TODO Auto-generated method stub this.getJspContext().getOut().write("Hello World from the HeloWorldTag.java"); } }
在tld文件中声明,在myeclipse中使用下面的方法。

完成之后代码如下:
<tag> <name>helloWorld</name> <tag-class>tag.HelloWorldTag</tag-class> <body-content>empty</body-content> </tag>
在页面中这么使用代码版的tag:
<%@ taglib prefix="my" uri="/WEB-INF/tags/callfunction.tld" %> ...... <my:helloWorld/>
2.下面省略在tld文件中声明过程。只是单独的列出所有使用的java文件。具有属性的标签:
/** * */ package tag; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.SimpleTagSupport; /** * @author jefferyxu * */ public class PhotoInfoTag extends SimpleTagSupport { private String fileName; /** * @param fileName the fileName to set */ public void setFileName(String fileName) { this.fileName = fileName; } /* (non-Javadoc) * @see javax.servlet.jsp.tagext.SimpleTagSupport#doTag() */ @Override public void doTag() throws JspException, IOException { // TODO Auto-generated method stub // 在这里使用fileName,这个参数是通过jsp页面传递进来的 this.getJspContext().getOut().write(this.fileName); super.doTag(); } }
如何使用?
<body> <my:photoInfo fileName="pass the fileName"></my:photoInfo> </body>

3.具有body标签的tag
MenuTag.java
/** * */ package tag; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.SimpleTagSupport; /** * @author xuqiang * */ public class MenuTag extends SimpleTagSupport { /* (non-Javadoc) * @see javax.servlet.jsp.tagext.SimpleTagSupport#doTag() */ @Override public void doTag() throws JspException, IOException { // TODO Auto-generated method stub /** * this.getJspBody()得到JspFragement对象,调用其中的invoke * 来处理其中的body。 */ JspWriter writer = this.getJspContext().getOut(); this.getJspBody().invoke(writer); String tmp = writer.toString(); writer.write(tmp); super.doTag(); } }
如何使用?
<%@ taglib prefix="my" uri="/WEB-INF/tags/callfunction.tld" %> ..... <body> <my:menu>1.menu1 2.menu2</my:menu> </body>
未解决的问题:
1.JspFragement的invoke方法?
2.myeclipse中对于文件模板的管理?
本文详细介绍JSP自定义标签的实现方式,包括使用JSP编写SimpleTagHandler及使用Java代码实现,涵盖HelloWorld标签、带属性的标签及带有主体内容的标签等案例。
2130

被折叠的 条评论
为什么被折叠?



