jsp 自定义标签-foreach标签,集合是后台查询的,不是请求得到的


1.在WEB-INF,tld文件夹下面建立一个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.2</jsp-version>  
     <short-name>cc</short-name>  
     <uri>www.content_channel.com</uri>  
       
     <tag>  
     <name>content</name>  
     <tag-class>ContentByChannel</tag-class>  
     <body-content>jsp</body-content>  
     <attribute>  
     <name>channelId</name>  
     <required>true</required>  
     <rtexprvalue>true</rtexprvalue>  
    </attribute>  
     <attribute>
            <name>var</name>
            <required>true</required>
            <rtexprvalue>false</rtexprvalue>
            <type>java.lang.String</type>
        </attribute>
     </tag>  
    </taglib> 

2.写一个ContentByChannel类继承TagSupport

package org.jeecgframework.tag.soeasytag;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspException;

import javax.servlet.jsp.tagext.TagSupport;

import org.jeecgframework.core.util.ApplicationContextUtil;
import org.jeecgframework.web.system.service.SystemService;

public class ContentByChannel extends TagSupport{
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private static SystemService systemService= ApplicationContextUtil.getContext().getBean(SystemService.class);
    private String channelId;  
    private String var;
    private Iterator<Object> it;

    public void setVar(String var) {
        this.var = var;
    }
    public void setChannelId(String channelId){  
        this.channelId = channelId;  
    }  
    
    
    public ContentByChannel(){  

    }  


 
    public void setItems() {
        String sql="select t1.CONTENT,t1.TITLE,t1.COLUMNID,t1.AUTHOR,date_format(t1.CREATETIME,'%Y/%m/%d') as CREATETIME,t1.IMAGEPATH  from tb_article_info t1 LEFT JOIN tb_article_column t2 on t1.COLUMNID=t2.ID where t2.ID="+"'"+channelId+"'";
        List<Map<String, Object>> listobj = systemService.findForJdbc(sql);
        Object object=listobj;
        if (object instanceof Collection) {
            it = ((Collection) object).iterator();
        } else if (object instanceof Map) {
            it = ((Map) object).entrySet().iterator();
        } else if (object.getClass().isArray()) {
            List<Object> list = new ArrayList<Object>();
            int len = Array.getLength(object);
            for (int i = 0; i < len; i++) {
                list.add(Array.get(object, i));
            }
            it = list.iterator();
        }
    }

    
    public int doStartTag() {
        setItems();
        if (it.hasNext()) {
            pageContext.setAttribute(var, it.next());
            return EVAL_BODY_INCLUDE;
        } else {
            return SKIP_BODY;
        }
    }  
    public int doAfterBody() throws JspException{  
        if (it.hasNext()) {
            pageContext.setAttribute(var, it.next());
            return EVAL_BODY_AGAIN;// 再次执行body部分
        } else {
            return SKIP_BODY;
        }
    }  
    public int doEndTag() throws JspException{  
        pageContext.removeAttribute(var);
        return EVAL_PAGE;
    }
}

备注:setItems可以是request传过来的,也可以是自己查询的


3.jsp页面用

<%@ taglib prefix="cc" uri="www.content_channel.com"  %> 

 <ul>
  <cc:content channelId="402881824aab8177014aab83d7930003" var="c">
    <li><a href="file:///C:/Users/MY/Desktop/web/www/${c.COLUMNID}/${c.CREATETIME}/${status.index+1}.shtml" target="_blank">${c.TITLE}</a>${c.CREATETIME}</li>
       
    </cc:content>
</ul>


你可能感兴趣的:(jsp,自定义标签-foreach标签,集合是后台查询的,不是请求得到的)