隨筆-10  評論-11  文章-20  trackbacks-0
            2011年3月6日
          使用w3m上網方法:
             
             第一步,需要安裝一個名為w3m的軟件工具,打開終端,輸入如下命令

            sudo apt-get install w3m w3m-img -y

            第二步,安裝好w3m之后,在終端里面啟動w3m,打開一個網址,比如w3m www.baidu.com 。現在是不是看到了只有在瀏覽器下面才能看到的百度首頁呢?

            如果您的終端不顯示中文請。安裝zhcon。安裝命令:

            sudo apt-get install zhcon -y

            好了,下面給出w3m的相關幫助文件。

            與vim常用命令vim常用命令集相似,h,j,k,l 可以分別用來做移動鍵,分別是左,下,上,右。如果你熟習vim操作的話這會是很方便的功能。 < 和 > 用來左右滾屏。

            按q就會提示你退出!

            在需要輸入的文本框內按回車,下面就會出現TEXT: ,這時你可以輸入你要搜索的文字。再按回車就是返回給文本框。這時再把光標移到”百度搜索”的那個按鍵,回車,就可以開始搜索了!

            使用 U 來重新輸入需要打開的網址。

            B 返回前一個頁面。

            多標簽操作!

            可以使用 T 按鍵來打開一個新標簽。

            在多個標簽內切換呢? 使用 { 和 }就可以了!

            使用 ESC-t 的話會打開標簽的菜單讓你選擇,功能類似系統中的alt-tab功能。

            C-q用于關于當前標簽頁。

            書簽

            C-a 添加書簽

            C-b 查看書簽

            查找

            / 向后查找當前頁面

            ? 向前查找當前頁面

            n 查找下一個已查找過的關鍵字

            N 向前查找已查找過的關鍵字

            幫助

            H 以上的選項在這里都可以看到!
          posted @ 2011-09-23 17:00 Soap MacTavish 閱讀(10367) | 評論 (1)編輯 收藏
             在使用SWT構建應用程序時,理解系統底層讀取和調度平臺GUI事件的線程模型是非常重要的,UI線程的實現方式會影響到在應用程序中使用 Java 線程時必須遵守的規則。

           

          本地事件調度

                  對于任何的GUI應用程序,不管所使用的是哪一種編程語言和UI工具包,背后的運行機制都是操作系統探測GUI事件并把它們放到應用程序的事件隊列中去。這種機制在不同的操作系統平臺中大同小異。當用戶點擊鼠標、鍵入字符、或者使窗口獲得焦點,操作系統就會生成應用程序的GUI事件,例如鼠標點擊、鍵盤輸入、或窗口重繪事件。操作系統決定哪一個窗口和應用程序應該接收用戶觸發的每一個事件并把它放入應用程序的事件隊列中。

           

                  任何基于窗口的GUI應用程序的底層實現結構都是一個事件循環(event loop),應用程序初始化并運行一個循環(loop),用于從事件隊列中讀取GUI事件,并作出相應的反應。處理事件的工作必須迅速完成,以保證GUI應用程序能夠對用戶作出快速反應。

           

                  UI事件觸發的耗時較長的操作應該在一個單獨的線程中執行,這樣才能讓事件循環主線程能夠快速返回,獲取應用程序事件隊列中的下一個事件。但是,在非UI線程中訪問圖形界面部件和平臺API 必須通過鎖和串行化的機制(locking and serialization)來實現。違反這個規則的應用程序會引起系統調用失敗,更嚴重的是鎖住整個GUI系統,使GUI失去反應。

           

          SWT UI 線程

                  SWT 遵循系統平臺所直接支持的這種線程模型,應用程序在它的主線程中運行事件循環(event loop),并在主線程中直接調度線程。UI線程就是Display 對象被創建的線程,所有其他的圖形部件都必須在這個UI線程中創建。

                  既然所有的處理事件的代碼是在應用程序的UI線程中觸發的,那么處理事件的程序代碼就能夠不需要任何特殊方法自由訪問和調用圖形部件。不過,在處理長耗時的事件操作時,需要使用多線程來實現應用程序的功能。

           

          注:在非UI線程中調用任何必須在UI線程調用的程序,SWT將會觸發一個 SWTException 異常。

           

          SWT 應用程序的主線程,包括事件循環,其代碼結構如下:

          Java代碼 復制代碼 收藏代碼
          1. public static void main (String [] args) {   
          2.    Display display = new Display ();   
          3.    Shell shell = new Shell (display);   
          4.    shell.open ();   
          5.    // start the event loop. We stop when the user has done   
          6.    // something to dispose our window.   
          7.    while (!shell.isDisposed ()) {   
          8.       if (!display.readAndDispatch ())   
          9.          display.sleep ();   
          10.    }   
          11.    display.dispose ();   
          12. }  

                  創建圖形部件和打開shell 窗口之后,程序讀取和分發來自操作系統事件隊列的事件,直到shell 窗口被銷毀。如果在隊列中不存在有效事件,display 進入睡眠狀態,把運行機會交給其他程序。

           

                  SWT 提供了在后臺主線程中調用圖形部件代碼的訪問方法。

           

          運行非UI線程

                   在非UI 線程中不能直接調用UI 代碼,必須提供一個 Runnable對象,在Runable中調用UI代碼。Display 類中的syncExec(Runnable) 和 asyncExec(Runnable) 方法用于在事件循環運行期間,在UI 線程中運行這些Runnable對象。

          • syncExec(Runnable)  當非UI 線程中的程序代碼依賴于UI 代碼的返回值,或者為了確保在返回到主線程之前Runnable 必須執行完成時,應該使用這個方法。SWT 將會阻塞調用線程,直到在應用程序的UI 線程中運行的這個Runnable運行結束為止。例如,一個后臺線程需要基于一個窗口的當前尺寸進行某種計算,就會需要同步地運行獲取窗口尺寸的代碼,然后繼續其后面的計算。
          • asyncExec(Runnable)  當程序需要執行一些UI 操作,但在繼續執行之前不依賴這些操作必須完成的時候,應該使用這個方法。例如,后臺主線程更新進度條,或者重繪一個窗口,它可以異步地發出更新或重繪的請求,并接著繼續后面的處理,在這種情況下,后臺主線線程的運行時間和Runnable的運行沒有必然的關系。
          下面的代碼片段演示了使用這兩個方法的方式:
          Java代碼 復制代碼 收藏代碼
          1. // do time-intensive computations   
          2. ...   
          3. // now update the UI. We don't depend on the result,   
          4. // so use async.   
          5. display.asyncExec (new Runnable () {   
          6.    public void run () {   
          7.       if (!myWindow.isDisposed())   
          8.          myWindow.redraw ();   
          9.    }   
          10. });   
          11. // now do more computations   
          12. ...  
                  在使用asyncExec 的時候,在runnable 中檢查圖形部件是否被銷毀是一個好的習慣做法,在調用asyncExec和Runnable執行期間主線程中有可能會發生其他的事情,不能保證runnable執行時圖形部件當前處于什么狀態。

          工作臺(Workbench)和多線程

                  實現SWT應用程序的多線程規則非常明確,你可以控制事件循環的初始化,在應用程序中使用多線程解決復雜問題。

           

                  向工作臺添加插件時的工作機制要父子一些,下面是使用平臺(workbench platform)UI 類的一些“規約”(Rules of engagement),隨著eclipse 的不斷發布,可能會出現一些例外:

          • 通常,任何添加到平臺中的工作臺(workbench) UI 擴展都是在工作臺的UI 主線程中執行的,除非是明確地把它們添加線程中或者是后臺作業(background job)中,例如后臺作業進度條。
          • 如果從工作臺接收到一個事件,不能保證它是在UI線程中執行的,查閱定義了監聽器或事件的類的java文檔,如果沒有特別說明使用線程,這個類就是一個UI 相關類,可以在工作臺主線程中獲得和運行。
          • 同樣,除非是文檔明確說明,平臺UI庫不能視作是線程安全的。請注意,大部分平臺UI類是在觸發事件的調用線程中運行監聽器的,平臺和JFace API調用并不檢查是在UI 線程中執行的,這意味著如果在非UI 線程中調用一個能夠觸發事件的方法,可能會引入問題。從非UI 線程中調用SWT的API,SWT會拋出 SWTException 異常。通常,除非文檔中明確規定,避免在別的線程中調用平臺UI 代碼。
          • 如果你的插件使用多線程或工作臺作業(workbench job),必須使用 Display 類的asyncExec(Runnable) 或 syncExec(Runnable) 方法,類調用任何的工作臺(workbench)、JFace或SWT 的應用程序接口(API),除非是API明確說明是可以直接調用的。
          • 如果在插件中使用 JFace的IRunnableContext 接口 調用進度監視器(progress monitor),以運行一個操作,IRunnableContext 提供了一個參數來確定是不是在一個新的線程中運行操作。
          附:
          參考:http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse.platform.doc.isv/guide/swt_threading.htm
          posted @ 2011-04-17 21:58 Soap MacTavish 閱讀(1488) | 評論 (0)編輯 收藏
          一.概述
          如果你是SWT/Jface的初學者,那么本片所描述的問題很可能是你已經碰到或者將要碰到的。
          這是關于多線程開辟的問題,比較常見。

          二.問題:
          當你在GUI主線程開辟一個新線程,進行其它操作,并且需要更新UI控件,意外發
          生了:
          Exception in thread "Thread-1" java.lang.NullPointerException。

          三。解決方案
          使用:Display#asyncExec(Runnable)或者Display#syncExec(Runnable)。
          把你的UI更新操作另開一個線程,比如:
          Display.getCurrent().asyncExec(new Runnable() {
              public void run() {
                  button.setText("update");
              }
          };


          如果其它數據處理操作耗時不長,可以把這部分代碼放入到與UI更新同一個線程,
          Display.getCurrent().asyncExec(new Runnable() {
              public void run() {
                  //Do business action
                  button.setText("update");
              }
          };

          如果其它數據處理耗時長,那么為了不讓UI響應遲鈍,應該把邏輯操作和UI更新分開:
          Thread t = new Thread(new Runnable() {
              public void run() {
                  //Do business action
                  Display.getCurrent().asyncExec(new Runnable() {
                      public void run() {
                          button.setText("update");
                      }
                  };
                 
              }

          }
          posted @ 2011-04-17 21:57 Soap MacTavish 閱讀(413) | 評論 (0)編輯 收藏

          要在后臺線程里對前臺界面組件進行訪問.

          解決方法是使用Display對象,Display對象主要負責管理事件循環和控制UI線程和其它線程之間的通信.
          Display.getDefault().asyncExec(new Runnable(){
              public void run(){
              //對前臺界面進行操作
              }
          });
          1. package com.tr069im.ui;  
          2.  
          3. import org.eclipse.swt.SWT;  
          4. import org.eclipse.swt.layout.GridData;  
          5. import org.eclipse.swt.widgets.Button;  
          6. import org.eclipse.swt.widgets.Display;  
          7. import org.eclipse.swt.widgets.MessageBox;  
          8. import org.eclipse.swt.widgets.ProgressBar;  
          9. import org.eclipse.swt.widgets.Shell;  
          10.  
          11. /**  
          12.  * 此類通過實現SWT滾動條,說明多線程問題 .解決了后臺線程訪問前臺界面的問題   
          13.  * @author llwbrothers  
          14.  */ 
          15. public class Login implements Runnable {  
          16.     private static Shell shell;  
          17.     private String loginResponse = "right";  
          18.     private static boolean flag = false;  
          19.  
          20.     public Login() {  
          21.  
          22.     }  
          23.  
          24.     public static void main(String[] args) {  
          25.           
          26.         final Display display = Display.getDefault();  
          27.         shell = new Shell(SWT.MIN);  
          28.         shell.setSize(290520);  
          29.         shell.setLocation(3005);  
          30.         shell.setText("SWT多線程");  
          31.  
          32.         // 添加平滑的進度條  
          33.         final ProgressBar pb1 = new ProgressBar(shell, SWT.HORIZONTAL  
          34.                 | SWT.SMOOTH);  
          35.         pb1.setBounds(7228216020);  
          36.         pb1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));  
          37.         // 顯示進度條的最小值  
          38.         pb1.setMinimum(0);  
          39.         // 設置進度條的最大值  
          40.         pb1.setMaximum(30);  
          41.  
          42.         // 登錄按鈕  
          43.         final Button btnLogin = new Button(shell, SWT.FLAT | SWT.PUSH);  
          44.         btnLogin.setBounds(8036311136);  
          45.         btnLogin.setText("取消");  
          46.  
          47.         shell.open();  
          48.         // 異步線程處理其他數據  
          49.         display.asyncExec(new Runnable() {  
          50.             public void run() {  
          51.                 Login pl = new Login();  
          52.                 Thread t = new Thread(pl);  
          53.                 t.start();  
          54.             }  
          55.         });  
          56.         // 添加線程,在線程中處理長時間的任務,并最終反映在平滑進度條上  
          57.         Runnable runnable = new Runnable() {  
          58.             public void run() {  
          59.                 for (int i = 0; i < 30; i++) {  
          60.                     try {  
          61.                         Thread.sleep(100);  
          62.                     } catch (InterruptedException e) {  
          63.                     }  
          64.                     display.asyncExec(new Runnable() {  
          65.                         public void run() {  
          66.                             if (pb1.isDisposed())  
          67.                                 return;  
          68.                             // 進度條遞增  
          69.                             pb1.setSelection(pb1.getSelection() + 1);  
          70.                         }  
          71.                     });  
          72.                     if (flag) { // 作為標志,使滾動條起作用  
          73.                         break;  
          74.                     }  
          75.                     if (i == 29) {  
          76.                         open();  
          77.                     }  
          78.                 }  
          79.             }  
          80.         };// 啟動這個線程  
          81.         new Thread(runnable).start();  
          82.  
          83.         while (!shell.isDisposed()) { // 如果主窗體沒有關閉  
          84.             if (!display.readAndDispatch()) { // 如果display不忙  
          85.                 display.sleep(); // 休眠  
          86.             }  
          87.         }  
          88.         display.dispose(); // 銷毀display  
          89.     }  
          90.  
          91.     public void run() {  
          92.         try {  
          93.             // 收到驗證信息后進行  
          94.             if (loginResponse.equals("right")) {  
          95.                 // 后臺線程訪問前臺界面  
          96.                 Display.getDefault().asyncExec(new Runnable() {  
          97.                     public void run() {  
          98.                         flag = true;  
          99.                         Shell shell = new Shell(SWT.MIN);  
          100.                         MessageBox messageBox = new MessageBox(shell,  
          101.                                 SWT.ICON_WARNING);  
          102.                         messageBox.setMessage("用戶名或密碼錯誤!");  
          103.                         messageBox.open();  
          104.                     }  
          105.                 });  
          106.  
          107.             } else {      
          108.             }  
          109.         } catch (Exception ee) {  
          110.         }  
          111.     }  
          112.  
          113.     public static void open() {  
          114.         Display.getDefault().asyncExec(new Runnable() {  
          115.             public void run() {  
          116.                 // 進度條進行完之后,執行的程序  
          117.             }  
          118.         });  
          119.     }  
          120. }  
          posted @ 2011-04-17 21:57 Soap MacTavish 閱讀(1457) | 評論 (0)編輯 收藏
          S2SH框架集成步驟:

          1、框架依賴jar文件
          (這些jar包可以去官網自己下載,地址不在連接)

          1.1 sping     所依賴工程 spring-framework-2.5.6
               dist\spring.jar          
               lib\cglib\cglib-nodep-2.1_3.jar        
               lib\jakarta-commons\commons-logging.jar        
               lib\aspectj\aspectjweaver.jar和aspectjrt.jar      lib\j2ee\common-annotations.jar

          1.2 hibernate   所依賴工程 hibernate-distribution-3.3.2.GA
               hibernate3.jar
               lib\required\jta-1.1.jar        javassist-3.9.0.GA.jar        dom4j-1.6.1.jar        commons-collections-3.1.jar        antlr-2.7.6.jar        slf4j-api-1.5.8.jar
               lib\bytecode\cglib\cglib-2.2.jar
               [二級緩存可選]lib\optional\oscache\oscache-2.1.jar        同時需要把\project\etc\oscache.properties 拷貝到src 下
               [二級緩存可選]lib\optional\ehcache\ehcache-1.2.3.jar  同時需要把\project\etc\ehcache.xml
               [二級緩存可選]lib\optional\c3p0\    配置c3p0數據庫連接池的使用 作用等同于apache的dbcp

               *使用hibernate注解:hibernate-annotations-3.4.0.GA\
               hibernate-annotations.jar
               lib\hibernate-commons-annotations.jar
               lib\ejb3-persistence.jar

               *若使用slf的日志還需要:slf4j-1.5.8
               slf4j-nop-1.5.8.jar

          1.3 struts2  所依賴工程  struts-2.1.8.1
               lib目錄下的:
                  struts2-core-2.1.8.1.jar
                  xwork-core-2.1.6.jar
                  ognl-2.7.3.jar
                      freemarker-2.3.15.jar
                  commons-fileupload-1.2.1.jar
                  commons-io-1.3.2.jar
                  struts2-spring-plugin-2.1.8.1.jar
                       aopalliance-1.0.jar
                  classworlds-1.1.jar
                  commons-beanutils-1.7.0.jar
                  commons-chain-1.2.jar
                  commons-collections-3.2.jar  在hibernate中已經引用          
                  commons-digester-2.0.jar        
                  commons-lang-2.3.jar
                  commons-logging-1.0.4.jar  此文件在spring中已存在
                  commons-logging-api-1.1.jar
                  commons-validator-1.3.1.jar
                  ezmorph-1.0.3.jar
                  json-lib-2.1.jar 若使用json可選
                  oro-2.0.8.jar
                  oval-1.31.jar


          2、框架的配置文件

          2.1 spring框架配置文件及集成hibernate、二級緩存
                  $WEB_ROOT/WEB-INF/applicationContext.xml
                      src/ehcache.xml

          <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="http://www.springframework.org/schema/beans"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xmlns:context="http://www.springframework.org/schema/context"
                  xmlns:aop="http://www.springframework.org/schema/aop"
                  xmlns:tx="http://www.springframework.org/schema/tx"
                  xsi:schemaLocation="http://www.springframework.org/schema/beans
                     <http://www.springframework.org/schema/beans/spring-beans-2.5.xsd>
                     <http://www.springframework.org/schema/context> <http://www.springframework.org/schema/context/spring-context-2.5.xsd>
                     <http://www.springframework.org/schema/aop> <http://www.springframework.org/schema/aop/spring-aop-2.5.xsd>
                     <http://www.springframework.org/schema/tx> <http://www.springframework.org/schema/tx/spring-tx-2.5.xsd>">
              
              <!-- 配置數據源 -->
                  <context:property-placeholder location="classpath:jdbc4mysql.properties" />
                  <bean id="dataSource"
                          class="org.apache.commons.dbcp.BasicDataSource"
                          destroy-method="close">
                          <property name="driverClassName" value="${driverClassName}" />
                          <property name="url" value="${url}" />
                          <property name="username" value="${username}" />
                          <property name="password" value="${password}" />
                          <!-- 連接池啟動時的初始值 -->
                          <property name="initialSize" value="${initialSize}" />
                          <!-- 連接池的最大值 -->
                          <property name="maxActive" value="${maxActive}" />
                          <!-- 最大空閑值.當經過一個高峰時間后,連接池可以慢慢將已經用不到的連接慢慢釋放一部分,一直減少到maxIdle為止 -->
                          <property name="maxIdle" value="${maxIdle}" />
                          <!--  最小空閑值.當空閑的連接數少于閥值時,連接池就會預申請去一些連接,以保證應急 -->
                          <property name="minIdle" value="${minIdle}" />
                  </bean>
                  
                   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
                   <property name="dataSource" ref="dataSource"/>
                   <property name="mappingResources">
                      <list>
                        <value>cn/tsp2c/sshdemo/domain/User.hbm.xml</value>
                      </list>
                   </property>
                   <property name="hibernateProperties">
                      <value>
                          hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
                          hibernate.hbm2ddl.auto=update
                          hibernate.show_sql=false
                          hibernate.format_sql=false
                          hibernate.cache.use_second_level_cache=true
                          hibernate.cache.use_query_cache=false
                          hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
                        </value>
                   </property>
                   </bean>
                  
              <!-- 配置事務管理器 -->
                  <bean id="txManager"
                          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
                          <property name="dataSource" ref="dataSource" />
                  </bean>
                  
                  <!-- spring支持兩種事務聲明方式:注解和xml配置,建議使用注解方式 -->
                  <!-- 采用@Transactional注解方式使用事務,在dao類及方法需用注解方式標明事務方式 -->
                  <tx:annotation-driven transaction-manager="txManager" />   

               <!-- 用戶DAO實現,實現方式:JDBC
              <bean id="userDao" class="cn.tsp2c.sshdemo.dao.impl.UserDaoImpl">
                  <property name="dataSource" ref="dataSource"/>
              </bean>
              -->
              <!-- 用戶DAO實現,實現方式:hibernte  -->    
              <bean id="userDao" class="cn.tsp2c.sshdemo.dao.impl.hibernate.UserDaoHibernateImpl">
                  <property name="sessionFactory" ref="sessionFactory"/>
              </bean>
             
               
                  <!-- 用戶服務bean -->
                  <bean id="userService" class="cn.tsp2c.sshdemo.service.impl.UserServiceImpl" >
                     <property name="userDao" ref="userDao"/>
                  </bean>
                  
                  <!-- 用戶Action bean,scope=prototype符合struts2的action生成機制 -->
              <bean id="userAction" class="cn.tsp2c.sshdemo.web.action.UserAction" scope="prototype">
                  <property name="userService" ref="userService"/>
              </bean>
          </beans>

          其中集成hibernate需要屬性文件:jdbc4mysql.properties

          driverClassName=com.mysql.jdbc.Driver
          url=jdbc\:mysql\://localhost\:3306/sshdemodb?useUnicode\=true&characterEncoding\=utf-8
          username=root
          password=1234
          initialSize=1
          maxActive=500
          maxIdle=2
          minIdle=1

          需要配置hibernate的二級緩存,如使用ehcache:在src路徑下加入ehcache.xml
          <?xml version="1.0" encoding="UTF-8"?>
          <!--
               defaultCache 設置節點為缺省的緩存策略
               maxElementsInMemory 設置內存中最大允許存在的對象數量
               eternal 設置緩存中的對象是否永遠不過期
               overflowToDisk 把超出內存設定的溢出的對象存放到硬盤上
               timeToIdleSeconds 設置緩存對象空閑多長時間就過期,過期的對象會被清除掉
               timeToLiveSeconds 設置緩存對象總的存活時間
               diskPersistent 當jvm結束時是否把內存中的對象持久化到磁盤
               diskExpiryThreadIntervalSeconds 設置用于清除過期對象的監聽線程的輪詢時間
          -->
          <ehcache>
              <diskStore path="e:\cache"/>
              <defaultCache  maxElementsInMemory="1000" eternal="false" overflowToDisk="true"
                  timeToIdleSeconds="120"
                  timeToLiveSeconds="180"
                  diskPersistent="false"
                  diskExpiryThreadIntervalSeconds="60"/>
                  <cache name="cn.tsp2c.sshdemo.domain.User" maxElementsInMemory="100" eternal="false"
              overflowToDisk="true" timeToIdleSeconds="300" timeToLiveSeconds="500" diskPersistent="false"/>
          </ehcache>

          2.2 struts2的配置文件: src/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>
              <!-- 指定web應用的默認編碼為UTF-8,功能等同于request.setCharacterEncoding() -->
              <constant name="struts.i18n.encoding" value="UTF-8"/>
              <!-- 指定struts2的請求處理后綴,匹配*.action的所有請求 -->
              <constant name="struts.action.extension" value="action"/>
              <!-- 關閉struts2的!動態方法調用,建議使用通配符匹配方式實現動態方法調用 -->
              <constant name="struts.enable.DynamicMethodInvocation" value="false"/>
              <!-- 設置瀏覽器是否緩存靜態頁面,默認為true,建議:開發階段關閉,生產環境打開 -->
              <constant name="struts.serve.static.browserCache" value="false" />
              <!-- 當struts.xml修改時自動重新加載,默認為false。建議:開發階段打開,生產環境關閉 -->
              <constant name="struts.configuration.xml.reload" value="true"/>
              <!-- 開發模式下打開,便于打印詳細日志,生產環境下關閉 -->
              <constant name="struts.devMode" value="true" />
              <!-- 設置視圖主題為css_xhtml -->
              <constant name="struts.ui.theme" value="simple" />
              <!-- 指定struts中action交由spring創建 -->
              <constant name="struts.objectFactory" value="spring"/>
              
              <package name="base" extends="struts-default">
                  <global-results>
                         <result name="message">/WEB-INF/page/message.jsp</result>
                         <result name="error">/WEB-INF/page/error.jsp</result>
                  </global-results>
              </package>
              
              <package name="user" namespace="/user" extends="base">
                  <action name="login" class="cn.tsp2c.sshdemo.web.action.LoginAction" method="execute">
                      <result name="success">/index.jsp</result>
                      <result name="input">/login.jsp</result>
                  </action>
                  <action name="user_*" class="cn.tsp2c.sshdemo.web.action.UserAction" method="{1}">
                      <result name="list">/userlist.jsp</result>
                      <result name="add" type="redirect">/useradd.jsp</result>
                  </action>
              </package>
          </struts>


          3、需要把spring、strusts2框架注入到web容器(hibernate框架被spring集成,和web容器沒有關系。所以不需要在web.xml中配置)

          web.xml文件:
          <?xml version="1.0" encoding="UTF-8"?>
          <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                  <http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd>">
              <!-- 配置spring的xml文件,若配置文件有多個,可用,或空格分隔 -->
                  <context-param>
                          <param-name>contextConfigLocation</param-name>
                          <param-value>
                                  /WEB-INF/applicationContext.xml
                          </param-value>
                  </context-param>

                  <!-- web容器加載struts2配置 -->
                  <filter>
                          <filter-name>struts2</filter-name>
                          <filter-class>
                                  org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
                          </filter-class>
                  </filter>
                  <filter-mapping>
                          <filter-name>struts2</filter-name>
                          <url-pattern>/*</url-pattern>
                  </filter-mapping>

                  <!-- web容器加載spring配置 -->
                  <listener>
                          <listener-class>
                                  org.springframework.web.context.ContextLoaderListener
                          </listener-class>
                  </listener>

                  <!-- web容器增加字符集轉換的過濾器,由于struts2框架解決了字符集轉碼,此配置可以注釋掉
                  <filter>
                          <filter-name>encoding</filter-name>
                          <filter-class>
                                  org.springframework.web.filter.CharacterEncodingFilter
                          </filter-class>
                          <init-param>
                                  <param-name>encoding</param-name>
                                  <param-value>UTF-8</param-value>
                          </init-param>
                  </filter>
                  <filter-mapping>
                          <filter-name>encoding</filter-name>
                          <url-pattern>/*</url-pattern>
                  </filter-mapping>
              -->
                  <!-- 解決hibernate的session關閉導致延遲加載異常的問題 -->
                  <!--  
                          <filter>
                          <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
                          <filter-class>
                            org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
                          </filter-class>
                          </filter>
                          <filter-mapping>
                            <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
                            <url-pattern>/*</url-pattern>
                          </filter-mapping>
                  -->

                  <welcome-file-list>
                          <welcome-file>index.jsp</welcome-file>
                  </welcome-file-list>
          </web-app>

          3.1 web容器集成spring

                  <!-- 配置spring的xml文件,若配置文件有多個,可用,或空格分隔 -->
                  <context-param>
                          <param-name>contextConfigLocation</param-name>
                          <param-value>
                                  /WEB-INF/applicationContext.xml
                          </param-value>
                  </context-param>

                  <!-- web容器加載spring配置 -->
                  <listener>
                          <listener-class>
                                  org.springframework.web.context.ContextLoaderListener
                          </listener-class>
                  </listener>


          3.2 web容器集成struts2
                  <!-- web容器加載struts2配置 -->
                  <filter>
                          <filter-name>struts2</filter-name>
                          <filter-class>
                                  org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
                          </filter-class>
                  </filter>
                  <filter-mapping>
                          <filter-name>struts2</filter-name>
                          <url-pattern>/*</url-pattern>
                  </filter-mapping>

          3.3 關于在session范圍內解決hibernate的延遲加載問題

          <!-- 解決hibernate的session關閉導致延遲加載異常的問題 -->
          <filter>
              <filter-name>HibernateSessionInViewFilter</filter-name>
              <filter-class>
                org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
              </filter-class>
          </filter>
          <filter-mapping>
              <filter-name>HibernateSessionInViewFilter</filter-name>
              <url-pattern>/*</url-pattern>
          </filter-mapping>


          4、如何使用s2sh框架實現MVC

          4.1 action

          struts.xml:
              <!-- 指定struts中action交由spring創建 -->
              <constant name="struts.objectFactory" value="spring"/>

          <action name="user_*" class="cn.tsp2c.sshdemo.web.action.UserAction" method="{1}">
              <result name="list">/userlist.jsp</result>
              <result name="add" type="redirect">/useradd.jsp</result>
          </action>

          在spring中配置:
          <!-- 用戶Action bean,scope=prototype符合struts2的action生成機制 -->
              <bean id="userAction" class="cn.tsp2c.sshdemo.web.action.UserAction" scope="prototype">
                  <property name="userService" ref="userService"/>
              </bean>

          4.2 service
          <!-- 用戶服務bean -->
          <bean id="userService" class="cn.tsp2c.sshdemo.service.impl.UserServiceImpl" >
             <property name="userDao" ref="userDao"/>
          </bean>

          4.3 dao
          <!-- 用戶DAO實現,實現方式:JDBC -->
          <bean id="userDao" class="cn.tsp2c.sshdemo.dao.impl.UserDaoImpl">
              <property name="dataSource" ref="dataSource"/>
          </bean>
          posted @ 2011-03-25 20:18 Soap MacTavish 閱讀(3305) | 評論 (1)編輯 收藏

          原文地址:http://hi.baidu.com/gg7663/blog/item/f7bfb70f74a5c1f536d122b9.html

          開發項目涉及到的表太多,一個一個的寫JAVA實體類很是費事。MyEclipse提供簡便的方法:反向數據庫 

          ***選擇表反向的時候一次全選上,省的后面配那個發現關聯表選項

          步驟大致如下: 
          第一步: 
          window-->open Perspective-->MyEclipse Java Persistence 
          操作后會出現一個視圖DB Brower:MyEclipse Derby 

          在空白區點擊右鍵,新建一個數據庫對象。我用的是mysql,其實我一直想用oracle之類的,只是機子內存小,又懶得倒騰別的,結果截個圖吧:

          左邊的屬性按照自己使用的數據庫填就行了,左邊部分是我建好以后的結果,數據庫名叫shop,有個user表 是專門這次測試用的




          第二步: 
          新建一個WEB項目 
          然后選中項目右鍵-->MyEclipse-->add Hibernate Capabilities 

          如果需要用到在實體類上添加注釋的話那么選中緊挨著的add Hibernate Annotations Support(據說現在都用annotations了就選上吧!!)

          然后下一步選中一個目錄存放自動生成hibernate.cfg.xml文件,一般毫無例外的放在src根目錄下,下一步選中一個DB Driver中我們第一步建立的那個(對我來說是com.mysql....反正就是自己建的那個唄),然后下一步選中一個目錄存放自動生成的HibernateSessionFactory工具類 ,結果出來以后我看了一下,這個hibernateSessionFactory工具類就是一個拿Session的單例,還有一些其它關閉session之類的方法,一看便知。下圖是生成的配置文件:



          第三步反向生成實體類 

          到DB Brower中找到要反向的表選中并且點擊右鍵--->Hibernate Reverse Enginnering

           

          看選項:java src folder 源碼包,不用多解釋,java package——存放將要反轉出來的實體類,選擇目錄(應該是提前建好的com.xxx.model之類的包);Create pojo<>db。。。這個選項選中,就是我們建立從表到簡單java 對象(即pojo)的配置,把下面的add hibernate mapping annotations to pojo的選上,其它不管,這個選項用來“添加映射注解到pojo對象上”,它上面的那個選項用來創建xml的,據說不太用了,就用annotation吧!


          這樣應該就夠了,下一步

          看圖:type Mapping要選上hibernatetypes,這樣這些注解都是來自 javax.persistence.*了 (剛觀察過!)

          id generator 看下拉列表就知道是配置id生成策略的

          那兩個enable 是說映射關系發現(detection),明白了吧?英文好就是沾光,不用像我一樣還得查字典了。


          下一步:沒啥理解不了的 動手點點就知道了



          直接完成,發現代碼很漂亮,注釋很完美,而且肯定沒有錯誤,幾分鐘都能搞定數十個表,不得不感嘆myeclipse,真是個大金礦。

          posted @ 2011-03-22 12:52 Soap MacTavish 閱讀(1332) | 評論 (0)編輯 收藏
           記得剛剛接觸Linux的時候,自己真是一名不折不扣的菜鳥,通過一年的努力,自己可以單獨操作Linux了,我將把以后遇到的比較有用的命令積累在這篇博客上。

               

                1.  Fedora 版Linux   ifconfig
                     /sbin/ipconfig

                2.  設置tomcat,系統啟動,同時tomcat也啟來

                     echo "/data/webapp/reg/bin/startup.sh">>/etc/rc.local

                3.  查看系統 Java 進程

                     ps -ef|grep java

                4.  設置動態IP

                     ifconfig eth0 192.168.1.101 up

                5.  在ubuntu下修改文件

                     gedit /etc/profile

                6.  netstat –anput   查看端口占有情況

                7.  killall –9 java   殺死所有系統中java進程

                8.  ./startup.sh ; tail –f /data/webapp/tomcat/logs/catalina.out   啟動tomcat,并且查看日志

                9.  vi編輯文件時,誤寫了,先按ESC 再按u ,可以恢復。

              10.  改變一個文件夾下所有文件的所有者

                     chown –R xg.xg folder

              11.  tomcat 隨著系統自啟動

                     1. 把tomcat和程序放到/home/user

                     2. 改變tomcat的所有者  chown –R hans.hans tomcat

                     3. echo "/home/hans/tomcat/startup.sh">>/etc/profile

              12.  還有一點要提醒修改linux文件時,一定要先備份

              13.  批量修改linux下文件的編碼:

                    cd /home/hans/test

                    find ./ -type f -name "*.c"|while read line;do
                    echo $line
                    iconv -f GB2312 -t UTF-8 $line > ${line}.utf8
                    mv $line ${line}.gb2312
                    mv ${line}.utf8 $line
                    done

              14.  vi / vim 編輯文件時,顯示行號

                    vim /etc/vimrc

                      在末行處加上 : set number

           

           

           

           

          Fedora 12 篇

              1.  Fedora 12 安裝后不能遠程使用CRT登錄:

                     ntsysv

                     出現圖形界面選擇ssh服務就OK

              2.  Fedora 12 和 Ubuntu的安裝軟件有所不同

                   Fedora : yum install gcc

                   Ubuntu :  apt-get install gcc

              3.  VMware 全屏

                   首先 Ctrl+Alt+Enter 全屏 但是linux只是占中間的屏幕,這是因為linux的分辨率與window的分辨率不一樣,調整linux的分辨率和windows一樣就好了。

              4.  Fedora 9 安裝完后不能使用 ifconfig ,還得這樣:/sbin/ifconfig 才有效

                   vi /etc/profile

                    if [ "$EUID" = "0" ]; then
                    pathmunge /sbin
                    pathmunge /usr/sbin
                    pathmunge /usr/local/sbin
                    fi

                  修改:把第一行注釋掉

                  #if [ "$EUID" = "0" ]; then
                    pathmunge /sbin
                    pathmunge /usr/sbin
                    pathmunge /usr/local/sbin
                    fi

                 然后重啟,就好使了!         

             5.  安裝中文輸入法

                 yum install scim* -y

          posted @ 2011-03-06 10:16 Soap MacTavish 閱讀(464) | 評論 (0)編輯 收藏
          主站蜘蛛池模板: 张家港市| 榆林市| 恩施市| 钦州市| 兴隆县| 阜康市| 碌曲县| 凭祥市| 辽源市| 万山特区| 巨鹿县| 武穴市| 东方市| 奎屯市| 沙田区| 安达市| 黄浦区| 望奎县| 手游| 乌兰县| 礼泉县| 韩城市| 孝感市| 阜南县| 晋城| 斗六市| 涟源市| 宁都县| 东平县| 家居| 田林县| 潜山县| 花莲市| 邳州市| 张北县| 龙江县| 正蓝旗| 博湖县| 天门市| 和田县| 云霄县|