云自無心水自閑

          天平山上白云泉,云自無心水自閑。何必奔沖山下去,更添波浪向人間!
          posts - 288, comments - 524, trackbacks - 0, articles - 6
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

           

          public String getFormatedDateString(int timeZoneOffset)
                  
          if (timeZoneOffset > 13 || timeZoneOffset < -12{
                      logger.error(
          "Configuration item TimeZone " + timeZoneString + " is invalid.");
                      timeZoneOffset 
          = 0;
                  }

                  TimeZone timeZone;

                  String[] ids 
          = TimeZone.getAvailableIDs(timeZoneOffset * 60 * 60 * 1000);
                  
          if (ids.length == 0{
                      
          // if no ids were returned, something is wrong. use default TimeZone
                      timeZone = TimeZone.getDefault();
                  }
           else {
                      timeZone 
          = new SimpleTimeZone(timeZoneOffset * 60 * 60 * 1000, ids[0]);
                  }


                  SimpleDateFormat sdf 
          = new SimpleDateFormat("yyyyMMddHHmmss");
                  sdf.setTimeZone(timeZone);

                  
          return sdf.format(newDate);
              }

          其中timeZoneOffset就是時區,比如東八區,就傳入8,西二區就傳入-2

          新的方法,使用指定的TimeZone ID來獲得TimeZone,這樣更精確,因為有一些城市,雖然時區。比如:悉尼和布里斯班,都是東10區,但是悉尼實行夏令時,所以夏天的時候,悉尼要比布里斯班早1小時。

                  TimeZone timeZoneSYD = TimeZone.getTimeZone("Australia/Sydney");
                  TimeZone timeZoneBNE = TimeZone.getTimeZone("Australia/Brisbane");
                 
                  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                  sdf.setTimeZone(timeZoneSYD);
                  Date date = new Date();
                  System.out.println(sdf.format(date));
                 
                  sdf.setTimeZone(timeZoneBNE);
                  System.out.println(sdf.format(date));

          其中TimeZone的ID列表,可以使用函數
              public static String[] TimeZone.getAvailableIDs();
          來獲得

           


          posted @ 2008-02-07 07:04 云自無心水自閑 閱讀(1569) | 評論 (0)編輯 收藏


          File[] filesWanted = dir.listFiles(
              new FilenameFilter() {
                  public boolean accept(File dir, String name) {
                      return name.endsWith(".html") || name.endsWith(".htm");
                  }
              });

          posted @ 2008-02-06 09:43 云自無心水自閑 閱讀(449) | 評論 (0)編輯 收藏

          <%@ taglib prefix="s" uri="/struts-tags"%>

          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html>
              
          <head>
                  
          <s:head />
              
          </head>

              
          <body>

                  
          <table border="1">
                      
          <s:iterator value="dataMap.keySet()" id="class">
                          
          <s:iterator value="dataMap.get(#class).keySet()" id="group">
                          
          <tr>
                              
          <td><s:property value="group"/></td>
                              
          <s:iterator value="dataMap.get(#class).get(#group).values()" id="name">
                                  
          <td><s:property value="name"/></td>
                              
          </s:iterator>
                          
          </tr>
                          
          </s:iterator>
                      
          </s:iterator>
                  
          </table>
              
          </body>
          </html>

          posted @ 2008-01-25 13:16 云自無心水自閑 閱讀(15312) | 評論 (10)編輯 收藏

          在Struts2中,radio標簽可以使用一個list來輸出一組radio按鈕,
                  <s:radio name="sex" list="#{'male','female'}" label="%{getText('app.label.sex')}" />
          但是如何設置其中一個被默認選中。

          查閱了struts2的文檔,發現radio標簽有一個value屬性是用于對radio的進行預選的: http://struts.apache.org/2.x/docs/radio.html
          value: Preset the value of input element.
          于是,進行了試驗,<s:radio name="sex" list="#{'male','female'}" label="%{getText('app.label.sex')}" value="male" />
          結果失敗了。male的值并沒有被選中,經過反復研究,終于得到了正確的結果:
          <s:radio name="sex" list="#{'male','female'}" label="%{getText('app.label.sex')}" value="'male'" />
          看到其中的區別了嗎,就是多了兩個單引號。
          我認為這是因為value屬性的特性引起的。如果male沒有加引號,那么struts2會去值的堆棧中尋找變量名為male的值,結果找不到。
          加上單引號后,struts2(應該是ognl)把'male'認為是一個簡單的字符串。

          這樣,radio就能夠正確地匹配到值,使指定的值默認被選中

          posted @ 2008-01-24 22:06 云自無心水自閑 閱讀(7065) | 評論 (7)編輯 收藏


          在iBatis中,對于in子句的標準做法是采用動態sql來解決的。具體方法大致是:Java代碼傳入一個List或者數組,然后在sqlMapConfig映射中使用iterate循環取這個變量,動態地生成sql語句。
          這個標準解法的缺點是,使用起來比較麻煩
          1. 需要在sqlMapConfig中使用動態語句
          2. 需要傳入一個Iterable的變量
          對于這個問題,我使用了一個偷懶的辦法,就是使用$標記。
          在iBatis中,普通的變量,比如:v,是使用#號,在這個例子中,就是:#v#。
          這樣,iBatis會使用prepareStatement,并對變量進行變量綁定。
          而$符號是簡單替代的用法,在數據庫的執行效率上要比前一種差。但優點就是簡單方便。
          比如:
          SELECT * FROM  emp WHERE emp_no in ($empString$);
          而empString的值就是1, 2, 3. 在Log中,可以看到,Sql語句就是:SELECT * FROM emp WHERE emp_no in (1,2,3)

          posted @ 2008-01-02 20:48 云自無心水自閑 閱讀(8585) | 評論 (3)編輯 收藏

          dhtmlXTree的節點定義可以從服務器生成的Xml中動態加載,我目前使用的是Struts2. 因此,我的做法是,在Javascript生成dhtmlxTree的時候,請求一個加載Struts2的action,Struts2的Action進行用戶權限認證,動態生成菜單的Xml字符串。而在jsp中只輸出這一個字符串。
          開始進行的都比較順利,但是客戶端和服務端一連起來就出問題,頁面在加載Xml的時候總是彈出一個對話框,說是加載的xml格式不正確。但是我在IE中直接輸入Action,頁面顯示的結果十分正確,沒有問題。后來,無意中我查看了返回xml頁面的源文件,這才發現了問題。
          原來,xml的<和>全都被轉換成&lt;和&gt;了。
          我一開始是想在后臺考慮如何對字符串進行轉換,后來在查struts2文檔的時候發現,<s:property有一個escape屬性,可以完美地解決這個問題,<s:property value="menuXmlString" escape="false"/>

          例子已經整理出來了:http://www.aygfsteel.com/usherlight/archive/2008/08/07/220756.html

          posted @ 2007-12-14 21:14 云自無心水自閑 閱讀(5439) | 評論 (6)編輯 收藏

          最近在網上搜索網頁上的樹形菜單的開源項目,發現有一個dhtmlx公司做的挺好的。
          dhtmlx.com
          其組件包括:
           dhtmlxTree    dhtmlxTabbar
           dhtmlxGrid    dhtmlxCombo
           dhtmlxTreeGrid   dhtmlxVault
           dhtmlxMenu   dhtmlxToolbar

          界面做得相當漂亮,在Live Demo中有3種Theme可以選擇。
          功能也相當出色:可以拖放樹中的節點、動態改變節點的Icon,定義節點事件。
          可以動態地從Xml中加載菜單的結構定義
          節點可編輯
          可使用鍵盤瀏覽樹
          樹節點可帶checkbox

          • Multibrowser/Multiplatform support
          • XHTML compatible
          • Loading from XML or Javascript
          • Async mode loading support
          • Editable Items
          • Keyboard navigation
          • Multiselect
          • Drag-&-drop (within one tree, between trees)
          • Right-to-left languages support (RTL)
          • Full controll with JavaScript API
          • Dynamic Loading for big trees
          • Distributed Loading for big levels
          • Smart XML Parsing for big trees
          • Serialization to XML


        1. Customizable drag-&-drop to/from dhtmlxGrid
        2. Copy with drag-n-drop
        3. Drop-between/drop-inside
        4. Checkboxes (two/three states, disabled/hidden, radio)
        5. Customizable View
        6. Unlimited User-data for nodes
        7. ASP.NET custom server control
        8. JSP custom tag
        9. Macromedia Cold Fusion support
        10. Detailed documentation
        11. posted @ 2007-12-11 19:38 云自無心水自閑 閱讀(2687) | 評論 (4)編輯 收藏


          網上介紹使用zipInStream和zipOutStream對文件或者文件夾進行壓縮和解壓縮的文章比較多。
          但是這次項目中需要對byte[]進行壓縮,然后decode,通過http發送到服務端。

          最簡單的方法,當然是把byte[]寫到文件里,然后根據網上已有的文章,生成fileInputStream,構造zipInStream。
          但是這個做法有著明顯的問題,需要操作IO,在效率上不可取。

          下面是利用ByteArrayOutStream來完成壓縮和解壓縮的代碼。



             
          /**
               * Answer a byte array compressed in the Zip format from bytes.
               * 
               * 
          @param bytes
               *            a byte array
               * 
          @param aName
               *            a String the represents a file name
               * 
          @return byte[] compressed bytes
               * 
          @throws IOException
               
          */
              
          public static byte[] zipBytes(byte[] bytes) throws IOException {
                  ByteArrayOutputStream tempOStream 
          = null;
                  BufferedOutputStream tempBOStream 
          = null;
                  ZipOutputStream tempZStream 
          = null;
                  ZipEntry tempEntry 
          = null;
                  
          byte[] tempBytes = null;

                  tempOStream 
          = new ByteArrayOutputStream(bytes.length);
                  tempBOStream 
          = new BufferedOutputStream(tempOStream);
                  tempZStream 
          = new ZipOutputStream(tempBOStream);
                  tempEntry 
          = new ZipEntry(String.valueOf(bytes.length));
                  tempEntry.setMethod(ZipEntry.DEFLATED);
                  tempEntry.setSize((
          long) bytes.length);
                  
                  tempZStream.putNextEntry(tempEntry);
                  tempZStream.write(bytes, 
          0, bytes.length);
                  tempZStream.flush();
                  tempBOStream.flush();
                  tempOStream.flush();
                  tempZStream.close();
                  tempBytes 
          = tempOStream.toByteArray();
                  tempOStream.close();
                  tempBOStream.close();
                  
          return tempBytes;
              }


              
          /**
               * Answer a byte array that has been decompressed from the Zip format.
               * 
               * 
          @param bytes
               *            a byte array of compressed bytes
               * 
          @return byte[] uncompressed bytes
               * 
          @throws IOException
               
          */
              
          public static void unzipBytes(byte[] bytes, OutputStream os) throws IOException {
                  ByteArrayInputStream tempIStream 
          = null;
                  BufferedInputStream tempBIStream 
          = null;
                  ZipInputStream tempZIStream 
          = null;
                  ZipEntry tempEntry 
          = null;
                  
          long tempDecompressedSize = -1;
                  
          byte[] tempUncompressedBuf = null;

                  tempIStream 
          = new ByteArrayInputStream(bytes, 0, bytes.length);
                  tempBIStream 
          = new BufferedInputStream(tempIStream);
                  tempZIStream 
          = new ZipInputStream(tempBIStream);
                  tempEntry 
          = tempZIStream.getNextEntry();
                  
                  
          if (tempEntry != null) {
                      tempDecompressedSize 
          = tempEntry.getCompressedSize();
                      
          if (tempDecompressedSize < 0) {
                          tempDecompressedSize 
          = Long.parseLong(tempEntry.getName());
                      }

                      
          int size = (int)tempDecompressedSize;
                      tempUncompressedBuf 
          = new byte[size];
                      
          int num = 0, count = 0;
                      
          while ( true ) {
                          count 
          = tempZIStream.read(tempUncompressedBuf, 0, size - num );
                          num 
          += count;
                          os.write( tempUncompressedBuf, 
          0, count );
                          os.flush();
                          
          if ( num >= size ) break;
                      }
                  }
                  tempZIStream.close();
              }

          posted @ 2007-09-26 10:31 云自無心水自閑 閱讀(4919) | 評論 (2)編輯 收藏

          1. 先寫Controller
          2. Controller將業務邏輯委派給Service完成
          3. Service返回一個Domain Object Model
          4. 將Domail Object Model封裝成ModelAndView作為Controller的返回結果,并賦予View的名稱。
          5. InternalResourceViewResolver根據View名稱取出對應的Jsp文件,創建一個包含前綴和后綴的真正的路徑
          6.  這些定義在spring-servlet.xml文件中
          7. 配置文件:首先要在web.xml中配置ContextLoaderListener,介紹這個的文章非常多
          <listener>
              <listener-class>
                  org.springframework.web.context.ContextLoaderListener
              </listener-class>
          </listener>
          8. 在web.xml中加入DispatherServlet的配置
          <servlet>
          <servlet-name>spring</servlet-name>
          <servlet-class>
          org.springframework.web.servlet.DispatcherServlet
          </servlet-class>
          </servlet>
          <servlet-mapping>
          <servlet-name>spring</servlet-name>
          <url-pattern>/app/*</url-pattern>
          </servlet-mapping>
          9. spring會根據這個servlet的名字(在這里是spring)自動尋找  <名字>-servlet.xml(這里將會是:spring-servlet.xml)
          10. 在spring-servlet.xml中,將service注射給controller

          posted @ 2007-09-22 21:28 云自無心水自閑 閱讀(1748) | 評論 (4)編輯 收藏

          On Sep 4, Matt Raible annouced the release of AppFuse 2.0RC1.
          On Sep 7, Matt Raible uploaded a PDF to appfuse.dev.java.net contains the relevant pages from the wiki that help you develop with AppFuse 2.0.
          And yesterday, Matt Raible uploaded a big package to appfuse.dev.java.net. It included all the dependencies of the appfuse2.0 RC1 needed.

          good job, Matt.

          posted @ 2007-09-17 12:42 云自無心水自閑 閱讀(562) | 評論 (0)編輯 收藏

          僅列出標題
          共29頁: First 上一頁 14 15 16 17 18 19 20 21 22 下一頁 Last 
          主站蜘蛛池模板: 运城市| 朝阳县| 商丘市| 桂林市| 昌邑市| 重庆市| 肃宁县| 茂名市| 昭通市| 福安市| 皮山县| 沁源县| 武义县| 宁波市| 余江县| 三穗县| 静宁县| 三江| 柞水县| 旬阳县| 增城市| 九江县| 松原市| 抚松县| 葵青区| 武城县| 康马县| 庆云县| 田阳县| 富顺县| 永丰县| 庆阳市| 徐汇区| 麻江县| 淳安县| 定陶县| 广南县| 巴青县| 革吉县| 宜兰县| 读书|