云自無心水自閑

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

          在table中,如果表格內容為空,那么顯示的時候,表格的邊框會少掉一塊。在HTML的規范中,應該使用empty-cells: show這個style來解決,但是IE要到IE8之后才支持這個屬性。而FireFox2是已經支持了。

          那么在IE7之前的版本中要解決這個問題,好像是需要使用border-collapse:collapse;這個style.

          <table style="border-collapse:collapse">

          posted @ 2008-04-14 08:03 云自無心水自閑 閱讀(544) | 評論 (0)編輯 收藏


          首先,在web項目的頁面根目錄下建立目錄template
          然后創建目錄simple和xhtml,以上的目錄名是struts2缺省使用的,不同的主題使用相應的目錄。然后再創建一個components目錄,在這個目錄下,創建一個property.ftl。 最后的目錄結構如下:
          template/simple/components/property.ftl
          template/xhtml/components/property.ftl

          然后在property.ftl中可以使用FreeMarker來定義新的模板(FreeMarker的具體語法可以查看FreeMarker的官方網站,相當的詳細易懂):
          <#include "/${parameters.templateDir}/${parameters.theme}/controlheader.ftl" />
          <@s.if test="${parameters.value} == null || ${parameters.value} == '' ">&nbsp;</@s.if>
          <@s.else><@s.property value="${parameters.value}" /></@s.else>
          <#include "/${parameters.templateDir}/xhtml/controlfooter.ftl" />

          以上是一個我自定義的模板,檢測結果是否為空字符串,如果是空的話,就輸出一個&nbsp; 這樣在輸出結果時表格的邊框線就是完整的了。

          定義好之后,在jsp頁面中就可以這樣使用了:

          <s:component template="/components/property.ftl" theme="simple">
            <s:param name="value" value="%{'bookName'}"/>
          </s:component>

          第一行中的目錄名從自components開始,struts2會自動在template目錄下去尋找,如是主題是simple, 就在simple目錄下找。
          另外,param的語法要注意一下,%{}里面需要加一對引號

          posted @ 2008-04-11 16:09 云自無心水自閑 閱讀(7559) | 評論 (4)編輯 收藏

          在Struts2的官方網站上,有doubleselect的用法示例,但是那個例子比較簡單。
          <s:doubleselect label="doubleselect test2" name="menu" list="#{'fruit':'Nice Fruits', 'other':'Other Dishes'}" doubleName="dishes" doubleList="top == 'fruit' ? {'apple', 'orange'} : {'monkey', 'chicken'}" />
          上面的例子演示了doubleselect的基本用法,但是其list和doubleList都是固定的。尤其是doublelist的切換使用了3目運算符 ? :,并沒有太大的實際使用價值。在實際應用中,list往往是action返回的一個List<DataObject>,listKey和listValue來顯示第一級下拉框,doubleList往往是一個Map<Integer, List<DataObject>>,其中Map中的Key值是第一級下拉框的listKey。
          舉個例子:
          Data Object:
          public class Book {
                  
          private int id;
                  
          private String name;
                  
          private int categoryId;

                  
          // getter and setter..
          }


          public class Category {
                  
          private int id;
                  
          private String name;

                  
          // getter and setter..
          }


          JSP:
          <s:doubleselect list="categoryList" listKey="id" listValue="name"
             doubleName="bookId" doubleList="bookMap.get(top.id)" doubleListKey="id" doubleListValue="name" theme="simple"/>

          此處要注意的是top的用法,開始我以為top就是指代list的值,所以使用的是bookMap.get(top),但是二級下拉框一直是空白,后來我突然想到說不定top是一個Category實例呢,嘗試了一下top.id,果然成功了。

          Action:

          public class DemoAction {
                  
          private Map<Integer, List<Book>> bookMap;
                  
          private List<Category> categoryList;

                  
          public String execute() throws Exception {
                          categoryList 
          = new ArrayList<Cateogry>();

                          Category category;
                          category 
          = new Category();
                          category.setId(
          1);
                          category.setName(
          "Fiction");
                          categoryList.add(category);
                          category 
          = new Category();
                          category.setId(
          2);
                          category.setName(
          "Java");
                          categoryList.add(category);

           

                          bookMap 
          = new HashMap<Integer, List<Book>>();

                          List
          <Book> bookList = new ArrayList<Book>();
                          Book book;
                          book 
          = new Book();
                          book.setId(
          1);
                          book.setName(
          "Harry Porter");
                          book.setCategoryId(
          1);
                          bookList.add(book);

                          book 
          = new Book();
                          book.setId(
          2);
                          book.setName(
          "Nightmare");
                          book.setCategoryId(
          1);
                          bookList.add(book);

                          bookMap.put(
          1, bookList);

           

                          bookList 
          = new ArrayList<Book>();
                          book 
          = new Book();
                          book.setId(
          3);
                          book.setName(
          "Thinking in Java");
                          book.setCategoryId(
          2);
                          bookList.add(book);
                          book 
          = new Book();
                          book.setId(
          4);
                          book.setName(
          "Head First Design Patterns");
                          book.setCategoryId(
          2);
                          bookList.add(book);

                          bookMap.put(
          2, bookList);


                          
          return SUCCESS;
                  }

                  
          // getter and setter..
          }



          posted @ 2008-03-27 22:08 云自無心水自閑 閱讀(12158) | 評論 (16)編輯 收藏

          對于jsp頁面,為了防止頁面被服務器緩存、始終返回同樣的結果。

          通常的做法是在客戶端的url后面加上一個變化的參數,比如加一個當前時間。

          我現在使用的方法是在jsp頭部添加以下代碼:

          <%
              request.setAttribute("decorator", "none");
              response.setHeader("Cache-Control","no-cache"); //HTTP 1.1
              response.setHeader("Pragma","no-cache"); //HTTP 1.0
              response.setDateHeader ("Expires", 0); //prevents caching at the proxy server
          %>

          這樣如果有多個調用此頁面的鏈接就不需要一個一個全部添加參數了。

          posted @ 2008-03-18 14:06 云自無心水自閑 閱讀(4301) | 評論 (2)編輯 收藏

          在數據庫的設計中,字典項是經常使用的技巧。
          比如在一個圖書館系統中,書籍表(Book)會有一個分類字段,這時候我們一般會單獨建立一張分類表(Category),在書籍表只保存分類表的ID。
          在用戶界面上顯示書籍明細的時候,會要求顯示CategoryID在Category表中對應的名稱。
          這樣通常的做法是把Book和Category兩張表進行關聯。
          但在實際應用中,Category一般都是Cache在應用服務器端,再使用數據表的連接就不夠高效。
          我的做法是這樣的:在iBatis中使用SqlMap從表中將數據取出,此時不使用數據表的連接。
          package com.demo;
          public class Book {
                
          /* 省略了getter和setter方法 */
                  private int id;
                  
          private String name;
                  
          private int categoryId;
                  
          private String author;
          }


          package com.demo;

          public class Category {
                 
          private static Map<Integer, Category> cacheMap;

                  
          /* 省略了這兩個屬性的getter和setter方法 */

                  
          private int id;
                  
          private String name;
                 
          public static Category getCategory(int id) {
                       init();
                      
          return cacheMap.get(id);
                  }
           
                  
          public static Map<Integer, Category> getCategoryMap() {
                      init();
                     
          return cacheMap();
                  }

                 
          private init() {
                      
          if ( cacheMap != null ) return;

                      
          // the code to load category from datebase
                      
          // 在這里為了演示的需要,使用以下代碼

                       cacheMap 
          = new HashMap<Integer, Category>();
                       Category category 
          = new Category();
                       category.setId(
          1);
                       category.setName(
          "Fiction");
                       cacheMap.put(
          1, category);
           
                       category 
          = new Category();
                       category.setId(
          2);
                       category.setName(
          "Cartoon");
                  }
          }



          package com.demo;

          public class BookAction  {
                  
          /* 省略了屬性的getter和setter方法 */
                  Book book;

                  
          public String execute() {
                          book 
          = new Book();
                          book.setId(
          1);
                          book.setName(
          "Thinking in java");
                          book.setCategoryId(
          1);
                          bookList.add(book);
                          
                          
          return SUCCESS;                
                  }
          }

          JSP:
          <%@ 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">
              
          <tr>
               
          <td>
                
          <s:text name="page.label.userName" />
               
          </td>
               
          <td>
                
          <s:property value="book.name" />
               
          </td>
              
          </tr>
              
          <tr>
               
          <td>
                
          <s:text name="page.label.category" />
               
          </td>
               
          <td>
                 
          <s:property value="@com.demo.Category@getCategory(book.categoryId).getName()"/></td>
              
          </tr>
           
          </body>
          </html>



          posted @ 2008-03-17 12:09 云自無心水自閑 閱讀(5790) | 評論 (8)編輯 收藏

          2008年2月24日,Flex3正式發布了。分為標準版和Professional版。
          兩者差別不大,標準版沒有advanced datagrid和一些性能分析工具。(advanced datagrid)可是我期盼好久的功能了。

          另外,價錢也是相當的不菲。Standard is $249 US, Professional costs $699.
          此外,還可以選擇從flex2升級上去:升級到Standard的話,99美元,升級到professional要299美元。

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

          Tomcat在后臺重起后,所有的session失效。如果客戶端繼續點擊了一個菜單項,發出一個請求。會得到一個exception。

          這時候,可以定義一個名為:sessionTimeout的global results

          <result name="sessionTimeout">/WEB-INF/pages/session_timeout.jsp</result>

          這樣,所有Action的session timeout都會被定向到指定的頁面

          posted @ 2008-02-21 12:03 云自無心水自閑 閱讀(1960) | 評論 (2)編輯 收藏

          最近嘗試用extjs來展示樹狀菜單。著實花了一番功夫。樹狀菜單的菜單項需要動態加載,而目前版本的extjs中只支持JSON格式的數據。查了一些資 料,決定使用struts2的json-plugin。首先按照例子做了一個,但是結果就是不成功,界面上只出來了一個js中生成的root節點,不能加 載從后臺生成的數據。研究后發現是數據格式有問題。使用json-plugin生成的數據格式如下:
          {"cls":"folder","id":10,"leaf":false,"children":[{"cls":"file","id":11,"leaf":true,"children":null,"text":"S600"},{"cls":"file","id":12,"leaf":true,"children":null,"text":"SLK200"}],"text":"Benz"}
          而extjs需要的數據格式如下:
          [{"cls":"folder","id":10,"leaf":false,"children":[{"cls":"file","id":11,"leaf":true,"children":null,"text":"S600"},{"cls":"file","id":12,"leaf":true,"children":null,"text":"SLK200"}],"text":"Benz"}]
          區別很小,就只相差最外面的兩個方括號。但是少了這兩個方括號,在json中,含義迥然不同,前者表示一個對象,而后者表示一個數組。而extjs中 tree的dataloader需要的數據必須是一個數組。而這樣的數據格式是json-plugin自動生成的,無法改變。所以,我最后放棄了json -plugin,轉而使用json-lib來解決這個問題。
          1. 下載json-lib, http://json-lib.sourceforge.net/
          2. lib目錄下的jar文件清單:
          commons-beanutils-1.7.0.jar
          commons-collections-3.2.jar
          commons-digester-1.6.jar
          commons-lang-2.3.jar
          commons-logging-1.1.jar
          dom4j-1.6.1.jar
          ezmorph-1.0.4.jar
          freemarker-2.3.8.jar
          javassist-3.8.1.jar
          json-lib-2.2.1-jdk15.jar
          log4j-1.2.13.jar
          ognl-2.6.11.jar
          struts2-core-2.0.11.jar
          xml-apis-1.0.b2.jar
          xwork-2.0.4.jar


          首先配置web.xml
          <?xml version="1.0" encoding="UTF-8"?>
          <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
            xmlns:xsi
          ="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation
          ="http://java.sun.com/xml/ns/j2ee
              http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
          >
            
          <welcome-file-list>
              
          <welcome-file>index.jsp</welcome-file>
            
          </welcome-file-list>
            
          <filter>
              
          <filter-name>struts2</filter-name>
              
          <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
            
          </filter>

            
          <filter-mapping>
              
          <filter-name>struts2</filter-name>
              
          <url-pattern>/*</url-pattern>
            
          </filter-mapping>
          </web-app>

          然后是struts.xml
          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE struts PUBLIC
              "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
              "http://struts.apache.org/dtds/struts-2.0.dtd"
          >
             
          <struts>
              
          <constant name="struts.devMode" value="true"/>
              
          <constant name="struts.i18n.encoding" value="UTF-8"/>
              
          <package name="person" extends="struts-default">
                  
          <action name="menus" method="execute" class="com.lab.MenuAction">
                      
          <result>/menu.jsp</result>
                  
          </action>
              
          </package>
          </struts>

          3. 樹的節點模型(省略了getter,setter)
          public class Menu {
              
          private int id;
              
          private String text;
              
          private boolean leaf;
              
          private String cls;
              
          private List<Menu> children;
          }

          4. action
          package com.lab;

          import java.util.ArrayList;
          import java.util.List;

          import net.sf.json.JSONArray;

          public class MenuAction {
              
          private String menuString;
             
              
          private List<Menu> menus;
             
              
          public String execute() {

                  menus 
          = new ArrayList<Menu>();
                 
                  Menu benz 
          = new Menu();
                  benz.setText(
          "Benz");
                  benz.setCls(
          "folder");
                  benz.setLeaf(
          false);
                  benz.setId(
          10);
                  menus.add(benz);
                 
                  List
          <Menu> benzList = new ArrayList<Menu>();
                  benz.setChildren(benzList);
                 
                  Menu menu;
                  menu 
          = new Menu();
                  menu.setText(
          "S600");
                  menu.setCls(
          "file");
                  menu.setLeaf(
          true);
                  menu.setId(
          11);
                  benzList.add(menu);
                  menu 
          = new Menu();
                  menu.setText(
          "SLK200");
                  menu.setCls(
          "file");
                  menu.setLeaf(
          true);
                  menu.setId(
          12);
                  benzList.add(menu);
                 
                  Menu bmw 
          = new Menu();
                  bmw.setText(
          "BMW");
                  bmw.setCls(
          "folder");
                  bmw.setLeaf(
          false);
                  bmw.setId(
          20);
                  menus.add(bmw);
                 
                  List
          <Menu> bmwList = new ArrayList<Menu>();
                  bmw.setChildren(bmwList);
                 
                  menu 
          = new Menu();
                  menu.setText(
          "325i");
                  menu.setCls(
          "file");
                  menu.setLeaf(
          true);
                  menu.setId(
          21);
                  bmwList.add(menu);
                 
                  menu 
          = new Menu();
                  menu.setText(
          "X5");
                  menu.setCls(
          "file");
                  menu.setLeaf(
          true);
                  menu.setId(
          22);
                  bmwList.add(menu);
                 
                  JSONArray jsonObject 
          = JSONArray.fromObject(menus);
                  
          try {
                      menuString 
          = jsonObject.toString();
                  } 
          catch (Exception e) {
                      menuString 
          = "ss";
                  }

                  
          return "success";
              }

              
          public String getMenuString() {
                  
          return menuString;
              }

              
          public void setMenuString(String menuString) {
                  
          this.menuString = menuString;
              }
          }

          5. menu.jsp
          <%@ taglib prefix="s" uri="/struts-tags" %>
          <s:property value="menuString" escape="false"/>

          6. html頁面和js
          我使用的就是extjs的example中的reorder.html和reorder.js,更改了reorder.js中treeloader的dataurl: menus.action
          <html>
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
          <title>Reorder TreePanel</title>
          <link rel="stylesheet" type="text/css" href="extjs/resources/css/ext-all.css" />

              
          <!-- GC -->
               
          <!-- LIBS -->
               
          <script type="text/javascript" src="extjs/adapter/ext/ext-base.js"></script>
               
          <!-- ENDLIBS -->
           
              
          <script type="text/javascript" src="extjs/ext-all.js"></script>
          <script type="text/javascript" src="reorder.js"></script>

          <!-- Common Styles for the examples -->
          <link rel="stylesheet" type="text/css" href="extjs/resources/css/example.css" />
          </head>
          <body>
          <script type="text/javascript" src="../examples.js"></script><!-- EXAMPLES -->
          <h1>Drag and Drop ordering in a TreePanel</h1>
          <p>This example shows basic drag and drop node moving in a tree. In this implementation there are no restrictions and 
          anything can be dropped anywhere except appending to nodes marked 
          &quot;leaf&quot; (the files). <br></p>
          <p>Drag along the edge of the tree to trigger auto scrolling while performing a drag and drop.</p>
          <p>In order to demonstrate drag and drop insertion points, sorting was <b>not</b> enabled.</p>
          <p>The data for this tree is asynchronously loaded with a JSON TreeLoader.</p>
          <p>The js is not minified so it is readable. See <href="reorder.js">reorder.js</a>.</p>

          <div id="tree-div" style="overflow:auto; height:300px;width:250px;border:1px solid #c3daf9;"></div>

          </body>
          </html>



          js:
          /*
           * Ext JS Library 2.0.1
           * Copyright(c) 2006-2008, Ext JS, LLC.
           * licensing@extjs.com
           *
           * http://extjs.com/license
           
          */

          Ext.onReady(
          function(){
              
          // shorthand
              var Tree = Ext.tree;
             
              
          var tree = new Tree.TreePanel({
                  el:'tree
          -div',
                  autoScroll:
          true,
                  animate:
          true,
                  enableDD:
          true,
                  containerScroll: 
          true,
                  loader: 
          new Tree.TreeLoader({
                      dataUrl:'http:
          //localhost:8080/lab/menus.action'
                  })
              });

              
          // set the root node
              var root = new Tree.AsyncTreeNode({
                  text: 'Ext JS',
                  draggable:
          false,
                  id:'source'
              });
              tree.setRootNode(root);

              
          // render the tree
              tree.render();
              root.expand();
          });

          我已經上傳了完整的War文件(包含所有源代碼),見:Extjs Tree + JSON + Struts2 的所有示例源代碼和war文件下載

          posted @ 2008-02-19 09:27 云自無心水自閑 閱讀(43658) | 評論 (49)編輯 收藏

          Struts2中支持使用List在頁面和Action之間直接傳遞表格數據。下面是一個示例:
          public class Person {
          int id;
          String name;
          int age;
          float height;
          }

          這是一個POJO,getter和setting省略了。

          action中可以這樣使用:

          public class MyAction {
          public List getPeopleList() { … }
          public void setPeopleList( List peopleList ) { … }

          }
          在我們使用Person類之前,需要添加一個配置文件,MyAction-conversion.properties,把這個文件和MyAction放在一起。
          這個文件里只有一行內容:
          Element_peopleList=Person
          前綴Element_是一個常量,表明等號左邊的表達式中跟在這個常量后面的是Action類中一個List類型的字段名。
          等號右邊的表達式是全類名(包含package)
          下面是一個頁面的代碼片段:
          <s:form action="update" method="post" >
          <s:iterator value="peopleList" status="stat">
          <s:hidden
          name="peopleList[%{#stat.index}].id"
          value
          ="%{peopleList[#stat.index].id}"/>
          <s:textfield label="Name"
          name
          ="peopleList[%{#stat.index}].name"
          value
          ="%{peopleList[#stat.index].name}"/>
          <s:textfield label="Age"
          name
          ="peopleList[%{#stat.index}].age"
          value
          ="%{peopleList[#stat.index].age}" />
          <s:textfield label="Height"
          name
          ="peopleList[%{#stat.index}].height"
          value
          ="%{peopleList[#stat.index].height}"/>
          <br/>
          </s:iterator>
          <s:submit value="Update"/>
          </s:form>


          使用這段代碼,Struts2會創建一個Person類的ArrayList,并且用setPersonList這個方法把頁面表格中的值傳遞回Action。
          如果你是想從用戶界面中動態創建列表值,需要允許Struts2給列表中類的實例。那么在配置文件MyAction-conversion.properties中添加一行:
          CreateIfNull_peopleList = true

          posted @ 2008-02-12 21:06 云自無心水自閑 閱讀(3276) | 評論 (2)編輯 收藏

               摘要: Struts2和Struts相比,一個重大改進就是支持Ajax。 本文主要看一下Struts2中的Div是如何用來輸出Ajax結果,其中主要使用了Dojo。 首先,我們先創建一個簡單的用例,在這個用例中,將在屏幕上顯示一個用戶列表,點擊列表中的userid時,列表的下方將顯示用戶的詳細信息,顯示用戶詳細信息的這個步驟我們將使用Ajax。 一、創建web.xml <?xml v...  閱讀全文

          posted @ 2008-02-10 20:56 云自無心水自閑 閱讀(12249) | 評論 (8)編輯 收藏

          僅列出標題
          共29頁: First 上一頁 13 14 15 16 17 18 19 20 21 下一頁 Last 
          主站蜘蛛池模板: 郯城县| 阳山县| 凤庆县| 新蔡县| 望奎县| 腾冲县| 扎囊县| 汉寿县| 临江市| 阿合奇县| 高平市| 宜川县| 衡南县| 三江| 河南省| 驻马店市| 房产| 故城县| 抚远县| 麻江县| 平乡县| 嘉义市| 沛县| 阳山县| 榆树市| 安达市| 措美县| 大新县| 龙泉市| 原平市| 沿河| 诸城市| 娱乐| 姚安县| 卢氏县| 岳普湖县| 民丰县| 旌德县| 陇川县| 历史| 乌海市|