日期:2014-05-16  浏览次数:20470 次

多使用jsp的自定义标签
  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">
 &