牛仔褲的夏天

          JAVA是藍色的- online

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            30 隨筆 :: 5 文章 :: 15 評論 :: 0 Trackbacks

          單位里的查詢系統是基于struts的,在結果集顯示的處理上都是用 <logic:iterate id=".." indexId="id" name=".." scope="request"> 然后在<bean:write name=".." property=".."> 這樣子對付的,后來覺得比較麻煩,就寫了一個顯示結果集的標簽

          • 行記錄用HashMap保存
          • 結果集用Vector封裝
          • 表格的標題等屬性用xml配置
          • dom4j解析xml文件
          • 輸出時根據配置合并多行內的相同屬性列
          • 求和

          表格配置文件table-config.xml保存在/WEB-INF下



          用于表格配置的Table.java

          package tax.tags;

          import java.util.Vector;

          public class Table {
           
          private String name;
           
          private String width;
           
          private String title;
           
          private String groupby;
           
          private Vector<Column> columns;
           
          public Table(){
           }

           
          public Table(String name){
            
          this.name=name;
           }

           
          public Vector<Column> getColumns() {
            
          return columns;
           }

           
          public void setColumns(Vector<Column> columns) {
            
          this.columns = columns;
           }

           
          public String getName() {
            
          return name;
           }

           
          public void setName(String name) {
            
          this.name = name;
           }

           
          public String getTitle() {
            
          return title;
           }

           
          public void setTitle(String title) {
            
          this.title = title;
           }

           
          public String getWidth() {
            
          return width;
           }

           
          public void setWidth(String width) {
            
          this.width = width;
           }

           
           
          public String getGroupby() {
            
          return groupby;
           }

           
          public void setGroupby(String groupby) {
            
          this.groupby = groupby;
           }

           
          public String toString(){
            StringBuffer buf
          =new StringBuffer();
            buf.append(
          "Name:").append(name).append("\n");
            buf.append(
          "Width:").append(width).append("\n");
            buf.append(
          "Title:").append(title).append("\n");
            buf.append(
          "Groupby:").append(groupby).append("\n");
            
          for(int i=0;i<columns.size();i++){
             Column column
          =columns.elementAt(i);
             buf.append(
          "column name:").append(column.getName()).append("\n");
             buf.append(
          "       header:").append(column.getHeader()).append("\n");
             buf.append(
          "       width:").append(column.getWidth()).append("\n");
             buf.append(
          "       sum:").append(column.isSum()).append("\n");
             buf.append(
          "       bind:").append(column.isBind()).append("\n");
            }

            
          return buf.toString();
           }

          }




          class Column{
           
          private String name;
           
          private String width;
           
          private String header;
           
          private String map;
           
          private boolean sum;
           
          private boolean bind;

           
          public Column(){
            sum
          =false;
            bind
          =false;
           }

           
          public boolean isBind() {
            
          return bind;
           }

           
          public void setBind(boolean bind) {
            
          this.bind = bind;
           }

           
          public String getHeader() {
            
          return header;
           }

           
          public void setHeader(String header) {
            
          this.header = header;
           }

           
          public String getName() {
            
          return name;
           }

           
          public void setName(String name) {
            
          this.name = name;
           }

           
          public boolean isSum() {
            
          return sum;
           }

           
          public void setSum(boolean sum) {
            
          this.sum = sum;
           }

           
          public String getWidth() {
            
          return width;
           }

           
          public void setWidth(String width) {
            
          this.width = width;
           }

           
          public String getMap() {
            
          return map;
           }

           
          public void setMap(String map) {
            
          this.map = map;
           }

          }


           

          用dom4j解析table-config.xml的Dom4jTable.java,因為用到了xpath,所以把jaxen-1.1-beta-6.jar也要拷貝到/web-inf/lib里面去

          package tax.tags;

          import java.util.List;
          import java.util.Vector;

          import org.dom4j.Document;
          import org.dom4j.DocumentException;
          import org.dom4j.Element;
          import org.dom4j.Node;
          import org.dom4j.io.SAXReader;

          public class Dom4jTable {
           String tableName;

           
          public Dom4jTable() { }

           
          public Dom4jTable(String tableName) {
            
          this.tableName = tableName;
           }


           
          public Document parse(String xmlpath) throws DocumentException {
            SAXReader reader 
          = new SAXReader();
            Document doc 
          = null;
            doc 
          = reader.read(xmlpath);
            
          return doc;
           }


           
          public Table getTable(Document doc) {
            Table table;
            String xpath 
          = "//table[@name='" + tableName + "']";
            Node node 
          = doc.selectSingleNode(xpath);
            
          if (node == null{
             table 
          = null;
            }
           else {
             table 
          = new Table(tableName);
             table.setWidth(getValue(node,
          "@width"));
             table.setTitle(getValue(node,
          "title"));
             table.setGroupby(getValue(node,
          "groupby"));
             Vector
          <Column> columns = new Vector<Column>();
             Node nodeColumns 
          = node.selectSingleNode("columns");
             List list 
          = ((Element) nodeColumns).elements();
             
          for (int i = 0; i < list.size(); i++{
              Column column 
          = new Column();
              Element element 
          = (Element) list.get(i);
              column.setName(getValue(element,
          "name"));
              column.setHeader(getValue(element,
          "header"));
              column.setWidth(getValue(element,
          "width"));
              column.setMap(getValue(element,
          "map"));
              column.setBind(Boolean.parseBoolean(getValue(element,
          "bind")));
              column.setSum(Boolean.parseBoolean(getValue(element,
          "sum")));
              columns.add(column);
             }

             table.setColumns(columns);
            }

            
          return table;
           }

           
          private String getValue(Node node,String name){
            String value
          =node.valueOf(name);
            
          if(value.equals("")){
             value
          =null;
            }

            
          return value;
           }

          }



          自定義標簽ShowTableTag.java,寫的有點亂,還好能用,本來用sax解析xml的,后來改為dom4j的

          package tax.tags;

          import java.math.BigDecimal;
          import java.util.HashMap;
          import java.util.Hashtable;
          import java.util.Iterator;
          import java.util.TreeMap;
          import java.util.Vector;

          import javax.servlet.jsp.JspException;
          import javax.servlet.jsp.JspWriter;
          import javax.servlet.jsp.tagext.TagSupport;

          import org.apache.commons.logging.Log;
          import org.apache.commons.logging.LogFactory;
          import org.dom4j.Document;
          import org.dom4j.DocumentException;
          import org.xml.sax.XMLReader;
          import org.xml.sax.helpers.XMLReaderFactory;

          public class ShowTableTag extends TagSupport {
           
          private static final long serialVersionUID = 1L;

           
          private Log log = LogFactory.getLog(this.getClass());

           
          private String vectorName;

           
          private String tableName;

           
          private boolean displaySN = false;

           
          private Vector<HashMap> data = null;

           
          public int doStartTag() throws JspException {
            
          return TagSupport.SKIP_BODY;
           }


           
          public int doEndTag() throws JspException {
            String newline 
          = "\n";
            
          boolean getsum=false;
            JspWriter out 
          = pageContext.getOut();
            StringBuffer buf 
          = new StringBuffer();
            
          if (data == null || data.size() == 0{
             buf.append(
          "There is no result available.");
            }
           else {
          //   取表格的各項屬性設置
             Table table = getTableConfigByDom4j();
          //   取表格的各列,然后存入cols數組
             Vector columns = table.getColumns();
             Column[] cols
          =new Column[columns.size()];
             BigDecimal[] sum
          =new BigDecimal[columns.size()];
             
          for(int i=0;i<columns.size();i++){
              cols[i]
          =(Column)columns.elementAt(i);
              
          if(cols[i].isSum()) getsum=true;
              sum[i]
          =new BigDecimal(0);
             }

             
          /////////////////////////////////////////////////////////////////////////////////////////
          //   如果table沒有設置title, 就不打印這部分
             if(table.getTitle()!=null){
              buf.append(
          "<DIV id=tag_head><div class=label>").append(newline);
              buf.append(table.getTitle()).append(
          "</div></div><div id=tag_body>");
              buf.append(newline);
             }

             
             buf.append(
          "<div class=content>").append(newline);
             buf.append(
          "<table border=1 cellPadding=0 cellSpacing=0 ");
             buf.append(
          "borderColorLight=#000000 borderColorDark=#f8f7f5 width=\"");
             buf.append(table.getWidth()).append("\">").append(newline);
             buf.append("<tr class=tablehead>").append(newline);
             
          //   如果標簽中的displaySN為true, 則打印序號列
             if (displaySN) {
              buf.append(
          "<th>序號</th>").append(newline);
             }


          //   打印table的th欄
             for (int i = 0; i < cols.length; i++{
              buf.append(
          "<th");
              
          if (cols[i].getWidth() != null){
               buf.append(
          " width=").append(cols[i].getWidth());
              }

              buf.append(
          ">").append(cols[i].getHeader()).append("</th>");
              buf.append(newline);
             }

             buf.append(
          "</tr>").append(newline);


          //   由table的groupby屬性產生key_group<Integer,Integer>
          //   第一個Integer是行號,從1開始
          //   第二個Integer是跨度span,即連續相同的key的數量
             Hashtable key_group = getKeyGroup(table.getGroupby());
             
             
          int rowid = 0//行號
             int distid = 0//捆綁相同key行后的行號
             Iterator<HashMap> it = data.iterator();
             
          while (it.hasNext()) {
              buf.append(
          "<tr class=tablepad>").append(newline);
              
              rowid
          ++;
              
          //    根據行號從key_group中取得pan值, span可能為null  
              Integer span=(key_group==null?null:(Integer)key_group.get(new Integer(rowid)));

          //     顯示序號
              if (displaySN) {
               
          if(key_group==null){
                
          //key_group為null表示table中不用合并相同的行
                buf.append("<td>").append(rowid).append("</td>");
               }
          else if(span!=null){
                buf.append(
          "<td rowspan=").append(span).append(">");
                buf.append(
          ++distid).append("</td>").append(newline);
               }

              }

          //    取得這一行的記錄
              HashMap row = (HashMap)it.next();
              
          for (int i = 0; i < cols.length; i++{
          //     取得td中應該顯示的內容
               String td = (String) row.get(cols[i].getName());
               
          if(cols[i].isSum()){
                sum[i]
          =sum[i].add(new BigDecimal(td));
               }

               
          if(cols[i].getMap()!=null){
                TreeMap map 
          = (TreeMap) pageContext.getServletContext()
                  .getAttribute(cols[i].getMap());
                td
          =(String)map.get(td);
               }

               
          if (td == null) td = "&nbsp;";
          //     區別這個列的bind屬性確定是否要合并顯示
               if(!cols[i].isBind()||key_group==null){
                buf.append(
          "<td>");
                buf.append(td).append(
          "</td>").append(newline);
               }
          else if(span!=null){
                buf.append(
          "<td rowspan=").append(span).append(">");
                buf.append(td).append(
          "</td>").append(newline);
               }

              }

              buf.append(
          "</tr>").append(newline);
             }

             
          //如果需要打印合計數,增加一行
             if(getsum){
              buf.append(
          "<tr class=tablepad>");
              
          if(displaySN){
               buf.append(
          "<td>&nbsp</td>");
              }

              
          for(int i=0;i<cols.length;i++){
               
          if(cols[i].isSum()){
                buf.append(
          "<td>").append(sum[i]).append("</td>").append(newline);
               }
          else{
                buf.append(
          "<td>&nbsp</td>").append(newline);
               }

              }

              buf.append(
          "</tr>").append(newline);
             }

             buf.append(
          "</table>").append(newline);
             buf.append(
          "</div></div>");
            }

            
          try {
             out.println(buf.toString());
            }
           catch (Exception e) {
             log.error(
          "e");
             
          return TagSupport.SKIP_PAGE;
            }

            
          return TagSupport.EVAL_PAGE;
           }



           
          private Hashtable getKeyGroup(String key) {
            
          if(key==nullreturn null;
          //  返回Hashtable, 鍵-從1開始的行號. 值-從這行開始連續的rowspan值.
            Hashtable<Integer, Integer> ht = new Hashtable<Integer, Integer>();
            Iterator it 
          = data.iterator();
            
          int count = 0;
            
          int start = 0;
            
          int current = 0;
            String value 
          = "", prevalue = "";

            
          while (it.hasNext()) {
             current
          ++;
             HashMap row 
          = (HashMap) it.next();
             value 
          = (String) row.get(key);
             
          if (value.equals(prevalue))
              count
          ++;
             
          else {
              ht.put(
          new Integer(start),new Integer(count));
              prevalue 
          = value;
              count 
          = 1;
              start
          =current;
             }

            }

            ht.put(
          new Integer(start),new Integer(count));
            ht.remove(
          new Integer(0));
            
          return ht;
           }


           
          private Table getTableConfigBySax() {
            Table table 
          = null;
            SaxTable sax 
          = new SaxTable();
            sax.setTableName(tableName);
            String xmlfile 
          = pageContext.getServletContext().getRealPath(
              
          "WEB-INF/table-config.xml");
            
          try {
             XMLReader xdr 
          = XMLReaderFactory
               .createXMLReader(
          "org.apache.xerces.parsers.SAXParser");
             xdr.setContentHandler(sax);
             xdr.parse(xmlfile);
            }
           catch (Exception e) {
             log.error(e);
            }

            table 
          = sax.getTable();
            
          return table;
           }


           
          private Table getTableConfigByDom4j(){
            Dom4jTable dt 
          = new Dom4jTable(tableName);
            Document doc 
          = null;
            String xmlPath 
          = pageContext.getServletContext().getRealPath(
              
          "WEB-INF/table-config.xml");
            
          try {
             doc 
          = dt.parse(xmlPath);
            }
           catch (DocumentException e) {
             log.error(e);
             
          return null;
            }

            Table table 
          = dt.getTable(doc);
            
          return table;
           }

           
           
          public String getTableName() {
            
          return this.tableName;
           }


           
          public void setTableName(String tableName) {
            
          this.tableName = tableName;
           }


           
          public String getVectorName() {
            
          return this.vectorName;
           }


           
          public void setVectorName(String vectorName) {
            
          this.vectorName = vectorName;
            Object o 
          = pageContext.getRequest().getAttribute(vectorName);
            
          if (o != null{
             data 
          = (Vector<HashMap>) o;
            }
           else {
             data 
          = null;
            }

           }


           
          public void setDisplaySN(boolean sn) {
            displaySN 
          = sn;
           }


           
          public boolean isDisplaySN() {
            
          return displaySN;
           }

          }




          在標簽庫app.tld中的這一段tag定義

           <tag>
            
          <name>showTable</name>
            
          <tagclass>tax.tags.ShowTableTag</tagclass>
            
          <bodycontent>empty</bodycontent>
            
          <info>
             extract the date from a vector and present date in table
            
          </info>
            
          <attribute>
             
          <name>vectorName</name>
             
          <required>true</required>
             
          <rtexprvalue>true</rtexprvalue>
            
          </attribute>
            
          <attribute>
             
          <name>tableName</name>
             
          <required>true</required>
             
          <rtexprvalue>true</rtexprvalue>
            
          </attribute>
            
          <attribute>
             
          <name>displaySN</name>
             
          <required>false</required>
             
          <rtexprvalue>true</rtexprvalue>
            
          </attribute>
           
          </tag>



          最后就可以在jsp文件中使用了,例如:

          <app:showTable tableName="jcgl_dzjk" vectorName="data" displaySN="true"/>



          還沒有在標簽里做分頁的處理,以后在加工吧

          posted on 2006-03-10 13:12 luckyrobbie 閱讀(1163) 評論(1)  編輯  收藏 所屬分類: Struts & Hibernate

          評論

          # re: 顯示結果集的標簽 2006-03-10 13:31 xfan
          用display這些標簽不就可以了嗎,多個配置文件麻煩  回復  更多評論
            

          主站蜘蛛池模板: 榆社县| 大同县| 昭通市| 阜城县| 湟中县| 灌云县| 宁乡县| 黄龙县| 民勤县| 罗平县| 德阳市| 建宁县| 广德县| 本溪市| 马公市| 金阳县| 长宁区| 陕西省| 柳河县| 石河子市| 马尔康县| 乌兰察布市| 宜章县| 永年县| 东台市| 峨眉山市| 五寨县| 河池市| 财经| 新河县| 陵川县| 溧水县| 渑池县| 绿春县| 梓潼县| 东兴市| 阳西县| 阳江市| 金华市| 高邑县| 和平县|