Believe it,do it!

          Ideal is the beacon. Without ideal, there is no secure direction; without direction ,there is no life.
          理想是指路明燈。沒(méi)有理想,就沒(méi)有堅(jiān)定的方向;沒(méi)有方向,就沒(méi)有生活。
          CTRL+T eclipse
          posts - 35, comments - 3, trackbacks - 0, articles - 0
            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          Display Tag使用小記收藏

          Posted on 2008-09-27 15:03 三羽 閱讀(425) 評(píng)論(0)  編輯  收藏 所屬分類: 收 藏 夾
              Display Tag Lib是一個(gè)標(biāo)簽庫(kù),用來(lái)處理jsp網(wǎng)頁(yè)上的Table,功能非常強(qiáng),可以對(duì)的Table進(jìn)行分頁(yè)、數(shù)據(jù)導(dǎo)出、分組、對(duì)列排序等等,反正我在做項(xiàng)目時(shí)需要的功能它都給我提供了,而且使用起來(lái)非常的方便。能夠大大減少代碼量。
              介個(gè)是Display Tag的官方網(wǎng)站http://displaytag.sourceforge.net/

              首先當(dāng)然是要下載它的jar包了,這里可以下載到最新的版本。將jar包放到WEB-INF的lib文件夾下。另外還需要兩個(gè)輔助包:apache的commons-lang和standard包,更多的輔助包可以在這里下載

              在web.xml下添加一個(gè)filter
              <filter>
                  <filter-name>exportFilter</filter-name>
                  <filter-class>org.displaytag.filter.ResponseOverrideFilter</filter-class>
              </filter>

              在jsp頁(yè)面做一個(gè)引用:
          <%@ taglib uri="http://displaytag.sf.net/el" prefix="display" %>

              首先我們定義一個(gè)list
          <%
           List test = new ArrayList( 6 );
           test.add( "Test String 1" );
           test.add( "Test String 2" );
           test.add( "Test String 3" );
           test.add( "Test String 4" );
           test.add( "Test String 5" );
           test.add( "Test String 6" );
           request.setAttribute( "test", test );
          %>

              當(dāng)我們想在jsp頁(yè)面上顯示這個(gè)list時(shí),我們只需要寫一句話
              <display:table name="test" />
              display tag會(huì)自動(dòng)生成一個(gè)table

              如果list是從控制層拋出來(lái)的,name可使用EL表達(dá)式表示
              <display:table name="${test}" />

              這是最簡(jiǎn)單的display tag的使用,我們可以給它加上樣式等,也可以定義顯示的列,下面的table顯示復(fù)雜一些
          <display:table name="test" styleClass="list" cellspacing="0" cellpadding="0">
            <display:column property="id" title="ID" class="idcol"/>
            <display:column property="name" />
            <display:column property="email" />
            <display:column property="description" title="Comments"/>
          </display:table>

              如果想要給它加個(gè)鏈接也很簡(jiǎn)單,下面的代碼給name加了連接,并附帶id參數(shù),email也自動(dòng)連接到mailto:XXX
          <display:table name="test" styleClass="list" cellspacing="0" cellpadding="0">
            <display:column property="id" title="ID" class="idcol"/>
            <display:column property="name" url="detail.jsp" paramId="id" paramProperty="id"/>
            <display:column property="email" autolink="true"/>
            <display:column property="description" title="Comments"/>
          </display:table>

          下面介紹幾個(gè)Display最常用的功能,更多功能請(qǐng)參考http://displaytag.homeip.net/displaytag-examples-1.1/
          1. 分頁(yè)
              如果想對(duì)代碼分頁(yè),只需在display:table標(biāo)簽中添加一項(xiàng)pagesize="每頁(yè)顯示行數(shù)",如
          <display:table name="test" pagesize="10"/>

          2. 對(duì)列排序
              display tag可對(duì)列進(jìn)行排序,就是點(diǎn)擊列名,對(duì)該列的數(shù)據(jù)進(jìn)行排序。你只需對(duì)想要排序的列添加 sort="true" 就OK,如下面的代碼可對(duì)前三列進(jìn)行排序。在display:table中添加defaultsort="列數(shù)",可默認(rèn)對(duì)指定的列排序。
          <display:table name="test" styleClass="list" cellspacing="0" cellpadding="0" defaultsort="1">
            <display:column property="id" title="ID" class="idcol" sort="true"/>
            <display:column property="name" url="detail.jsp" paramId="id" paramProperty="id" sort="true"/>
            <display:column property="email" autolink="true" sort="true"/>
            <display:column property="description" title="Comments"/>
          </display:table>
             如果table有分頁(yè),Display Tag默認(rèn)只對(duì)當(dāng)前頁(yè)進(jìn)行排序,如果想對(duì)整個(gè)list排序,可以在display:table之間添加一段代碼:
          <display:setProperty name="sort.amount" value="list"/>

          3. 導(dǎo)出數(shù)據(jù)
              在display:table中添加export="true",看看會(huì)出現(xiàn)什么!Display Tag默認(rèn)會(huì)提供三種數(shù)據(jù)導(dǎo)出方式:CSV、Excel、XML 。
              另外Display Tag還可以導(dǎo)出為PDF格式,在http://prdownloads.sourceforge.net/itext/下載一個(gè)輔助包iText.jar,copy到lib目錄下,然后在display:table之間添加一段代碼:
          <display:setProperty name="export.pdf" value="true"/>,大功告成。

          4. Display Tag的屬性設(shè)置
              前面所說(shuō)的display:setProperty 是一種改變Display Tag屬性的方法,但是在每個(gè)jsp中都要寫太麻煩了。
              Display Tag中設(shè)置了很多默認(rèn)的屬性,它有一個(gè)專門的屬性文件,是在它的jar包中的displaytag/properties/TableTag.properties
              想要改變它的默認(rèn)屬性,我們可以在WEB-INF\classes下新建一個(gè)文件displaytag.properties,仿照TableTag.properties中屬性的格式設(shè)置需要修改的屬性。
              TableTag.properties中的# messages中設(shè)置的是顯示在頁(yè)面上的提示信息。默認(rèn)是英文的,我們可以把它改為中文的。不過(guò)這里只能使用unicode,就是說(shuō)中文字符必須轉(zhuǎn)換為unicode碼,這個(gè)可以使用jdk自帶的native2ascii.exe進(jìn)行轉(zhuǎn)換。

          5. 其它功能
              DisplayTag還有一些很實(shí)用的小功能,這里提兩個(gè)。一個(gè)是對(duì)數(shù)據(jù)的Format,這是1.1版本添加的新功能,可以使用標(biāo)簽的方式格式化時(shí)間、數(shù)字、字符串。比如日期,在需要格式化的column標(biāo)簽中添加format="{0,date,yyyy-MM-dd}",第一個(gè)參數(shù)為格式化的數(shù)據(jù)序號(hào),第二個(gè)參數(shù)是數(shù)據(jù)類型,數(shù)字為number,第三個(gè)參數(shù)為數(shù)據(jù)格式。
              另外一個(gè)功能是對(duì)table數(shù)據(jù)的合計(jì)功能。在table標(biāo)簽中添加 decorator="org.displaytag.decorator.TotalTableDecorator",然后在想要進(jìn)行合計(jì)的數(shù)據(jù)列的column標(biāo)簽中添加 total="true",該列就可以被計(jì)算總數(shù)了。但這個(gè)功能有個(gè)缺點(diǎn),不能用在有分頁(yè)的時(shí)候,它只能合計(jì)第一頁(yè)的數(shù)據(jù)。

          DisplayTag的不足
              初次使用DisplayTag的人可能會(huì)覺(jué)得驚喜,但是用久了會(huì)發(fā)現(xiàn)很多問(wèn)題,最大的問(wèn)題是對(duì)中文的支持不好,比如如果查詢條件中有中文,就無(wú)法翻頁(yè),無(wú)法對(duì)中文排序,將中文導(dǎo)出為指定文件時(shí)出現(xiàn)亂碼等等。這些問(wèn)題有時(shí)候會(huì)讓人很郁悶,有時(shí)候逼得你要去修改它的源代碼。下面是對(duì)以上幾個(gè)問(wèn)題的解決方法:
              1. 對(duì)于中文無(wú)法翻頁(yè)、排序,最簡(jiǎn)單的辦法是修改Tomcat下的server.xml文件。找到HTTP的Connector標(biāo)簽,在里面添加一項(xiàng)URIEncoding="...",引號(hào)里面的內(nèi)容取決于你的頁(yè)面編碼,比如可以是GBK,UTF8等。這樣上面兩個(gè)問(wèn)題就可以解決了。
              2. 導(dǎo)出為文件:其實(shí)這個(gè)功能除了中文支持外還有很多其它問(wèn)題,比如它會(huì)將Html標(biāo)簽一起導(dǎo)出、只導(dǎo)出顯示的內(nèi)容,但如果對(duì)table進(jìn)行了decorator,decorator后的內(nèi)容無(wú)法導(dǎo)出。如果想要將中文正確導(dǎo)出,需要修改DisplayTag源代碼。
              下載相同版本的源代碼,在org.displaytag.export.ExcelView.java文件中找到getMimeType()方法,將此方法修改為 return "application/vnd.ms-excel;charset=GB2312";,修改后導(dǎo)出數(shù)據(jù)的速度會(huì)慢很多,不過(guò)將就吧。
              3. 新版的DisplayTag1.1添加了對(duì)一次取部分?jǐn)?shù)據(jù)的支持,相關(guān)的標(biāo)簽包括partialList和size,需要設(shè)置partialList="true"和size的大小。具體怎么用偶還沒(méi)研究。

          亂碼解決:

          tomcat/jboss   修改server.xml

          <Connector port="8001" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" debug="0" connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="GBK" />


          .整合tomcat和apache后的設(shè)置
              剛開(kāi)始我沒(méi)有整合的時(shí)候傳遞的中文參數(shù)可以很好的解決,后來(lái)整合apache后,卻出現(xiàn)亂碼了,繞過(guò)apache后是正常的,所以問(wèn)題出現(xiàn)在apache或apache和tomcat的通信上,經(jīng)過(guò)baidu,好不容易找到答案。修改server.xml文件中的如下行:

            這出現(xiàn)apache和tomcat的通信上,由于我這里的整合是采用JK,tomcat使用的是ajp13協(xié)議,所以,需要在他們的通信階段來(lái)處理編碼,即代碼中的URIEncoding="UTF-8"部分。
           
          websphere 
          “服務(wù)器”-->應(yīng)用程序服務(wù)器,找到自己應(yīng)用使用的服務(wù)器。
          WS6有不同,注意。
          找到“服務(wù)器基礎(chǔ)結(jié)構(gòu)”,展開(kāi)其下的“Java 和進(jìn)程管理”,然后進(jìn)入“進(jìn)程定義”。
          進(jìn)入后找到“其它屬性”-->“Java 虛擬機(jī)”:
          將“Java 虛擬機(jī)”通用 JVM 參數(shù) 設(shè)置為:-Dfile.encoding=GBK

          weblogic

          【關(guān)鍵字】 Tomcat容器 中文 字符編碼 亂碼 Weblogic容器 服務(wù)器
          【正文】因?yàn)榫幋a方式的不同在使用Tomcat容器時(shí)會(huì)出現(xiàn)提交到Servlet的中文是亂碼的方式,而且Tomcat5.x對(duì)于POST和GET的方式處理似乎還有不同,POST方式是采用Filter的方式即可,怎樣能夠處理GET方式中文提交亂碼的問(wèn)題呢?
           可以采用配置服務(wù)器字符編碼的方法,具體操作如下:
          1、打開(kāi)Tomcat安裝目錄中的conf目錄
          2、修改server.xml中的connector一個(gè)子項(xiàng),具體可能類似如下:

          <Connector port="8001" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" debug="0" connectionTimeout="20000" disableUploadTimeout="true" />

          在其中添加URIEncoding="GBK" ,或者是其他的編碼方式,變成如下:

          <Connector port="8001" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" debug="0" connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="GBK" />

          3、停止Tomcat服務(wù),重新啟動(dòng)Tomcat服務(wù)即可。

          【另附Weblogic容器處理編碼的方式】

          處理Weblogic容器的編碼比較簡(jiǎn)單,只需要在站點(diǎn)的web.xml中配置一行如下的代碼即可。

          <context-param> <param-name>weblogic.httpd.inputCharset./*</param-name> <param-value>GBK</param-value> </context-param>

          【另附Tomcat處理POST提交亂碼的方式】

          1、首先在web.xml中配置過(guò)濾器

          <filter> <filter-name>SetCharacterEncodingFilter</filter-name> <filter-class> cn.cublog.jedliu.SetCharacterEncodingFilter </filter-class> <init-param> <param-name>encoding</param-name> <param-value>GBK</param-value> </init-param> </filter>


          <filter-mapping> <filter-name>SetCharacterEncodingFilter</filter-name> <url-pattern>*.do</url-pattern> </filter-mapping>

          2、在cn.cublog.jedliu包中SetCharacterEncodingFilter是用于來(lái)實(shí)現(xiàn)編碼的過(guò)濾器。

          public class SetCharacterEncodingFilter implements Filter {
              protected String encoding = null;
              protected FilterConfig filterConfig = null;
              protected boolean ignore = true;
              public void destroy() {
                  this.encoding = null;
                  this.filterConfig = null;
              }
              public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain)
              throws IOException, ServletException {
                  if (ignore || (request.getCharacterEncoding() == null)) {
                      String encoding = selectEncoding(request);
                      if (encoding != null)
                          request.setCharacterEncoding(encoding);
                  }
                  chain.doFilter(request, response);
              }

              public void init(FilterConfig filterConfig) throws ServletException {
               this.filterConfig = filterConfig;
                  this.encoding = filterConfig.getInitParameter("encoding");
                  String value = filterConfig.getInitParameter("ignore");
                  if (value == null)
                      this.ignore = true;
                  else if (value.equalsIgnoreCase("true"))
                      this.ignore = true;
                  else if (value.equalsIgnoreCase("yes"))
                      this.ignore = true;
                  else
                      this.ignore = false;
              }
              protected String selectEncoding(ServletRequest request) {
                  return (this.encoding);
              }
          }

          主站蜘蛛池模板: 无锡市| 荔波县| 琼海市| 柞水县| 彰化县| 中山市| 华亭县| 佳木斯市| 电白县| 星座| 安溪县| 搜索| 内黄县| 汝南县| 河北区| 肃宁县| 葫芦岛市| 体育| 富民县| 九江市| 阜宁县| 额敏县| 荥经县| 滕州市| 清苑县| 宾川县| 东乌珠穆沁旗| 甘肃省| 广德县| 定安县| 镇沅| 明水县| 涟源市| 华阴市| 沙河市| 柳河县| 土默特右旗| 龙里县| 洛隆县| 梅州市| 上思县|