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下就可以了