zoukankan      html  css  js  c++  java
  • displaytag 动态列实现

    这种动态列的实现方法来自displaytag-examples-1.2.war提供的示例中,实际上下载下来的zip文件中不仅有各种jar包,还有这个包含各种例子的war包,是学习displaytag的非常好的资料。

    献上完整代码:

    <jsp:root version="1.2" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:display="urn:jsptld:http://displaytag.sf.net/el"
      xmlns:c="urn:jsptld:http://java.sun.com/jstl/core">
      <jsp:directive.page contentType="text/html; charset=UTF-8" />
      <jsp:directive.page import="org.displaytag.sample.*" />
      <jsp:include page="inc/header.jsp" flush="true" />
    
      <jsp:scriptlet>
    
      // available column sets should be managed in the backend, not in the jsp page.
      // you just need to build a list with Maps containing standard column attributes
    
      java.util.Map email = new java.util.HashMap();
      email.put("property", "email");
      email.put("title", "email title");
    
    
      java.util.Map date = new java.util.HashMap();
      date.put("property", "date");
      date.put("title", "date");
      date.put("sortable", Boolean.TRUE);
    
    
      java.util.Map money = new java.util.HashMap();
      money.put("property", "money");
      money.put("title", "money");
      money.put("format", "{0,number,000.00 €}");
    
      java.util.List set1 = new java.util.ArrayList();
      java.util.List set2 = new java.util.ArrayList();
      java.util.List set3 = new java.util.ArrayList();
    
      set1.add(email);
      set1.add(date);
      set1.add(money);
    
      set2.add(money);
      set2.add(email);
    
      set3.add(date);
      set3.add(date);
    
      // this is the logic for choosing a column set
      // should be done in a controller/viewer helper, not in the jsp
    
      String choose = request.getParameter("set");
      if ("3".equals(choose))
      {
          request.setAttribute("collist", set3);
      }
      else if ("2".equals(choose))
      {
          request.setAttribute("collist", set2);
      }
      else
      {
          request.setAttribute("collist", set1);
      }
    
      // just prepare the usual list
      request.setAttribute( "test", new TestList(10, false) );
    
      </jsp:scriptlet>
    
    
      <h2>Using predefined column lists</h2>
    
      <p>This example shows how a predefined set of columns can be applied to a table.</p>
      <p>All you have to do is feeding the table with a list of beans/Maps which contains the needed column attributes and
      create the needed <code><![CDATA[<display:column>]]></code> tags and use a simple iteration directly in the jsp</p>
      <p>In this page we create three different set of colums, implemented as Lists of Maps, directly in the jsp. Obviously,
      you will probably want to configure them in a db, xml file, or any other source for a real use (Spring xml files could
      be an easy and flexible solution). No dipendence on displaytag-specific classes is required in your application, since
      you can simply use a plain Map or implement a custom bean.</p>
      <p>Click on a column set below to see it applied to the table.</p>
    
      <br />
      <br />
    
      <ul id="stylelist">
        <li><a href="example-columnlist.jsp?set=1">column set 1</a></li>
        <li><a href="example-columnlist.jsp?set=2">column set 2</a></li>
        <li><a href="example-columnlist.jsp?set=3">column set 3</a></li>
      </ul>
    
      <br />
      <br />
    
      <display:table name="test">
        <c:forEach var="cl" items="${collist}">
          <display:column property="${cl.property}" title="${cl.title}" sortable="${cl.sortable}" format="${cl.format}" />
        </c:forEach>
      </display:table>
    
      <jsp:include page="inc/footer.jsp" flush="true" />
    
    </jsp:root>
    View Code

     整个逻辑非常简单,下面是我的分析:

    (1)首先是整个例子由于非常简单,所以将数据层,控制层,显示层都放在了一个jsp文件中了,文件中也提到了实际上是不应该这么做的。

    (2)再想一想到底什么是动态列?首先是同一个jsp页面(如果是不同的jsp页面,直接就是静态的就可以得到不同的显示效果),不同的时候(一般是根据不同的人具有不同的权限,额这是我猜的)得到的显示结果不一样(是指得到的结果具有不一样的列,但是其bean数据还是一样的,只是显示bean的不同属性

    (3)而我们正常情况下的操作就是<display:table></display:table>中包含若干个我们要显示的bean属性,也就是列名。我们发现<display:column>的结构是非常相似的,于是我们可以使用<c:forEach>循环输出我们想要的<c:column>即可

    (4)如果要达到动态列的效果,关键在于控制<c:forEach>所遍历的集合,分析上述代码,知道该集合是一个Set,其元素是一个个Map元素。每一个Map元素对应一个bean属性,也就是一列。这个Map元素由若干个名值对组成,每个名值对就是<c:column>中的一个属性。

    (5)剩下的就是点击页面上的按钮,传递不同的参数,由后台控制Set集合到底包含哪些列。还有一点需要注意,就是显示的时候,在遍历Set集合时,<c:column>中的属性必须写全,它应该包含所有的Map元素可能包含的所有属性,并不是所有的Map元素都包含同样的属性。

    (6)了解了整个原理之后,在应用这个动态列的时候,最主要的反而是控制层的实现,就是说到底什么时候显示哪些列,其他时候又是显示另一些列,这必须做成容易扩展的,因为这是一个非常容易变化的需求点,甚至于突然增加一种情况。可以考虑XML文件,或者数据库保存。

  • 相关阅读:
    远程管理登录Linux
    Linux常用命令之文件搜索命令
    PowerPoint笔记(七)
    PowerPoint笔记(六)
    C Primer Plus(四)
    Linux常用命令之文件处理命令
    Linux常用命令之权限管理命令
    C Primer Plus(五)
    C Primer Plus(六)
    C Primer Plus(三)
  • 原文地址:https://www.cnblogs.com/3tree/p/3475964.html
Copyright © 2011-2022 走看看