Loading...

          java .net

          好幾天了,發現無法打開我的google blogger,難道被傳說中的和諧了?!我剛剛搬家到blogger啊,還沒玩兒幾天呢
          posted @ 2009-06-03 12:23 豬 閱讀(421) | 評論 (1)編輯 收藏
          private void buttonToExcel_Click(object sender, EventArgs e)
          {

              SaveFileDialog saveFileDialog = new SaveFileDialog();
              saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
              saveFileDialog.FilterIndex = 0;
              saveFileDialog.RestoreDirectory = true;
              saveFileDialog.CreatePrompt = true;
              saveFileDialog.Title = "Export Excel File To";
              saveFileDialog.ShowDialog();
              Stream myStream;
              myStream = saveFileDialog.OpenFile();
              StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
              string str = "";
              try
              {
                  //寫標題
                  for (int i = 0; i < this.dataGridView3.ColumnCount; i++)
                  {
                      if (i > 0)
                      {
                          str += "\t";
                      }

                      str += this.dataGridView3.Columns[i].HeaderText;
                  }
                  sw.WriteLine(str);
                  //寫內容
                  for (int j = 0; j < this.dataGridView3.Rows.Count; j++)
                  {
                      string tempStr = "";
                      for (int k = 0; k < this.dataGridView3.Columns.Count; k++)
                      {
                          if (k > 0)
                          {
                              tempStr += "\t";
                          }
                          tempStr += this.dataGridView3.Rows[j].Cells[k].Value + "";
                      }
                      sw.WriteLine(tempStr);
                  }

                  sw.Close();
                  myStream.Close();
              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.ToString());
              }

              finally
              {
                  sw.Close();
                  myStream.Close();
              }    
              MessageBox.Show("OK");
          }
          posted @ 2009-06-03 12:23 豬 閱讀(1160) | 評論 (0)編輯 收藏
          今早看到一則新聞:出處http://www.infoq.com/cn/news/2009/05/vs2010-beta1
          昨日,Soma在其博客上宣布Visual Studio 2010和.NET 4.0的Beta 1已經可以通過MSDN訂閱下載了。
          過去的幾個月里,我們已經初步了解了Visual Studio 2010和.NET 4.0的一系列新特性。和上次發布的CTP版本不同,這次的Beta 1是以安裝包形式發布的,而非VPC鏡像。這樣我們就可以自己下載安裝,一睹為快了,不過大家最好還是安裝到虛擬機中。
          在Visual Studio 2010的產品站點上,我們可以了解到VS 2010給我們帶來的革新:
          • 加強的用戶體驗:VS2010帶來了新用戶界面,讓工作更專注。
          • 便捷的SharePoint開發:對SharePoint開發更完善的支持,讓SharePoint開發不再痛苦。
          • 應用程序生命周期管理(ALM):把項目角色更緊密地集成到整個應用程序生命周期中。
          • 更佳的Web應用開發:對JavaScript的加強,Silverlight開發的完整支持,更容易部署。
          • 云應用程序開發:集成Azure開發包,無縫進行云應用程序的開發。
          • 數據庫支持:對IBM DB2和Oracle更好的支持。
          • 并行編程:從運行時到IDE都對并行編程提供了很好的支持。
          • 更高的生產力:代碼上下文可幫助理解現存代碼,對C++開發體驗的提升,Windows 7的支持,Office開發支持的加強。
          下面一些有用的鏈接,可讓大家獲取更多關于Visual Studio 2010和.NET 4.0的信息:PressPass:Visual Studio 2010公開 eWeek:Quest Tool for Oracle Database Developers InformationWeek:微軟詳述了Visual Studio 2010和.NET 4.0 Channel 9:Visual Studio 2010 來自Jono Wells的Twitter更新

          我開始接觸.net是Visual Studio 2003和.net1.1,不由感嘆技術更新之快,之后微軟出了很多新名詞:WPF、WCF、Silverlight、Linq等等,真是抓瞎了,學不過來,現在都2010了
          posted @ 2009-06-03 12:22 豬 閱讀(269) | 評論 (0)編輯 收藏
          經常遇到javascript 缺少對象的錯誤,無外乎調用的js方法未定義,名字寫錯了,之類,今兒遇到的郁悶的
          我在頁面中加了一個注釋 ,注意結束標簽--和>之間有個空格,結果未起作用,結果可想而知,找了半天才找到
          posted @ 2009-04-23 20:39 豬 閱讀(3262) | 評論 (1)編輯 收藏
          通過簡單修改,將blogger原有標簽widget改成標簽云形式
          修改html模板
          找到
          <b:loop values='data:labels' var='label'>
          ..........
          </b:loop>
          <b:loop>標簽之間的內容改成:
          <b:if cond='data:blog.url == data:label.url'>
            <span expr:dir='data:blog.languageDirection'>
              <font expr:size='data:label.count'><data:label.name/></font>
            </span>
          <b:else/>
            <a expr:dir='data:blog.languageDirection' expr:href='data:label.url'>
              <font expr:size='data:label.count'><data:label.name/></font>
            </a>
          </b:if>
          <span dir='ltr'>(<data:label.count/>)</span>
          OK啦 看看效果吧
          posted @ 2009-04-22 15:45 豬 閱讀(306) | 評論 (0)編輯 收藏
          早就注意到google blogger 首頁文章不能只顯示摘要,而是全部顯示
          google了幾個網頁提供的方法,官方的方式是在文章中加一個span,讓當在首頁顯示時span隱藏,這種方式只適合以后新增文章,以前的舊文章均沒有加入這個span,就不好使
          想到blogger這么高度自定義,那么用jquery實現吧
          經過一個晚上的實驗,終于出結果了

          修改html模板,在<head>下面加入
          <b:if cond='data:blog.pageType != "item"'>
             <script src='http://www.google.com/jsapi'/>
                 <script type='text/javascript'>
                      google.load('jquery', '1.3.2');
                 </script>
                 <script language='javascript' src='http://www.reindel.com/truncate/jquery.truncate.js' type='text/javascript'/>
                 <script type='text/javascript'>
                 $(function() {
                     $("div[class='post-body entry-content']").truncate( 500, {
                         chars: /\s/,
                         trail: [ " ( <a href='#' class='truncate_show'>more</a> . . . )", " ( . . . <a href='#' class='truncate_hide'>less</a> )" ]
                     });
                 });
                 </script>
                 </b:if>

          經測試,IE7中會提示:是否停止運行腳本?此頁面的腳本造成Internet Explorer 運行速度減慢,如果繼續運行,您的計算機將可能停止響應

          這個.truncate的第一個參數設的大小也有關系,這里設了500

          在FireFox3和Google Chrome中沒有什么提示,速度還挺快,但會有顯示問題,因為應用的jquery.truncate框架有瀏覽器兼容問題

          posted @ 2009-04-22 12:49 豬 閱讀(473) | 評論 (0)編輯 收藏
              截止到今天下午,中午把自己的應用部署到了Google app engine,用了一周的時間啊,真是費勁周折
              好長時間了,聽說Google出了個Google App Engine - Google Code,說是可以讓用戶上傳自己的應用,但是只支持python,當時就想什么

          時候支持java啦啊,上個月看新聞就聽說Google app engine 要開始支持java啦,哈哈,喜,到時候把我的寫的blog傳上去,哈哈
              上周,終于知道了Google app engine終于開始宣布支持java了,哈哈呼呼,動手,到官網一看,沒有支持java的動靜啊,

          對,英文頁面,哈哈,看來中文的還是慢一拍啊
              首先到注冊一個,竟然還要手機號發注冊碼,Google了一下 說在手機號前加 86就OK了,填上手機號點按鈕,呦,短信馬

          上就來啦,搞定,注冊成功。
              這下下載SDK,還有eclipse插件,幫助文檔只有英文的,配合Google工具欄的翻譯功能,基本能看懂,哈哈,感覺一路下

          來,英語水平有所上升啊,哈哈
              打開eclipse,裝上插件,很輕松的建了第一個應用,插件自動生成了一個小實例,部署上去,驚嘆于Google的GWT
              開始一直我原來寫的blog程序,原來到處找免費的空間,jsp的空間不好找,先是找了http://www.eatj.com/,但是沒24小

          時(好像是)就會自動停止,必須手動重啟服務,原來還堅持去上去看看,后來終于因為有一次很忙時間太長被注銷了,接下來就聽

          說了http://www.stax.net/,跟Google的路子差不多,好像還更自定義些,繼續使用著,搞java朋友可以去看看
              因為google app engine使用Google Account,原來的用戶管理模塊需要剔除,原來數據庫使用的是mysql hibernate,現在

          Google 使用JDO,數據庫被封裝了,還好,當初設計是采用了工廠模式,實現了數據庫訪問模塊與業務處理模塊的松耦合,很容易

          配置數據庫訪問模塊的更改,感受到好的設計真是為以后修改省很多事啊。
              接著說部署我的Google app engine,終于改好相關代碼,準備上傳了,噩夢開始了
              1、Unable to upload:
          java.lang.IllegalStateException: cannot find javac executable based on java.home, tried “D:\Java\jdk1.6.0_13

          \jre\bin\javac.exe” and “D:\Java\bin\javac.exe”
          參考http://zhuyx808.javaeye.com/blog/370124
          http://onlypython.group.javaeye.com/group/blog/366471
          http://618119.com/archives/2009/04/12/148.html
              2、決定轉戰Linux..
              手上有四個版本的Linux,Ubuntu,Redhat,Fedora,openSUSE,都裝過,后來感覺還是Fedora比較好用,我裝的是

          Fedora9,Fedora10正在下載中

          以下Linux安裝配置,僅限于在Fedora9上安裝通過,資料都是通過Google搜索得來,感謝各位網友,網絡的力量真是強大。

          1、在VMware中裝上Fedora9后,首先安裝VMware-tools,方便與主機交互

          首先需要下載相應的kernel-devel.rpm包進行安裝
          首先查看內核版本:uname -r一下,我的是2.6.25-14.fc9.i686,

          所以下載kernel-devel-2.6.25-14.fc9.i686.rpm,然后運行rpm -ivh kernel-devel-2.6.25-14.fc9.i686.rpm 進行安裝. 內核安

          裝完畢后,需要用這個命令確定內核 C header 的安裝目錄:ls -d /usr/src/kernels/$(uname -r)*/include

          安裝內核具體步驟如下:

          [root@localhost ~]# uname -r
          2.6.25-14.fc9.i686
          [root@localhost ~]# rpm -q kernel-devel
          package kernel-devel is not installed
          [root@localhost ~]# cd /home
          [root@localhost home]# wget ftp://rpmfind.net/linux/fedora/releases/9/Everything/i386/os/Packages/kernel-devel-

          2.6.25-14.fc9.i686.rpm
          [root@localhost home]# rpm -i kernel-devel-2.6.25-14.fc9.i686.rpm
          [root@localhost home]# rpm -q kernel-devel
          kernel-devel-2.6.25-14.fc9.i686

          再安裝gcc,否則安裝VMware-tools時會提示:Setup is unable to find the "gcc" program on your machine. Please make

          sure it
          is installed. Do you want to specify the location of this program by hand?
          [yes]

          What is the location of the "gcc" program on your machine?

          在命令行執行:yum install gcc


          然后安裝VMware Tools就行了,打開菜單“VM -> Install VMware Tools”,然后有光盤自動彈出,把里面的源代碼拷貝出來.我的文

          件是VMwareTools-6.5.0-xxxxx.tar.gz,我把它拷貝到/opt里.

          安裝 VMware TOOLS

                    cd /opt

                    tar -zxvf VMwareTools-6.5.0-xxxxx.tar.gz

                    cd vmware-tools-distrib/

                    ./vmware-install.pl

          再一路安回車OK


          2、由于在裝載xorg-x11-drv-vmmouse驅動時的一個bug,在客戶虛擬機的顯示中,鼠標位置可能不正確。直到被更新前,在客戶機

          中添加Option NoAutoAddDevices到/etc/X11/xorg.conf文件的ServerFlags節中。如果需要,創建這個節:

          Section "ServerFlags"
                  Option      "NoAutoAddDevices"
          EndSection


          3、第一,安裝JDK
          第一,到http://java.sun.com下載最新JDK,當前本人下載的是jdk1.6.0_02!下載文件:jdk-6u2-linux-i586-rpm.bin.注意是

          rpm.bin的!

          第二,給下載回來的文件增加執行權限:chmod 755 jdk-6u2-linux-i586-rpm.bin.

          第三,執行文件:./jdk-6u2-linux-i586-rpm.bin.

          第四,執行文件產生一個rpm文件,可直接雙擊執行也可以在shell下執行:rpm -ivh jdk-6u2-linux-i586-rpm.

          第五:配置環境變量,環境變量配置可在全局文件/etc/profile下修改,這樣所有linux系統的用戶都可以用JDK,如果只是特定的

          用戶用可修改/root/.bashrc文件,本人的修改為:

          #java set
          set JAVA_HOME=/usr/java/jdk1.6.0_02
          export JAVA_HOME
          export JRE_HOME=/usr/java/jre1.6.0_02
          set JAVA_BIN=/usr/java/jre1.6.0_02
          export JAVA_BIN

          第五,當環境變量修改完后,重起系統,在shell下輸入java,看是否輸入相關JAVA幫助信息,如果有,說明已經安裝成功!如果沒有,

          檢查一下變量環境設置是否有誤!

          2,安裝Eclipse,Eclipse不需要安裝,只要解壓縮就行了

          運行Google app Engine不需要Tomcat
          3,安裝tomcat的方法跟安裝eclipse的一樣,也是下一個tar.gz的文件按安裝eclipse方法和步驟就行了!進入tomcat/bin

          下./startup.sh,如果在shell出現jdk的相關信息說明已經安裝成功啟動了,在firefox下打http://localhost:8080/出現tomcat頁就

          大成功了!

          在以上的安裝過程之中,出現了小插曲,就是tomcat找不到JDK,后來我又到JAVA網站下了一個JRE回來安裝,并建JRE_HOME,重起

          TOMCAT,成功了!


          4、運行Google App Engine的應用,提示
          ** Unable to load Mozilla for hosted mode **
          java.lang.UnsatisfiedLinkError:
          /home/dhofmann/development/ide/gwt-linux-1.4.61/mozilla-1.7.12/libxpcom.so:
          libstdc++.so.5: cannot open shared object file: No such file or directory
          at java.lang.ClassLoader$NativeLibrary.load(Native Method)
          解決辦法:
          Search for stdc++5 in synaptic package manager (System->Administration->Synaptic) and then mark and install it. Or

          use this console command:
          yum install libstdc++.so.5

          停止進程命令:
          就是kill 比較不錯,如果kill不了. 加上-9
          如#kill -9 1778
          注1778 為進程pid
          pid可以通過ps aux|grep 服務名查得


          啟動Eclipse時,提示錯誤:

          eclipse.buildId=M20090211-1700
          java.version=1.6.0
          java.vendor=Sun Microsystems Inc.
          BootLoader constants: OS=linux, ARCH=x86, WS=gtk, NL=zh_CN
          Command-line arguments: -os linux -ws gtk -arch x86

          !ENTRY org.eclipse.ui.workbench 4 0 2009-04-18 02:12:18.085
          !MESSAGE Widget disposed too early!
          !STACK 0
          java.lang.RuntimeException: Widget disposed too early!
              at org.eclipse.ui.internal.WorkbenchPartReference$1.widgetDisposed(WorkbenchPartReference.java:171)

          在網上搜索得知:   
          eclipse.ini文件加個參數
          -vmargs
          -Dorg.eclipse.swt.browser.XULRunnerPath=

          在Fedora9中安裝拼音輸入法
          保證Fedora 9聯網的狀態下
          在application->system tools->terminal 應用程序->系統工具->終端
          輸入
          su 回車
          提示輸入root密碼
          yum install scim 回車
          系統會自動從一個鏡uy像站點檢索scim,并詢問是否下載,選擇y
          下載完成后輸入
          yum install scim-pinyin 回車
          提示和操作如上
          然后輸入
          scim 回車
          (筆者的電腦運行到starting SCIM后就沒有反應了,不過沒關系這時候可以按ctrl+c中斷)
          選擇Fedora 9
          System->Preference->Personal->Input Method
          系統->首選項->個人->輸入法
          選擇啟用新特性,然后選擇里面的SCIM,然后選擇配置SCIM,
          在進入后的界面中選擇Global Set 全局設置,在這里設置激活熱鍵即可,然后重新啟動電腦,就可以在Fedora 9下面使用中文輸入

          法了 雖然有點麻煩 但畢竟fedora等眾多linux桌面系統還很年輕,有很多不足之處。

          參考資料:
          http://www.5dlinux.com/article/6/2007/linux_9042.html
          http://zhidao.baidu.com/question/62290384.html
          http://www.linuxidc.com/Linux/2008-09/16011.htm
          posted @ 2009-04-19 00:32 豬 閱讀(6448) | 評論 (6)編輯 收藏
          JavaScript寫的小游戲 用了jquery

          訪問地址:http://liuspring315.googlepages.com/

          posted @ 2009-04-02 10:56 豬 閱讀(648) | 評論 (0)編輯 收藏
          備案:
          java.lang.IllegalStateException: No data type for node: org.hibernate.hql.ast.tree.IdentNode


          hibernate查詢時報上面的異常,仔細檢查hql寫的有不對的地方,我就是把逗號,寫成點.
          posted @ 2009-03-31 13:27 豬 閱讀(472) | 評論 (0)編輯 收藏
          給你一個日期2009-01-01 00:00:00 獲取這個月的最后一天。
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          Date date=null;
          try {
              date = sdf.parse("2009-12-01 00:00:00");
          } catch (ParseException e) {
              e.printStackTrace();
          }
          Calendar calendar = new GregorianCalendar();
          calendar.setTime(date);
          calendar.set(Calendar.DATE, calendar
                  .getActualMaximum(Calendar.DATE));
          Date d = calendar.getTime();
          System.out.println(sdf.format(d));
          posted @ 2009-03-31 13:23 豬 閱讀(637) | 評論 (0)編輯 收藏

          好久沒寫了,過了個年,過了七天豬的日子,吃了睡睡了吃中間偶爾看看電視,為假期準備了幾個電影都沒看,唉:-(忙,上了幾天班了,怎么感覺這周過的這么慢呢,剛剛剛周四
          昨兒看了一集struts2

          總結幾點

          1、struts.xml中的package標簽有個abstract="true"屬性,如果為true,則這個package中不能有<action>標簽,為true表示這個package為一個抽象package,就像java中的接口中不能有方法實現似的。

          2、package標簽還有個namespace屬性,它關系到這個package中的action訪問路徑,默認是"",比如

          <package name= "capinfo" extends= "struts-default" namespace="/hello">
          <action name= "HelloWorld"
          class= "com.capinfo.struts2.action.HelloWordAction" >
          <result>/HelloWorld.jsp</result>
          </action>
          </package>

          則jsp中則應為<form action="/hello/HelloWorld.action" method="post">,如果沒有找到hello/HelloWorld.action則會尋找namespace為""的,也就是HelloWorld.action,如果再沒有,就會拋出異常。

          3、在struts1中可以有多個struts-config.xml的文件,只要在web.xml配置org.apache.struts.action.ActionServlet時加到config參數中,以逗號分隔,在struts2中也可以有多個struts.xml形式的文件,這次不需要修改web.xml了,在struts.xml中添加諸如
          <include file=""/>就Ok了

          4、關于struts各種參數的配置信息位于struts2-core.jar/org.apache.struts2/default.properties中,如果要修改里面的配置,有兩種方式,一是在struts.xml中配置,例如<constant name="struts.devMode" value="true" />,二是在classess中建一個struts.properties,在里面配置比如struts.i18n.encoding=GBK,在struts2-core.jar/org.apache.struts2/default.properties中有一下這句注釋,說明了struts.properties將覆蓋default.properties中的配置

          ### Struts default properties
          ###(can be overridden by a struts.properties file in the root of the classpath)
          ###

          就總結這么些先。

          posted @ 2009-02-05 13:40 豬 閱讀(318) | 評論 (0)編輯 收藏
          前兩天實踐了關于攔截器的具體實現,說實話關于底層實現還沒有看明白,看jdk的源碼中的
          public static Class<?> getProxyClass(ClassLoader loader,Class<?>... interfaces)
          方法,好長啊
          迂回一下,今兒看struts2的具體攔截器Interceptor怎么配置
          配置可比自己寫實現攔截器容易多了
          1、首先寫一個攔截器類,攔截器類有兩只寫法(目前俺知道的)
          一種是顯示com.opensymphony.xwork2.interceptor.Interceptor接口,com.opensymphony.xwork2.interceptor.Interceptor接口有三個方法destroy()、init()和String intercept(ActionInvocation actionInvocation),跟過濾器差不多
          這里指出的是init初始化方法將在容器啟動是調用這個方法。
          package com.test.interceptor;

          /**
          * Created by IntelliJ IDEA.
          * User: Administrator
          * Date: 2009-1-15
          * Time: 16:34:17
          * To change this template use File | Settings | File Templates.
          */
          import com.opensymphony.xwork2.interceptor.Interceptor;
          import com.opensymphony.xwork2.ActionInvocation;

          public class MyInterceptor implements Interceptor{

              public void destroy() {

              }

              public void init() {

              }

              public String intercept(ActionInvocation actionInvocation) throws Exception {

                  System.out.println("test intercept begin");
                  String result = actionInvocation.invoke();
                  System.out.println("test intercept finish");
                  return result;
              }
          }

          另一種就是繼承com.opensymphony.xwork2.interceptor.AbstractInterceptor,這是個抽象類,并實現了com.opensymphony.xwork2.interceptor.Interceptor接口,分別實現了init和destroy方法,但什么都沒做,繼承AbstractInterceptor后,實現intercept方法就行了,
          這里指出的是在intercept方法中執行actionInvocation.invoke();執行所攔截的action中的方法;
          2、攔截器寫完了剩下就是配置了,這里要用到struts.xml的組織結構<struts>中有<package>包的的概念,包與包之間可以繼承extends,就像子類繼承父類一樣,子類將擁有父類的屬性和配置,我們一般都繼承extends="struts-default",而struts-default定義在struts2-core.jar 中的struts-default.xml中,struts-default包中定義了很多struts2提供的攔截器和攔截器棧(攔截器棧可以包含多個攔截器或攔截器棧),struts2的好多功能都是實現在這些攔截器中,其中有個<default-interceptor-ref name="defaultStack"/>標簽定義了默認的攔截器,如果<action>配置中沒有攔截器配置,那就調用默認攔截器,如果有攔截器配置,要么同時加上默認攔截器,要么在自己的package中加入設置默認攔截器的標簽。
          <package name="capinfo" extends="struts-default">
                  <interceptors>
                      <interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor">
                      </interceptor>
                  </interceptors>
                  <action name="HelloWorld"
                      class="com.capinfo.struts2.action.HelloWordAction">
                      <result>/HelloWorld.jsp</result>
                      <interceptor-ref name="myInterceptor"></interceptor-ref>
                      <interceptor-ref name="defaultStack"></interceptor-ref>
                  </action>
           
                  <!-- Add your actions here -->
              </package>

          posted @ 2009-01-16 16:08 豬 閱讀(295) | 評論 (0)編輯 收藏
          說是struts2的核心就是攔截器
          先看看攔截器的實現
          實現攔截器有這么幾個東西構成
          1、目標對象-將被攔截的對象,這個對象中的方法被調用是將被攔截
          要求目標對象面向接口編程,首先要定義接口
          package com.test.interceptor;

          /**
          * Created by IntelliJ IDEA.
          * User: Administrator
          * Date: 2009-1-9
          * Time: 17:37:23
          * 面向接口編程
          * 目標對象的接口
          */
          public interface TargetInterface {
              public void doSomething();
          }
          目標對象實現
          package com.test.interceptor;

          /**
          * Created by IntelliJ IDEA.
          * User: Administrator
          * Date: 2009-1-9
          * Time: 17:36:26
          * 目標對象
          */
          public class Target implements TargetInterface{
              public void doSomething(){
                  System.out.println("do something");
              }
          }

          2、攔截器對象-里面包含在調用目標對象前或后要調用的方法
          package com.test.interceptor;

          /**
          * Created by IntelliJ IDEA.
          * User: Administrator
          * Date: 2009-1-9
          * Time: 17:39:32
          * 攔截器
          */
          public class Interceptor {
              public void before(){
                  System.out.println("before");
              }

              public void after(){
                  System.out.println("after");
              }
          }

          3、處理器-處理器決定攔截器如何攔截目標對象
          package com.test.interceptor;

          /**
          * Created by IntelliJ IDEA.
          * User: Administrator
          * Date: 2009-1-9
          * Time: 17:41:08
          */
          import java.lang.reflect.InvocationHandler;
          import java.lang.reflect.Method;

          /**
          * 處理器
          * 在這里將目標對象Target和攔截器Interceptor聯系在了一起
          */
          public class MyHandler implements InvocationHandler{

              /**
               * 目標對象
               * 將來目標對象的方法將被注入interceptor
               */
              private Object object;

              /**
               * 攔截器
               * MyHandler負責將Interceptor這個攔截器注入到傳入的目標對象object
               */
              private Interceptor interceptor = new Interceptor();

              public void setObject(Object object){
                  this.object = object;
              }

              /**
               * 實現InvocationHandler接口的方法
               * @param o 在其上調用方法的代理實例
               * @param method 對應于在代理實例上調用的接口方法的 Method 實例
               * @param objects 包含傳入代理實例上方法調用的參數值的對象數組
               * @return  從代理實例的方法調用返回的值
               * @throws Throwable
               */
              public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
                  Object result = null;
                  interceptor.before();
                  result = method.invoke(object,objects);
                  interceptor.after();
                  return result;
              }
          }

          4、代理對象-用于得到目標對象的代理對象
          package com.test.interceptor;

          /**
          * Created by IntelliJ IDEA.
          * User: Administrator
          * Date: 2009-1-9
          * Time: 17:50:52
          * 代理類
          */
          import java.lang.reflect.Proxy;
          public class MyProxy {
              /**
               * 返回obj的,處理器為MyHandler的代理對象
               * @param obj 目標對象
               * @return  目標對象的代理對象
               */
              public Object getProxy(Object obj){
                  MyHandler myHandler = new MyHandler();
                  myHandler.setObject(obj);
                  return Proxy.newProxyInstance(obj.getClass().getClassLoader(),
                          obj.getClass().getInterfaces(),myHandler);
              }
          }

          5、最后客戶調用代碼
          package com.test.interceptor;

          /**
          * Created by IntelliJ IDEA.
          * User: Administrator
          * Date: 2009-1-9
          * Time: 17:55:45
          * 客戶調用
          */
          public class Client {
              public static void main(String[] args){
                  //目標對象
                  TargetInterface target = new Target();
                  //代理對象
                  MyProxy myProxy = new MyProxy();
                  //通過代理對象得到目標對象的代理
                  TargetInterface proxy = (TargetInterface)myProxy.getProxy(target);
                  //通過目標對象的代理調用目標對象的方法
                  proxy.doSomething();
              }
          }

          //輸出:
          //before 
          //do something
          //after

          將自己的理解寫了注釋,現在只理解到這個程度,像
          Proxy.newProxyInstance
          里面是怎么實現的,還沒深入考究
          繼續努力

          posted @ 2009-01-13 11:26 豬 閱讀(296) | 評論 (0)編輯 收藏
          關于類型轉換還有一種寫法:
          public class ConverterPoint extends StrutsTypeConverter {

          //    @Override
          //    public Object convertValue(Map context, Object value, Class toType) {
          //        if(Point.class == toType){
          //            String[] params = (String[])value;
          //            params = params[0].split(",");
          //            Point point = new Point();
          //            point.setX(Integer.parseInt(params[0]));
          //            point.setY(Integer.parseInt(params[1]));
          //            return point;
          //           
          //        }else if(String.class == toType){
          //            Point point = (Point)value;
          //            return "x=" + point.getX() + ",y=" + point.getY();
          //        }
          //        return super.convertValue(context, value, toType);
          //    }

              @Override
              public Object convertFromString(Map arg0, String[] arg1, Class arg2) {
                  String[] params = arg1;//(String[])value;
                  params = params[0].split(",");
                  Point point = new Point();
                  point.setX(Integer.parseInt(params[0]));
                  point.setY(Integer.parseInt(params[1]));
                  return point;
              }

              @Override
              public String convertToString(Map arg0, Object arg1) {
                  Point point = (Point)arg1;
                  return "x=" + point.getX() + ",y=" + point.getY();
              }

             
          }

          原來是繼承ognl.DefaultTypeConverter類,現在改成繼承org.apache.struts2.util.StrutsTypeConverter類了,看org.apache.struts2.util.StrutsTypeConverter的源碼可知道org.apache.struts2.util.StrutsTypeConverter是個抽象類,并實現了ognl.TypeConverter,org.apache.struts2.util.StrutsTypeConverter中有兩個要實現的方法一個convertFromString,一個convertToString,看方法名字就知道一個從字符串轉換成Object,一個是將Object轉換成String,這樣好像更加清晰

          另外還有一種全局轉換的配置,就是建立一個統一的屬性文件,文件名為xwork-conversion.properties,與struts.xml位于統一目錄下,里面的配置改成了
          com.test.struts2.data.Point=com.test.struts2.action.ConverterPoint
          意思是所有com.test.struts2.data.Point類型的對象都要通過com.test.struts2.action.ConverterPoint來轉換

          posted @ 2009-01-08 13:30 豬 閱讀(284) | 評論 (0)編輯 收藏

          struts2局部類型轉換

          需求為:在頁面的文本框輸入一個x、y坐標,之間用","隔開,Action中有一個Point類型的屬性,Point類里面有兩個字段,分別是x,y,int型,要求當頁面提交時將文本框中的字符串轉換成Point對象,當Action返回到頁面時將Point轉換成字符串顯示

          1、首先做一個jsp用于輸入和提交:point.jsp

          <s:form action="Converter">
          <s:textfield name="point" label="Point"></s:textfield>
          <s:textfield name="point.x" label="Point.X"></s:textfield>
          <s:textfield name="point.y" label="Point.Y"></s:textfield>
          <s:submit label="submit"></s:submit>
          </s:form>

          2、再建一個用于顯示的jsp-converter.jsp

          <body>
          <s:property value="point"/><br>
          <s:property value="point.x"/><br>
          <s:property value="point.y"/><br>
          </body>

          3、Action類 ConverterAction

          public class ConverterAction extends ActionSupport{

          private Point point;

          public Point getPoint() {
          return point;
          }

          public void setPoint(Point point) {
          this.point = point;
          }

          public String execute()throws Exception {
          return "success";
          }
          }

          4、struts.xml

          <action name="Converter"
          class="com.test.struts2.action.ConverterAction">
          <result name="success">/converter.jsp</result>
          <result name="input">/point.jsp</result>
          </action>

          5、轉換類 ConverterPoint

          public class ConverterPoint extends DefaultTypeConverter {

          @Override
          public Object convertValue(Map context, Object value, Class toType) {
          if(Point.class == toType){
          String[] params = (String[])value;
          params = params[0].split(",");
          Point point = new Point();
          point.setX(Integer.parseInt(params[0]));
          point.setY(Integer.parseInt(params[1]));
          return point;

          }else if(String.class == toType){
          Point point = (Point)value;
          return "x=" + point.getX() + ",y=" + point.getY();
          }
          return super.convertValue(context, value, toType);
          }


          }

          6、配置轉換
          在ConverterAction 類的同一目錄下定義屬性文件ConverterAction-conversion.properties,注意文件名,-conversion.properties為固定不變,ConverterAction為Action的名字,文件內容:

          point=com.test.struts2.action.ConverterPoint

          Ok了,這樣就完工了

          posted @ 2009-01-05 13:12 豬 閱讀(292) | 評論 (0)編輯 收藏
          僅列出標題
          共27頁: 上一頁 1 2 3 4 5 6 7 8 9 下一頁 Last 

          公告

          希望有一天

          我能用鼠標雙擊我的錢包

          然后選中一張100元

          按住“ctrl+c”

          接著不停的“ctrl+v”

          嘻嘻~~~笑醒~~~



          導航

          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          統計

          常用鏈接

          留言簿(6)

          隨筆分類(102)

          隨筆檔案(398)

          文章分類

          文章檔案(10)

          有趣網絡

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 万源市| 华宁县| 海淀区| 安乡县| 额济纳旗| 镇坪县| 哈巴河县| 麻城市| 乌审旗| 汝城县| 福贡县| 绥滨县| 都江堰市| 乌海市| 玛曲县| 金溪县| 泌阳县| 玉树县| 葫芦岛市| 海阳市| 呼伦贝尔市| 理塘县| 灌阳县| 平乐县| 望城县| 青阳县| 福清市| 呼和浩特市| 肃南| 个旧市| 溧阳市| 内丘县| 湘乡市| 怀仁县| 新郑市| 南开区| 陆丰市| 唐山市| 枝江市| 安龙县| 霸州市|