08-09
29

自定义遍历标签,修改版

上篇文章的自定义标签我用了一段时间,我发现pageContent的值不能在下一次标签使用时销毁。这样我又改进了下,如下,另增加了indexId属性
public class MyIterateTag 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;

    private String indexId;

    // 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 {

    if (bodyContent != null)
        try {
        bodyContent.writeOut(bodyContent.getEnclosingWriter());
        } catch (IOException e) {
        // TODO 自动生成 catch 块
        e.printStackTrace();
        }

    return continueNext(it);
    }

    public int doEndTag() throws JspTagException {
    it = null;
    flag = 1;

    return EVAL_PAGE;
    }

    /** */
    /**
         * 保护方法,用于把it.next()设置为pagecontext的属性
         */
    int flag = 1;

    protected int continueNext(Iterator it) throws JspTagException {

    if (it.hasNext()) {

        pageContext.setAttribute(id, it.next(), PageContext.PAGE_SCOPE);

        if (indexId != null) {
        pageContext.setAttribute(indexId, new Integer(flag),
            PageContext.PAGE_SCOPE);
        log.info("index" + flag);
        flag++;
        }
        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";
    }

    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;
    }

    public String getIndexId() {
    return indexId;
    }

    public void setIndexId(String indexId) {
    this.indexId = indexId;
    }

}


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