tld定义格式
<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"> <taglib> <!--定义标签版本库--> <tlib-version>1.0</tlib-version> <!--定义jsp版本库--> <jsp-version>1.2</jsp-version> <short-name>Cms Tag</short-name> <description><!--标签描述---> A simple appbase tag library </description> <tag> <name>page</name><!--tag的名字--> <tag-class>com.cms.common.tag.PageTag</tag-class><!--tag对应的java类的名字--> <body-content>empty</body-content> <!--关于body-content 有三个值可选;empty:标签体必须为空;jsp:标签由其他jsp元素组成比如标签中含有<%=attributeName%>的jsp元素,那么此时body-content的值就是实际attributeName传入的值;tagdependent:有标签解释不带jsp转换(这个深入的含义不太了解)--> <attribute><!---这里表示的是这个tag的一个参数--> <name>cmsform</name><!--这个参数的名字--> <required>true</required><!--是否是必填选项--> <rtexprvalue>true</rtexprvalue><!--这个参数的值是否可以写入,换句话说 就是这个参数是否可以动态赋值--> </attribute> </tag> </taglib>
定义Tag对应类
此类必须重写doStartTag以及doEndTag方法
/** * */ package com.cms.common.tag; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.TagSupport; /** * @author louisliao * */ public class DemoViewTag extends TagSupport { /** * */ private static final long serialVersionUID = 1L; private String cmsform = ""; public String getCmsForm() { return cmsform ; } public void setCmsForm(String cmsform ) { this.cmsform = cmsform ; } /** * */ public DemoViewTag() { // TODO Auto-generated constructor stub } public int doStartTag() { return super.SKIP_BODY; } public int doEndTag() throws JspException { JspWriter writer=pageContext.getOut(); try { writer.print("这是我的标签示例<br/>"+"cmsform :"+this.cmsform); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return super.EVAL_PAGE; } }
在web.xml中加入taglib对应文件配置
如:
<taglib> <taglib-uri>http://mytag.sf.net</taglib-uri> <taglib-location>/WEB-INF/mytag.tld</taglib-location> </taglib>
这样就表示了http://mytag.sf.net对应WEB-INF/mytag.tld文件
在Jsp页面中引用
如:
<%@ taglib uri="http://mytag.sf.net" prefix="myTag"%>
在Jsp页面中使用
<myTag:exname1><myTag:exname1>
示例:
定义myTag.tld标签文件
<?xml version="1.0" encoding="utf-8"?> <!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>MyJSPTag Library</shortname> <uri>http://mytag.sf.net</uri> <info>我的示例标签库</info> <tag> <name>demo.Viewport</name> <tagclass>com.myapp.web.tag.DemoViewTag</tagclass> <bodycontent>JSP</bodycontent> <info>demo.Viewport标签</info> <attribute> <name>northTitle</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>westTitle</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
定义标签类
/** * */ package com.myapp.web.tag; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.TagSupport; /** * @author louisliao * */ public class DemoViewTag extends TagSupport { /** * */ private static final long serialVersionUID = 1L; private String northTitle = ""; private String westTitle = ""; public String getNorthTitle() { return northTitle; } public void setNorthTitle(String northTitle) { this.northTitle = northTitle; } public String getWestTitle() { return westTitle; } public void setWestTitle(String westTitle) { this.westTitle = westTitle; } /** * */ public DemoViewTag() { // TODO Auto-generated constructor stub } public int doStartTag() { return super.SKIP_BODY; } public int doEndTag() throws JspException { JspWriter writer=pageContext.getOut(); try { writer.print("这是我的标签示例<br/>westTitle:"+this.westTitle+"<br/>northTitle:"+this.northTitle); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return super.EVAL_PAGE; } }
web.xml添加配置
<taglib> <taglib-uri>http://mytag.sf.net</taglib-uri> <taglib-location>/WEB-INF/mytag.tld</taglib-location> </taglib>
测试页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib uri="http://mytag.sf.net" prefix="myTag"%> <% 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 'tagtldDemo.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> This is my JSP page. <br> <myTag:demo.Viewport northTitle="南" westTitle="西"></myTag:demo.Viewport> </body> </html>