08-09
29

自定义遍历标签,原始版

这样做主要是为了方便用户自制SKIN
public class IterateTag extends BodyTagSupport {

    private static final long serialVersionUID = -6171671879525533157L;

    Logger log = Logger.getLogger(IterateTag.class);

    // Tag的属性
    private String typeid;

    private String id;

    private String row;

    private String type;

    private String orderby;

    private String att;

    // it为要迭代的对象
    private Iterator it = null;

    /** */
    /**
         * 设置属性collection
         */
    public void setCollection(Collection collection) {
    if (collection.size() > 0)
        it = collection.iterator();
    }

    /** */
    /**
         * 如果it属性为null,那么忽略计算tagbody。
         */
    public int doStartTag() throws JspTagException {

    if (typeid == null)
        return SKIP_BODY;
    else {

        String hql = this.getHql(type, orderby);
        
        Collection li = new ArrayList();

        li = this.getList(hql.toString(), row);
        
        it = li.iterator();
    }

    if (it == null)
        return SKIP_BODY;
    else
        return continueNext(it);
    }

    public int doAfterBody() throws JspTagException {
    return continueNext(it);
    }

    public int doEndTag() throws JspTagException {
    try {
        if (bodyContent != null)
        bodyContent.writeOut(bodyContent.getEnclosingWriter());
    } catch (java.io.IOException e) {
    }

    return EVAL_PAGE;
    }

    /** */
    /**
         * 保护方法,用于把it.next()设置为pagecontext的属性
         */
    protected int continueNext(Iterator it) throws JspTagException {
    if (it.hasNext()) {
        
        pageContext.setAttribute(id, it.next(), PageContext.PAGE_SCOPE);
        return EVAL_BODY_TAG;
    } else {
        return SKIP_BODY;
    }
    }

    public Collection getList(String hql, String row) {

    // System.out.println(hql);

    ArticleService s = new ArticleService();

    List li = null;

    if (type != null && type.equals("rand"))
        li = s.getMaxRsRandArticle(hql, Integer.parseInt(row));
    else
        li = s.getMaxRsManayArticle(hql, Integer.parseInt(row));

    return li;
    }

    public String getHql(String type, String orderby) {

    int label = 0;

    // 切词
    IterateBean bean = new IterateBean();

    StringBuffer addHql = new StringBuffer();

    // 处理typeid
    if (!typeid.equals("0")) {
        
        addHql.append("(");

        addHql.append("a.catalog=" + typeid);

        CatalogService cs = new CatalogService();

        int cid = Integer.parseInt(typeid);

        Catalog catalog = cs.getCatalog(cid);

        //关联的子集
        addHql = bean.getChildId(catalog, addHql);
            
        addHql.append(")");
        
        label = 1;
    }

    // 处理type
    if (type != null) {

        if (label == 1)
        addHql.append(" and ");

        if (type.equals(".image")) {
        addHql.append("a.imgPath!=null");
        label = 1;
        }

    }
    
    log.info("att:"+att);

    if (att != null) {
        if (label == 1)
        addHql.append(" and ");

        addHql.append("a.att ='" + att + "'");
        label = 1;
    }

    String hql = "";
    String oby = "id";

    // 处理orderby
    if (orderby != null) {
        oby = orderby;
    }

    if (label == 0) {
        hql = "from Article as a order by a." + oby + " desc";
    } else {
        hql = "from Article as a where " + addHql + " order by a." + oby
            + " desc";
    }

    log.info("heh hql::" + hql);

    return hql;
    }

    public String getId() {
    return id;
    }

    public void setId(String id) {
    this.id = id;
    }

    public String getRow() {
    return row;
    }

    public void setRow(String row) {
    this.row = row;
    }

    public String getTypeid() {
    return typeid;
    }

    public void setTypeid(String typeid) {
    this.typeid = typeid;
    }

    public String getOrderby() {
    return orderby;
    }

    public void setOrderby(String orderby) {
    this.orderby = orderby;
    }

    public String getType() {
    return type;
    }

    public void setType(String type) {
    this.type = type;
    }

    public String getAtt() {
    return att;
    }

    public void setAtt(String att) {
    this.att = att;
    }

}


[本日志由 blurxx 于 2008-09-29 03:34 PM 编辑]
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: 遍历
相关日志:
评论: 0 | 引用: 0 | 查看次数: 539
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭