table.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:2k
源码类别:
Java编程
开发平台:
Java
- package tag;
- import java.util.*;
- import javax.servlet.jsp.*;
- import javax.servlet.jsp.tagext.*;
- import java.io.*;
- public class table extends TagSupport {
- private Iterator iterator;
- private String items;
- private String var;
- //设置表头
- private String header;
- private StringBuffer sbTable=new StringBuffer("");
- private int numCols=0;
- private int ctr=0;
- private JspWriter out;
- public void setItems(String items) {
- this.items = items;
- }
- public void setHeader(String header){
- this.header=header;
- }
- public void setVar(String var) {
- this.var = var;
- }
- public int doStartTag( ) throws JspTagException {
- out=pageContext.getOut();
- //render the table header with column names
- sbTable.append("<table border='1' cellspacing='1' cellpadding='0' bgcolor='#DDDDDD' width='100%'><tr>");
- String[] colNames=(String[])pageContext.findAttribute(header);
- if (colNames == null) {
- throw new JspTagException("没有读取到表头设置信息");
- }
- numCols=colNames.length;
- for(int i=0;i<numCols;i++){
- sbTable.append("<td>"+colNames[i]+"</td>");
- }
- sbTable.append("</tr><tr>");
- //get table data
- Collection coll = (Collection) pageContext.findAttribute(items);
- if (coll == null) {
- try {
- out.println("没有相关数据内容");
- }
- catch (IOException ex) {
- }
- return SKIP_BODY;
- }
- iterator = coll.iterator();
- if (iterator.hasNext( )) {
- // pageContext.setAttribute(var, iterator);
- return EVAL_BODY_INCLUDE;
- }
- else {
- return SKIP_BODY;
- }
- }
- public int doAfterBody( ) {
- if (iterator.hasNext( )) {
- ctr++;
- // pageContext.setAttribute(var, iterator.next( ));
- //need start a new row
- // QQQ what if numCols ==1
- sbTable.append("<td>"+(String)iterator.next()+"</td>");
- if((ctr%numCols)==0){
- System.out.println(ctr+"--"+numCols);
- sbTable.append("</tr><tr>");
- }
- return EVAL_BODY_AGAIN;
- }
- else {
- //finish render table data
- sbTable.append("</tr></table>");
- // System.out.println(sbTable);
- try {
- out.print(sbTable);
- }
- catch (IOException ex) {
- }
- return SKIP_BODY;
- }
- }
- }