图片新闻的制作

作者在 2011-03-23 14:24:04 发布以下内容
package com.web.tag;

import java.io.Serializable;
import java.util.List;

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

import com.westarsoft.base.util.DateTime;
import com.westarsoft.base.vo.BackNewsVO;
import com.westarsoft.gsc.news.bean.BackNewsBean;

public class MyNewsImg extends BodyTagSupport implements Serializable {

    /**
     *
    
*/
    private static final long serialVersionUID = -7631407752509462650L;
    private String parenttype;
    private String childtype;
    private int focuswidth = 300;

    private int focusheight = 250;

    private int imgnum = 6;

    public int getImgnum() {
        return imgnum;
    }

    private int titleLength = 10;
    
    public void setImgnum(int imgnum) {
        this.imgnum = imgnum;
    }

    public int doStartTag() throws JspTagException {
        List<BackNewsVO> listnewspic = null;
        String img = "";
        String url = "";
        String title = "";
        BackNewsBean nbean = new BackNewsBean();
        try {

            BackNewsVO cxvo = new BackNewsVO();
            cxvo.setChild_type(this.getChildtype());
            cxvo.setType(this.getParenttype());
            cxvo.setDisplay("1");
            cxvo.setCurrentTime(DateTime.Instance().getCurrentTime(3));// 有效时间
            cxvo.setTitle_image_name("true");// 查询有图片的
            listnewspic = nbean.getNews(cxvo, 1, this.getImgnum());
            if (listnewspic != null && listnewspic.size() > 0) {
                for (BackNewsVO vo : listnewspic) {
                    String fileName = vo.getTitle_image_name();
                    if (fileName != null && !fileName.equals("")) {
                        url += "/web/show.jsp?id=" + vo.getId()
                        + "%26type=" + vo.getType() + "%26child_type="
                        + vo.getChild_type()+"|";
                        title += this.cutString(vo.getTitle(), this.getTitleLength()) + "|";
                        if (!fileName.substring(fileName.length() - 4,
                                fileName.length()).equalsIgnoreCase(".jpg")) {
                            img += "/web/images/errorimg.JPG|";
                        } else {
                            img += "/uploadfile/"+fileName + "|";
                        }
                    } else {
                        continue;
                    }
                }
                url = url.substring(0, url.length() - 1);
                title = title.substring(0, title.length() - 1);
                img = img.substring(0, img.length() - 1);
                img = img.replace("\\", "/");
            }
            StringBuffer str = new StringBuffer();
            str.append("<script type=\"text/javascript\">\n");
            str.append("var focus_width=" + this.getFocuswidth() + ";\n");
            str.append("var focus_height=" + this.getFocusheight() + ";\n");
            str.append("var text_height=20;\n");
            str.append("var swf_height = focus_height+text_height;\n");
            str.append("var t=0;\n");
            str.append("var pics=\"" + img + "\";\n");
            str.append("var links=\"" + url + "\";\n");
            str.append("var texts=\"" + title + "\";\n");
            str
                    .append("document.write('<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" width=\"'+ focus_width +'\" height=\"'+ swf_height +'\">');\n");
            str
                    .append("document.write('<param name=\"allowScriptAccess\" value=\"sameDomain\"><param name=\"movie\" value=\"/web/images/focus.swf\"><param name=\"quality\" value=\"high\"><param name=\"bgcolor\" value=\"#F0F0F0\">');\n");
            str
                    .append("document.write('<param name=\"menu\" value=\"false\"><param name=wmode value=\"opaque\">');\n");
            str
                    .append("document.write('<param name=\"FlashVars\" value=\"pics='+pics+'&links='+links+'&texts='+texts+'&borderwidth='+focus_width+'&borderheight='+focus_height+'&textheight='+text_height+'\">');\n");
            str
                    .append("document.write('<embed src=\"pixviewer.swf\" wmode=\"opaque\" FlashVars=\"pics='+pics+'&links='+links+'&texts='+texts+'&borderwidth='+focus_width+'&borderheight='+focus_height+'&textheight='+text_height+'\" menu=\"false\" bgcolor=\"#F0F0F0\" quality=\"high\" width=\"'+ focus_width +'\" height=\"'+ focus_height +'\" allowScriptAccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" />');\n");
            str.append("document.write('</object>');\n");
            str.append("</script>\n");
            pageContext.getOut().write(str.toString());
            // System.out.println(listnewspic.size());

        } catch (Exception e) {
            e.printStackTrace();
        }
        return EVAL_BODY_BUFFERED;
    }

    public int doAfterBody() throws JspTagException {
        try {
            // bodyContent.getEnclosingWriter().write(bodyContent.getString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return SKIP_BODY;
    }

    public int doEndTag() {
        try {
        } catch (Exception e) {
            e.printStackTrace();
        }
        return EVAL_PAGE;
    }

    public int getFocuswidth() {
        return focuswidth;
    }

    public void setFocuswidth(int focuswidth) {
        this.focuswidth = focuswidth;
    }

    public int getFocusheight() {
        return focusheight;
    }

    public void setFocusheight(int focusheight) {
        this.focusheight = focusheight;
    }

    public String getParenttype() {
        return parenttype;
    }

    public void setParenttype(String parenttype) {
        this.parenttype = parenttype;
    }

    public String getChildtype() {
        return childtype;
    }

    public void setChildtype(String childtype) {
        this.childtype = childtype;
    }
    
    private String cutString(String str, int num) throws Exception {
        try {
            if (str != null && str.length() > num) {
                str = str.substring(0, num) + "...";
            }
        } catch (Exception e) {
            throw e;
        }
        return delNullString(str);
    }

    private String delNullString(String str) {
        try {
            if (str == null) {
                str = "";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }

    public int getTitleLength() {
        return titleLength;
    }

    public void setTitleLength(int titleLength) {
        this.titleLength = titleLength;
    }
}
tld里面的内容
<tlibversion>1.0</tlibversion>
    <jspversion>1.1</jspversion>
    <shortname>my</shortname>

<tag>
        <name>newsImg</name>
        <tagclass>com.web.tag.MyNewsImg</tagclass>
        <bodycontent>empty</bodycontent>
        <info>pager tag provide the frame of the page divider</info>
        <attribute>
            <name>parenttype</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>childtype</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>focuswidth</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>focusheight</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>imgnum</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>titleLength</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
jsp页面的应用
<%@ taglib uri="/WEB-INF/mytag.tld" prefix="my"%>
<my:newsImg parenttype="2" childtype="115" focuswidth="330" titleLength="18" />
专业文章 | 阅读 1244 次
文章评论,共0条
游客请输入验证码
浏览290958次