JSP自定义标签TLD文件详解

想写个tld的分页标签,但是里边一些具体含义都忘了,- -! 于是百度了一把。弄清楚了常用一些标签含义,如下 解说
================================================================
先写一个cms.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>error</name><!--tag的名字-->
  <tag-class>com.cms.common.tag.ErrorTag</tag-class><!--其中对应的java类-->
  <body-content>empty</body-content>
<!--关于body-content 有三个值可选;empty:标签体必须为空;jsp:标签由其他jsp元素组成比如标签中含有<%=attributeName%>的jsp元素,那么此时body-content的值就是实际attributeName传入的值;tagdependent:有标签解释不带jsp转换(这个深入的含义不太了解)-->
  </tag>

  <tag>
<name>page</name><!--tag的名字-->
<tag-class>com.cms.common.tag.PageTag</tag-class>
<body-content>empty</body-content>
<attribute><!---这里表示的是这个tag的一个参数-->
            <name>cmsform</name><!--这个参数的名字-->
            <required>true</required><!--是否是必填选项-->
            <rtexprvalue>true</rtexprvalue><!--这个参数的值是否可以写入,换句话说 就是这个参数是否可以动态赋值-->
        </attribute>
  </tag>

  <tag>
<name>topmenu</name>
<tag-class>com.cms.common.tag.TopMenuTag</tag-class>
<body-content>empty</body-content>
<attribute>
            <name>value</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
  </tag>
 
</taglib>
====================================================================
在写java类必须是要继承TagSupport 而且要覆盖doEndTag()方法

如下
package com.cms.common.tag;

import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;

import com.cms.Constants;
import com.cms.common.form.CmsForm;

public class PageTag extends TagSupport{
private String cmsform="";

public int doStartTag()throws JspTagException{
return EVAL_BODY_INCLUDE;
}

public int doEndTag()throws JspTagException{

try
{
CmsForm form=(CmsForm)pageContext.getRequest().getAttribute(cmsform);

if(form!=null)
{
if(form.getPageCount()>0){
pageContext.getOut().write("<div align=\"right\">");
pageContext.getOut().write("共"+form.getRecordCount()+"条记录 ");
pageContext.getOut().write("第"+form.getPageNum()+"页 ");
pageContext.getOut().write("共"+form.getPageCount()+"页 ");
pageContext.getOut().write("每页显示"+Constants.CMS_PageSize+"条 ");
pageContext.getOut().write("<a href=\"javascript:void(0);\" onclick=\"document.forms[0].pageNum.value=1;document.forms[0].submit();return false;\" title=\"首页\" >首页</a> ");
int pageNum=form.getPageNum();
int pageCount=form.getPageCount();
int nextPage=pageNum+1>pageCount?pageCount:pageNum+1;
int prevPage=pageNum-1<1?1:pageNum-1;
pageContext.getOut().write("<a href=\"javascript:void(0);\" onclick=\"document.forms[0].pageNum.value="+prevPage+";document.forms[0].submit();return false;\" title=\"上一页\" >上一页</a> ");
pageContext.getOut().write("<a href=\"javascript:void(0);\" onclick=\"document.forms[0].pageNum.value="+nextPage+";document.forms[0].submit();return false;\" title=\"下一页\" >下一页</a> ");
pageContext.getOut().write("<a href=\"javascript:void(0);\" onclick=\"document.forms[0].pageNum.value="+(form.getPageCount())+";document.forms[0].submit();return false;\" title=\"尾页\" >尾页</a> ");
pageContext.getOut().write("<select id=\"pageNum\" name=\"pageNum\" onchange=\"document.forms[0].submit();\">");
for(int i=1;i<=form.getPageCount();i++){
pageContext.getOut().write("<option value=\""+i+"\" "+(i==form.getPageNum()?"selected":"")+">"+i+"/"+form.getPageCount()+"</option>");
}
pageContext.getOut().write("</seletct>");
//pageContext.getOut().write("<input type=\"text\" id=\"pageNum\" name=\"pageNum\" value=\""+form.getPageNum()+"\" size=\"3\" onkeypress=\"validatechar();\" /> ");
//pageContext.getOut().write("<input type=\"submit\" onclick=\"return validateOnSubmit();\" value=\"Go\" class=\"btn\" />");
                pageContext.getOut().write("<input type=\"hidden\" name=\"pageCount\" id=\"pageCount\" value=\""+form.getPageCount()+"\" />");
                pageContext.getOut().write("</div>");
}else{
pageContext.getOut().write("<input type=\"hidden\" id=\"pageNum\" name=\"pageNum\" value=\""+form.getPageNum()+"\" />");
}
}else{
pageContext.getOut().write("");
}

}
catch(Exception e)
{
System.out.println("com.mytag.ErrorTag 发生异常!");
throw new JspTagException("Fatal error:hello tag conld not write to JSP out");
}
return EVAL_PAGE;
}

public void setCmsform(String cmsform) {
this.cmsform = cmsform;
}


}
----------------------------------------------------------
package com.cms.common.tag;

import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;

public class TopMenuTag extends TagSupport{
private String value="1";
private String cssClass="navtab";

public int doStartTag()throws JspTagException{
return EVAL_BODY_INCLUDE;
}
private String getNow(String parm){
if(value.equals(parm)){
return "now";
}
return "";
}
public int doEndTag()throws JspTagException{
/*
try
{
return EVAL_PAGE;
}

public void setValue(String value) {
this.value = value;
}


}
====================================================================
在web.xml中的<jsp-config>需要添加
<jsp-config>
<taglib>
<taglib-uri>/WEB-INF/cmstag.tld</taglib-uri>
<taglib-location>/WEB-INF/cmstag.tld</taglib-location>
</taglib>

</jsp-config>
---------------------------------------------------
jsp中相关应用摘录部分
  <html:form action="/company/companymgmt.do" method="post" >
  <html:hidden property="actionType" value="Query"/>
  <html:hidden property="cus_num" />
  <html:hidden property="menuId"/>
 
  <input type="hidden" name="chakan_num" id="chakan_num" />
  <table class="maintab">
  <tr>
    <td>
<cms:topmenu value="1" /> 


...
...
<td align="right"><cms:page cmsform="companyInfoForm" /></td>

你可能感兴趣的:(jsp,cms,Web,servlet,百度)