JSP的自定义标签其实是挺好用的,比如在一个比较复杂的页面中,简单地插入几句
,就可以避免大量的冗余的代码,比如在一个新闻网站中,可以做一个播放幻灯的自定义标签。下面总结下,大概做自定义标签的套路如下:
public class SlidenewsTag extends SimpleTagSupport {

/** 标签体处理 */
    public void doTag() throws JspException, IOException{

。。。。。。。
//使用WebApplicationContextUtils工具类获取Spring IOC容器中的dao实例
    dao = (BaseDAOImpl)WebApplicationContextUtils.getRequiredWebApplicationContext(((PageContext)getJspContext()).getServletContext()).getBean("dao");   
   

             StringBuffer sb=new StringBuffer();
List list=dao.query(hql,1,number);
if(list==null||list.size()==0){
    //输出处理结果到页面上
    getJspContext().getOut().println("");
return;
}
Iterator it=list.iterator();
sb.append("    <script language=javascript>\n");
sb.append(" var focus_width"+slideno+"="+width+";     /*幻灯片新闻图片宽度*/\n");
sb.append(" var focus_height"+slideno+"="+height+";    /*幻灯片新闻图片高度*/\n");
sb.append(" var text_height"+slideno+"=20;    /*幻灯片新闻文字标题高度*/\n");
sb.append(" var swf_height"+slideno+" = focus_height"+slideno+"+text_height"+slideno+";\n");
sb.append(" var pics"+slideno+" = '';\n");
sb.append(" var links"+slideno+" = '';\n");
sb.append(" var texts"+slideno+" = '';\n");
sb.append(" function ati"+slideno+"(url, img, title)\n");
sb.append(" {\n");
sb.append(" if(pics"+slideno+" != '')\n");
sb.append(" {\n");
sb.append(" pics"+slideno+" = \"|\" + pics"+slideno+";\n");
sb.append(" links"+slideno+" = \"|\" + links"+slideno+";\n");
sb.append(" texts"+slideno+" = \"|\" + texts"+slideno+";\n");
sb.append(" }");
sb.append(" pics"+slideno+" = escape(img) + pics"+slideno+";\n");
sb.append(" links"+slideno+" = escape(url) + links"+slideno+";\n");
sb.append(" texts"+slideno+" = title + texts"+slideno+";\n");
sb.append(" }\n");
sb.append("    </script>\n");
sb.append("    <script language=javascript> \n");
while(it.hasNext()){
obj=(News)it.next();
if (obj.getTitle().length()>titlelen){
sb.append("      ati"+slideno+"('"+baseurl+obj.getHtmlPath()+"', '"+baseurl+"/"+obj.getPicture().trim()+"', '"+Tools.cutString(obj.getTitle(), titlelen*2)+"');\n");
}else{
sb.append("      ati"+slideno+"('"+baseurl+obj.getHtmlPath()+"', '"+baseurl+"/"+obj.getPicture().trim()+"', '"+obj.getTitle()+"');\n");
}
}
sb.append(" document.write('<embed src=\""+baseurl+"/js/pixviewer.swf\" wmode=\"opaque\" FlashVars=\"pics='+pics"+slideno+"+'&links='+links"+slideno+"+'&texts='+texts"+slideno+"+'&borderwidth='+focus_width"+slideno+"+'&borderheight='+focus_height"+slideno+"+'&textheight='+text_height"+slideno+"+'\" menu=\"false\" bgcolor=\"#DADADA\" quality=\"high\" width=\"'+ focus_width"+slideno+"+'\" height=\"'+ swf_height"+slideno+" +'\" allowScriptAccess=\"sameDomain\" type=\"application/x-shockwave-flash\"/>'); \n");
sb.append("</script>\n");
    //输出处理结果到页面上
    getJspContext().getOut().println(sb);


}

   可以看到,其实就是不断组装HTML而已,没什么复杂和特别的

2 建立xxx.tld文件
   <taglib xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
    version="2.0">
   <!-- 标签库的版本号 -->
  <tlib-version>1.0</tlib-version>
  <!-- 标签库的默认前缀 -->
  <short-name>eportal</short-name>
  <!-- 标签库的默认URI -->
  <uri>/eportal</uri>
<tag>
<description>幻灯片新闻列表标签</description>
    <name>slidenews</name>
    <tag-class>com.eportal.tld.SlidenewsTag</tag-class>
<!-- 标签体为空 -->   
    <body-content>empty</body-content>
    <attribute>
        <description>栏目编号,多个栏目编号中间用号码分隔,如:I_001,I_002</description>
        <name>section</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>新闻类型:0-最新新闻 1-头条新闻 2-热点新闻 3-精彩推荐 4-所有类型</description>
        <name>newstype</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>   
    <attribute>
        <description>显示新闻的条数</description>
        <name>number</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>幻灯片宽度</description>
        <name>width</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>幻灯片高度</description>
        <name>height</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>标题字数</description>
        <name>titlelen</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>基本URL</description>
        <name>baseurl</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>在本页中当前幻灯片的序号</description>
        <name>slideno</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>    
  </tag>

注意,其中的所有<attribute>,都要在JAVA里设置getter和setter

3 调用

<%@ taglib prefix="e" uri="/eportal"%>

  <!-- 使用自定义幻灯新闻标签 -->
<e:slidenews section="001" number="5" titlelen="30" width="440" height="260" baseurl="<%=basepath%>" slideno="1"/>

注意不TLD放在web-inf下就可以了