Jsp 自定義標簽 嵌套自帶標簽
定義標簽處理類:
Copy to Clipboard引用的內容:[www.bkjia.com]
package com.f139.frame.tag;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.entity.Entity;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.Ioc;
import org.nutz.mvc.Mvcs;
import com.f139.frame.pojo.business.BizBuy;
/**
* 求購標簽
* @author Administrator
*
*/
public class BizBuyTag extends TagSupport {
private static final long serialVersionUID = 1L;
private int num; // 顯示條數
private int vid; // 專區ID
private int orderType; // 排序類型,0:推薦的供應信息,按時間排序, 1:按時間排序,2:按點擊次數排序
public int doEndTag() throws JspException {
return EVAL_PAGE;
}
public int doStartTag() throws JspException {
List<BizBuy> list= getList();
pageContext.setAttribute("list",list);
return EVAL_PAGE;
}
private List<BizBuy> getList() {
StringBuffer conBuf = new StringBuffer();
String topStr = "";
if(this.getVid()>0){
conBuf.append(" (charindex(','+'"+this.getVid()+"'+',',','+nodepath+',')>0 or b.vid = "+this.getVid()+")" );
}
if(this.getNum()>0){
topStr = " top " + this.getNum() + " ";
}else {
topStr = " top 10 ";
}
if(this.getOrderType()==0){
conBuf.append(" and IsElite='True' order by updateTime desc");
}else if(this.getOrderType()==1){
conBuf.append(" order by updateTime desc");
}else if(this.getOrderType()==2){
conBuf.append(" order by Hits,updateTime desc");
}else {
conBuf.append(" order by updateTime desc");
}
StringBuffer buySql=new StringBuffer();
buySql.append("select ").append(topStr).append(" b.buyID,b.title,b.updateTime from f139Business.dbo.Buy as b left join DataCenter.dbo.Variety as v on b.vid = v.id where b.status = 1 and DATEADD(d,b.validDate,b.updatetime)>getdate() and ");
buySql.append(conBuf);
// 得到數據
Ioc ioc = Mvcs.getIoc(pageContext.getServletContext());
Dao dao = ioc.get(Dao.class, "dao");
Sql sql = Sqls.create(buySql.toString());
sql.setCallback(Sqls.callback.entities());
System.out.println("sql語句:"+sql);
Entity<BizBuy> entity = dao.getEntity(BizBuy.class);
sql.setEntity(entity);
dao.execute(sql);
List<BizBuy> list = sql.getList(BizBuy.class);
return list;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getVid() {
return vid;
}
public void setVid(int vid) {
this.vid = vid;
}
public int getOrderType() {
return orderType;
}
public void setOrderType(int orderType) {
this.orderType = orderType;
}
}
定義TLD:
Copy to Clipboard引用的內容:[www.bkjia.com]
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.0</jsp-version>
<short-name>Tab</short-name>
<uri>http://www.f139.com</uri>
<display-name>buy</display-name>
<description>求購信息模塊</description>
<tag>
<name>buyTag</name>
<tag-class>com.f139.frame.tag.BuyTag</tag-class>
<body-content>JSP</body-content>
<display-name></display-name>
<description>求購信息模塊</description>
<attribute>
<name>num</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>java.lang.Integer</type>
<description>讀取求購信息的條數</description>
</attribute>
<attribute>
<name>vid</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>java.lang.Integer</type>
<description>內部平台Variety表的vid</description>
</attribute>
<attribute>
<name>orderType</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>java.lang.Integer</type>
<description>排序類型,0:推薦的供應信息,按時間排序, 1:按時間排序,2:按點擊次數排序</description>
</attribute>
</tag>
</taglib>
使用:
Copy to Clipboard引用的內容:[www.bkjia.com]
<buy:buyTag vid="10" orderType="1" num="10" >
<c:forEach var="b" items="${list}">
${b.title}
</c:forEach>
</buy:buyTag>