JSP table template upgrade (3) -- table template with variable Columns

Source: Internet
Author: User

Previously, JSP table template upgrade (1) -- Dynamic specifying methods and parameters using Java reflection has implemented dynamic specifying specific business methods, achieving a certain degree of reusability, however, in actual use, a list with only two columns cannot meet most display requirements. Therefore, this article transformed the table template and implemented a dynamic table template with variable columns.

The first is the change in the data structure. Because the table changes from the Key-Value form of two columns to a variable column, the data structure strain is one-to-many relationship. For the sake of simplicity, the author defines a class in the form of Key-ValueList, representing the data of a row, where "key" is a description of a row, here, the author maps it to the value of the first column in the table. ValueList is a list that stores string values of any length:

package com.reports.charts.bean; import java.util.List; public classKeyValueListBean {    private String Key;    private List
 
   Value;       public KeyValueListBean() {    }    public KeyValueListBean(String key,List
  
   value) {        Key = key;        Value = value;    }       public String getKey() {        return Key;    }    public void setKey(String key) {        Key = key;    }    public List
   
    getValue() {        return Value;    }    public void setValue(List
    
      value) {        Value = value;    }   }
    
   
  
 

All the data in the entire table is finally assembled into a KeyValueListBean List. The first element of the List stores the column headers of the table. After the List is put into the request, it is traversed in the jsp code:

             
 
         
 
                 
 
 
 
$ {List [0]. key} $ {Ch}
$ {Row. key} $ {Col}

Considering the reusability of methods, the data structure used here is not appropriate. Aside from other factors, but for a jsp table template, we can use a List Type List, or simply use a two-dimensional string array to transmit data, the corresponding code is similar, I will not go into details, the final table effect is similar:

The complete sample code of jsp is as follows: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHAgYWxpZ249 "left">

<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <% @ taglib uri =" http://java.sun.com/jsp/jstl/core "Prefix =" c "%> <% @ page import =" com. reports. util. reportFunctions "%> <% @ page import =" com. reports. util. springContextHolder "%> <% @ page import =" com. reports. charts. bean. keyValueListBean "%> <% @ page import =" java. lang. reflect. method "%> <% @ page import =" java. util. * "%> <% Stringzoneid = request. getParameter ("zoneid"); if (zoneid = null) {zoneid = "000000";} String dateFrom = request. getParameter ("dateFrom"); StringdateTo = request. getParameter ("dateTo"); // this parameter is a String passed by the front-end that is separated by commas (,) String categories = request. getParameter ("categories"); String deptProperty = request. getParameter ("deptProperty"); String deptCode = request. getParameter ("deptCode"); String deptNum = request. getParameter ("deptNum"); String methodname = request. getParameter ("method"); ReportFunctionsreportF = SpringContextHolder. getBean (ReportFunctions. class); Methodmethod = null; List
 
  
List = null; if (methodname. equals ("deptCategoryByZoneid") | methodname. equals ("elevMonitorByZoneid") | methodname. equals ("elevTestStatusByzoneid") {method = reportF. getClass (). getMethod (methodname, String. class); list = (List
  
   
) Method. invoke (reportF, zoneid);} else if (methodname. equals ("maintenanceTimelyCategory") | methodname. equals ("maintenanceTimeQualify") {method = reportF. getClass (). getMethod (methodname, String. class, String. class, String. class); list = (List
   
    
) Method. invoke (reportF, zoneid, dateFrom, dateTo);} else if (methodname. equals ("elevAlarmRateByDept") {List
    
     
Lc = newArrayList
     
      
(); String [] ac = categories. split (","); for (int I = 0; I) method. invoke (reportF, deptProperty, zoneid, dateFrom, dateTo, deptNum, lc);} else {List
      
        Lc = newArrayList
       
         (); String [] ac = categories. split (","); for (int I = 0; I) method. invoke (reportF, zoneid, dateFrom, dateTo, deptProperty, deptCode, lc);}/* Self-Test Data List
        
          List = new ArrayList
         
           (); List
          
            Tl = new ArrayList
           
             (); Tl. add ("elemenet"); tl. add ("atest"); tl. add ("haha"); KeyValueListBeantemp = new KeyValueListBean ("test", tl); list. add (temp); list. add (temp); */request. setAttribute ("list", list); %>
             
            
             
            
             
            
            
 
$ {List [0]. key} $ {Ch}
$ {Row. key} $ {Col}

At this point, our jsp table template is almost perfect and can meet most of the requirements. Of course, this table is only the most basic two-dimensional table, and the style is also very simple. For complex tables, if the reusability is relatively high and needs to be organized into templates, it is not a problem to follow the ideas of this series of articles, but the data structure and style may be more complex. Style issues are simpler. As long as you have an artist's design, you can adjust it slowly ~



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.