Jsp 自定义标签 镶套自带标签

摘要

Jsp 自定义标签 镶套自带标签,在项目中,往往程序语言自定义的标签不满足于需求,可以根据实际情况,自己开发标签使用。

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

<?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>


IT家园
IT家园

网友最新评论 (0)