08-09
29
自定义遍历标签,原始版
作者:Java伴侣 日期:2008-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;
}
}
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;
}
}
评论: 0 | 引用: 0 | 查看次数: 540
发表评论