{
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
{
//寫標(biāo)題
for (int i = 0; i < this.dataGridView3.ColumnCount; i++)
{
if (i > 0)
{
str += "\t";
}
str += this.dataGridView3.Columns[i].HeaderText;
}
sw.WriteLine(str);
//寫內(nèi)容
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");
}
-
加強(qiáng)的用戶體驗(yàn):VS2010帶來(lái)了新用戶界面,讓工作更專注。
-
便捷的SharePoint開(kāi)發(fā):對(duì)SharePoint開(kāi)發(fā)更完善的支持,讓SharePoint開(kāi)發(fā)不再痛苦。
-
應(yīng)用程序生命周期管理(ALM):把項(xiàng)目角色更緊密地集成到整個(gè)應(yīng)用程序生命周期中。
-
更佳的Web應(yīng)用開(kāi)發(fā):對(duì)JavaScript的加強(qiáng),Silverlight開(kāi)發(fā)的完整支持,更容易部署。
-
云應(yīng)用程序開(kāi)發(fā):集成Azure開(kāi)發(fā)包,無(wú)縫進(jìn)行云應(yīng)用程序的開(kāi)發(fā)。
-
數(shù)據(jù)庫(kù)支持:對(duì)IBM DB2和Oracle更好的支持。
-
并行編程:從運(yùn)行時(shí)到IDE都對(duì)并行編程提供了很好的支持。
-
更高的生產(chǎn)力:代碼上下文可幫助理解現(xiàn)存代碼,對(duì)C++開(kāi)發(fā)體驗(yàn)的提升,Windows 7的支持,Office開(kāi)發(fā)支持的加強(qiáng)。
我開(kāi)始接觸.net是Visual Studio 2003和.net1.1,不由感嘆技術(shù)更新之快,之后微軟出了很多新名詞:WPF、WCF、Silverlight、Linq等等,真是抓瞎了,學(xué)不過(guò)來(lái),現(xiàn)在都2010了
我在頁(yè)面中加了一個(gè)注釋 ,注意結(jié)束標(biāo)簽--和>之間有個(gè)空格,結(jié)果未起作用,結(jié)果可想而知,找了半天才找到
修改html模板
找到
<b:loop values='data:labels' var='label'>
..........
</b:loop>
將<b:loop>標(biāo)簽之間的內(nèi)容改成:
<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>
google了幾個(gè)網(wǎng)頁(yè)提供的方法,官方的方式是在文章中加一個(gè)span,讓當(dāng)在首頁(yè)顯示時(shí)span隱藏,這種方式只適合以后新增文章,以前的舊文章均沒(méi)有加入這個(gè)span,就不好使
想到blogger這么高度自定義,那么用jquery實(shí)現(xiàn)吧
經(jīng)過(guò)一個(gè)晚上的實(shí)驗(yàn),終于出結(jié)果了
修改html模板,在<head>下面加入
<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>
經(jīng)測(cè)試,IE7中會(huì)提示:是否停止運(yùn)行腳本?此頁(yè)面的腳本造成Internet Explorer 運(yùn)行速度減慢,如果繼續(xù)運(yùn)行,您的計(jì)算機(jī)將可能停止響應(yīng)
這個(gè).truncate的第一個(gè)參數(shù)設(shè)的大小也有關(guān)系,這里設(shè)了500
在FireFox3和Google Chrome中沒(méi)有什么提示,速度還挺快,但會(huì)有顯示問(wèn)題,因?yàn)閼?yīng)用的jquery.truncate框架有瀏覽器兼容問(wèn)題
好長(zhǎng)時(shí)間了,聽(tīng)說(shuō)Google出了個(gè)Google App Engine - Google Code,說(shuō)是可以讓用戶上傳自己的應(yīng)用,但是只支持python,當(dāng)時(shí)就想什么
時(shí)候支持java啦啊,上個(gè)月看新聞就聽(tīng)說(shuō)Google app engine 要開(kāi)始支持java啦,哈哈,喜,到時(shí)候把我的寫的blog傳上去,哈哈
上周,終于知道了Google app engine終于開(kāi)始宣布支持java了,哈哈呼呼,動(dòng)手,到官網(wǎng)一看,沒(méi)有支持java的動(dòng)靜啊,
對(duì),英文頁(yè)面,哈哈,看來(lái)中文的還是慢一拍啊
首先到注冊(cè)一個(gè),竟然還要手機(jī)號(hào)發(fā)注冊(cè)碼,Google了一下 說(shuō)在手機(jī)號(hào)前加 86就OK了,填上手機(jī)號(hào)點(diǎn)按鈕,呦,短信馬
上就來(lái)啦,搞定,注冊(cè)成功。
這下下載SDK,還有eclipse插件,幫助文檔只有英文的,配合Google工具欄的翻譯功能,基本能看懂,哈哈,感覺(jué)一路下
來(lái),英語(yǔ)水平有所上升啊,哈哈
打開(kāi)eclipse,裝上插件,很輕松的建了第一個(gè)應(yīng)用,插件自動(dòng)生成了一個(gè)小實(shí)例,部署上去,驚嘆于Google的GWT
開(kāi)始一直我原來(lái)寫的blog程序,原來(lái)到處找免費(fèi)的空間,jsp的空間不好找,先是找了http://www.eatj.com/,但是沒(méi)24小
時(shí)(好像是)就會(huì)自動(dòng)停止,必須手動(dòng)重啟服務(wù),原來(lái)還堅(jiān)持去上去看看,后來(lái)終于因?yàn)橛幸淮魏苊r(shí)間太長(zhǎng)被注銷了,接下來(lái)就聽(tīng)
說(shuō)了http://www.stax.net/,跟Google的路子差不多,好像還更自定義些,繼續(xù)使用著,搞java朋友可以去看看
因?yàn)間oogle app engine使用Google Account,原來(lái)的用戶管理模塊需要剔除,原來(lái)數(shù)據(jù)庫(kù)使用的是mysql hibernate,現(xiàn)在
Google 使用JDO,數(shù)據(jù)庫(kù)被封裝了,還好,當(dāng)初設(shè)計(jì)是采用了工廠模式,實(shí)現(xiàn)了數(shù)據(jù)庫(kù)訪問(wèn)模塊與業(yè)務(wù)處理模塊的松耦合,很容易
配置數(shù)據(jù)庫(kù)訪問(wèn)模塊的更改,感受到好的設(shè)計(jì)真是為以后修改省很多事啊。
接著說(shuō)部署我的Google app engine,終于改好相關(guān)代碼,準(zhǔn)備上傳了,噩夢(mèng)開(kāi)始了
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、決定轉(zhuǎn)戰(zhàn)Linux..
手上有四個(gè)版本的Linux,Ubuntu,Redhat,F(xiàn)edora,openSUSE,都裝過(guò),后來(lái)感覺(jué)還是Fedora比較好用,我裝的是
Fedora9,F(xiàn)edora10正在下載中
以下Linux安裝配置,僅限于在Fedora9上安裝通過(guò),資料都是通過(guò)Google搜索得來(lái),感謝各位網(wǎng)友,網(wǎng)絡(luò)的力量真是強(qiáng)大。
1、在VMware中裝上Fedora9后,首先安裝VMware-tools,方便與主機(jī)交互
首先需要下載相應(yīng)的kernel-devel.rpm包進(jìn)行安裝
首先查看內(nèi)核版本:uname -r一下,我的是2.6.25-14.fc9.i686,
所以下載kernel-devel-2.6.25-14.fc9.i686.rpm,然后運(yùn)行rpm -ivh kernel-devel-2.6.25-14.fc9.i686.rpm 進(jìn)行安裝. 內(nèi)核安
裝完畢后,需要用這個(gè)命令確定內(nèi)核 C header 的安裝目錄:ls -d /usr/src/kernels/$(uname -r)*/include
安裝內(nèi)核具體步驟如下:
[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時(shí)會(huì)提示: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?
在命令行執(zhí)行:yum install gcc
然后安裝VMware Tools就行了,打開(kāi)菜單“VM -> Install VMware Tools”,然后有光盤自動(dòng)彈出,把里面的源代碼拷貝出來(lái).我的文
件是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驅(qū)動(dòng)時(shí)的一個(gè)bug,在客戶虛擬機(jī)的顯示中,鼠標(biāo)位置可能不正確。直到被更新前,在客戶機(jī)
中添加Option NoAutoAddDevices到/etc/X11/xorg.conf文件的ServerFlags節(jié)中。如果需要,創(chuàng)建這個(gè)節(jié):
Section "ServerFlags"
Option "NoAutoAddDevices"
EndSection
3、第一,安裝JDK
第一,到http://java.sun.com下載最新JDK,當(dāng)前本人下載的是jdk1.6.0_02!下載文件:jdk-6u2-linux-i586-rpm.bin.注意是
rpm.bin的!
第二,給下載回來(lái)的文件增加執(zhí)行權(quán)限:chmod 755 jdk-6u2-linux-i586-rpm.bin.
第三,執(zhí)行文件:./jdk-6u2-linux-i586-rpm.bin.
第四,執(zhí)行文件產(chǎn)生一個(gè)rpm文件,可直接雙擊執(zhí)行也可以在shell下執(zhí)行:rpm -ivh jdk-6u2-linux-i586-rpm.
第五:配置環(huán)境變量,環(huán)境變量配置可在全局文件/etc/profile下修改,這樣所有l(wèi)inux系統(tǒng)的用戶都可以用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
第五,當(dāng)環(huán)境變量修改完后,重起系統(tǒng),在shell下輸入java,看是否輸入相關(guān)JAVA幫助信息,如果有,說(shuō)明已經(jīng)安裝成功!如果沒(méi)有,
檢查一下變量環(huán)境設(shè)置是否有誤!
2,安裝Eclipse,Eclipse不需要安裝,只要解壓縮就行了
運(yùn)行Google app Engine不需要Tomcat
3,安裝tomcat的方法跟安裝eclipse的一樣,也是下一個(gè)tar.gz的文件按安裝eclipse方法和步驟就行了!進(jìn)入tomcat/bin
下./startup.sh,如果在shell出現(xiàn)jdk的相關(guān)信息說(shuō)明已經(jīng)安裝成功啟動(dòng)了,在firefox下打http://localhost:8080/出現(xiàn)tomcat頁(yè)就
大成功了!
在以上的安裝過(guò)程之中,出現(xiàn)了小插曲,就是tomcat找不到JDK,后來(lái)我又到JAVA網(wǎng)站下了一個(gè)JRE回來(lái)安裝,并建JRE_HOME,重起
TOMCAT,成功了!
4、運(yùn)行Google App Engine的應(yīng)用,提示
** 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
停止進(jìn)程命令:
就是kill 比較不錯(cuò),如果kill不了. 加上-9
如#kill -9 1778
注1778 為進(jìn)程pid
pid可以通過(guò)ps aux|grep 服務(wù)名查得
啟動(dòng)Eclipse時(shí),提示錯(cuò)誤:
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)
在網(wǎng)上搜索得知:
eclipse.ini文件加個(gè)參數(shù)
-vmargs
-Dorg.eclipse.swt.browser.XULRunnerPath=
在Fedora9中安裝拼音輸入法
保證Fedora 9聯(lián)網(wǎng)的狀態(tài)下
在application->system tools->terminal 應(yīng)用程序->系統(tǒng)工具->終端
輸入
su 回車
提示輸入root密碼
yum install scim 回車
系統(tǒng)會(huì)自動(dòng)從一個(gè)鏡uy像站點(diǎn)檢索scim,并詢問(wèn)是否下載,選擇y
下載完成后輸入
yum install scim-pinyin 回車
提示和操作如上
然后輸入
scim 回車
(筆者的電腦運(yùn)行到starting SCIM后就沒(méi)有反應(yīng)了,不過(guò)沒(méi)關(guān)系這時(shí)候可以按ctrl+c中斷)
選擇Fedora 9
System->Preference->Personal->Input Method
系統(tǒng)->首選項(xiàng)->個(gè)人->輸入法
選擇啟用新特性,然后選擇里面的SCIM,然后選擇配置SCIM,
在進(jìn)入后的界面中選擇Global Set 全局設(shè)置,在這里設(shè)置激活熱鍵即可,然后重新啟動(dòng)電腦,就可以在Fedora 9下面使用中文輸入
法了 雖然有點(diǎn)麻煩 但畢竟fedora等眾多l(xiāng)inux桌面系統(tǒng)還很年輕,有很多不足之處。
參考資料:
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
訪問(wèn)地址:http://liuspring315.googlepages.com/

java.lang.IllegalStateException: No data type for node: org.hibernate.hql.ast.tree.IdentNode
hibernate查詢時(shí)報(bào)上面的異常,仔細(xì)檢查hql寫的有不對(duì)的地方,我就是把逗號(hào),寫成點(diǎn).
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));
好久沒(méi)寫了,過(guò)了個(gè)年,過(guò)了七天豬的日子,吃了睡睡了吃中間偶爾看看電視,為假期準(zhǔn)備了幾個(gè)電影都沒(méi)看,唉:-(忙,上了幾天班了,怎么感覺(jué)這周過(guò)的這么慢呢,剛剛剛周四
昨兒看了一集struts2
總結(jié)幾點(diǎn)
1、struts.xml中的package標(biāo)簽有個(gè)abstract="true"屬性,如果為true,則這個(gè)package中不能有<action>標(biāo)簽,為true表示這個(gè)package為一個(gè)抽象package,就像java中的接口中不能有方法實(shí)現(xiàn)似的。
2、package標(biāo)簽還有個(gè)namespace屬性,它關(guān)系到這個(gè)package中的action訪問(wèn)路徑,默認(rèn)是"",比如
<action name= "HelloWorld"
class= "com.capinfo.struts2.action.HelloWordAction" >
<result>/HelloWorld.jsp</result>
</action>
</package>
則jsp中則應(yīng)為<form action="/hello/HelloWorld.action" method="post">,如果沒(méi)有找到hello/HelloWorld.action則會(huì)尋找namespace為""的,也就是HelloWorld.action,如果再?zèng)]有,就會(huì)拋出異常。
3、在struts1中可以有多個(gè)struts-config.xml的文件,只要在web.xml配置org.apache.struts.action.ActionServlet時(shí)加到config參數(shù)中,以逗號(hào)分隔,在struts2中也可以有多個(gè)struts.xml形式的文件,這次不需要修改web.xml了,在struts.xml中添加諸如
<include file=""/>就Ok了
4、關(guān)于struts各種參數(shù)的配置信息位于struts2-core.jar/org.apache.struts2/default.properties中,如果要修改里面的配置,有兩種方式,一是在struts.xml中配置,例如<constant name="struts.devMode" value="true" />,二是在classess中建一個(gè)struts.properties,在里面配置比如struts.i18n.encoding=GBK,在struts2-core.jar/org.apache.struts2/default.properties中有一下這句注釋,說(shuō)明了struts.properties將覆蓋default.properties中的配置
### Struts default properties
###(can be overridden by a struts.properties file in the root of the classpath)
###
就總結(jié)這么些先。
public static Class<?> getProxyClass(ClassLoader loader,Class<?>... interfaces)
方法,好長(zhǎng)啊
迂回一下,今兒看struts2的具體攔截器Interceptor怎么配置
配置可比自己寫實(shí)現(xiàn)攔截器容易多了
1、首先寫一個(gè)攔截器類,攔截器類有兩只寫法(目前俺知道的)
一種是顯示com.opensymphony.xwork2.interceptor.Interceptor接口,com.opensymphony.xwork2.interceptor.Interceptor接口有三個(gè)方法destroy()、init()和String intercept(ActionInvocation actionInvocation),跟過(guò)濾器差不多
這里指出的是init初始化方法將在容器啟動(dòng)是調(diào)用這個(gè)方法。
/**
* 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,這是個(gè)抽象類,并實(shí)現(xiàn)了com.opensymphony.xwork2.interceptor.Interceptor接口,分別實(shí)現(xiàn)了init和destroy方法,但什么都沒(méi)做,繼承AbstractInterceptor后,實(shí)現(xiàn)intercept方法就行了,
這里指出的是在intercept方法中執(zhí)行actionInvocation.invoke();執(zhí)行所攔截的action中的方法;
2、攔截器寫完了剩下就是配置了,這里要用到struts.xml的組織結(jié)構(gòu)<struts>中有<package>包的的概念,包與包之間可以繼承extends,就像子類繼承父類一樣,子類將擁有父類的屬性和配置,我們一般都繼承extends="struts-default",而struts-default定義在struts2-core.jar 中的struts-default.xml中,struts-default包中定義了很多struts2提供的攔截器和攔截器棧(攔截器棧可以包含多個(gè)攔截器或攔截器棧),struts2的好多功能都是實(shí)現(xiàn)在這些攔截器中,其中有個(gè)<default-interceptor-ref name="defaultStack"/>標(biāo)簽定義了默認(rèn)的攔截器,如果<action>配置中沒(méi)有攔截器配置,那就調(diào)用默認(rèn)攔截器,如果有攔截器配置,要么同時(shí)加上默認(rèn)攔截器,要么在自己的package中加入設(shè)置默認(rèn)攔截器的標(biāo)簽。
<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>
先看看攔截器的實(shí)現(xiàn)
實(shí)現(xiàn)攔截器有這么幾個(gè)東西構(gòu)成
1、目標(biāo)對(duì)象-將被攔截的對(duì)象,這個(gè)對(duì)象中的方法被調(diào)用是將被攔截
要求目標(biāo)對(duì)象面向接口編程,首先要定義接口
/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2009-1-9
* Time: 17:37:23
* 面向接口編程
* 目標(biāo)對(duì)象的接口
*/
public interface TargetInterface {
public void doSomething();
}
/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2009-1-9
* Time: 17:36:26
* 目標(biāo)對(duì)象
*/
public class Target implements TargetInterface{
public void doSomething(){
System.out.println("do something");
}
}
2、攔截器對(duì)象-里面包含在調(diào)用目標(biāo)對(duì)象前或后要調(diào)用的方法
/**
* 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、處理器-處理器決定攔截器如何攔截目標(biāo)對(duì)象
/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2009-1-9
* Time: 17:41:08
*/
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
/**
* 處理器
* 在這里將目標(biāo)對(duì)象Target和攔截器Interceptor聯(lián)系在了一起
*/
public class MyHandler implements InvocationHandler{
/**
* 目標(biāo)對(duì)象
* 將來(lái)目標(biāo)對(duì)象的方法將被注入interceptor
*/
private Object object;
/**
* 攔截器
* MyHandler負(fù)責(zé)將Interceptor這個(gè)攔截器注入到傳入的目標(biāo)對(duì)象object
*/
private Interceptor interceptor = new Interceptor();
public void setObject(Object object){
this.object = object;
}
/**
* 實(shí)現(xiàn)InvocationHandler接口的方法
* @param o 在其上調(diào)用方法的代理實(shí)例
* @param method 對(duì)應(yīng)于在代理實(shí)例上調(diào)用的接口方法的 Method 實(shí)例
* @param objects 包含傳入代理實(shí)例上方法調(diào)用的參數(shù)值的對(duì)象數(shù)組
* @return 從代理實(shí)例的方法調(diào)用返回的值
* @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、代理對(duì)象-用于得到目標(biāo)對(duì)象的代理對(duì)象
/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2009-1-9
* Time: 17:50:52
* 代理類
*/
import java.lang.reflect.Proxy;
public class MyProxy {
/**
* 返回obj的,處理器為MyHandler的代理對(duì)象
* @param obj 目標(biāo)對(duì)象
* @return 目標(biāo)對(duì)象的代理對(duì)象
*/
public Object getProxy(Object obj){
MyHandler myHandler = new MyHandler();
myHandler.setObject(obj);
return Proxy.newProxyInstance(obj.getClass().getClassLoader(),
obj.getClass().getInterfaces(),myHandler);
}
}
5、最后客戶調(diào)用代碼
/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2009-1-9
* Time: 17:55:45
* 客戶調(diào)用
*/
public class Client {
public static void main(String[] args){
//目標(biāo)對(duì)象
TargetInterface target = new Target();
//代理對(duì)象
MyProxy myProxy = new MyProxy();
//通過(guò)代理對(duì)象得到目標(biāo)對(duì)象的代理
TargetInterface proxy = (TargetInterface)myProxy.getProxy(target);
//通過(guò)目標(biāo)對(duì)象的代理調(diào)用目標(biāo)對(duì)象的方法
proxy.doSomething();
}
}
//輸出:
//before
//do something
//after
將自己的理解寫了注釋,現(xiàn)在只理解到這個(gè)程度,像
Proxy.newProxyInstance
里面是怎么實(shí)現(xiàn)的,還沒(méi)深入考究
繼續(xù)努力
// @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();
}
}
原來(lái)是繼承ognl.DefaultTypeConverter類,現(xiàn)在改成繼承org.apache.struts2.util.StrutsTypeConverter類了,看org.apache.struts2.util.StrutsTypeConverter的源碼可知道org.apache.struts2.util.StrutsTypeConverter是個(gè)抽象類,并實(shí)現(xiàn)了ognl.TypeConverter,org.apache.struts2.util.StrutsTypeConverter中有兩個(gè)要實(shí)現(xiàn)的方法一個(gè)convertFromString,一個(gè)convertToString,看方法名字就知道一個(gè)從字符串轉(zhuǎn)換成Object,一個(gè)是將Object轉(zhuǎn)換成String,這樣好像更加清晰
另外還有一種全局轉(zhuǎn)換的配置,就是建立一個(gè)統(tǒng)一的屬性文件,文件名為xwork-conversion.properties,與struts.xml位于統(tǒng)一目錄下,里面的配置改成了
com.test.struts2.data.Point=com.test.struts2.action.ConverterPoint
意思是所有com.test.struts2.data.Point類型的對(duì)象都要通過(guò)com.test.struts2.action.ConverterPoint來(lái)轉(zhuǎn)換
struts2局部類型轉(zhuǎn)換
需求為:在頁(yè)面的文本框輸入一個(gè)x、y坐標(biāo),之間用","隔開(kāi),Action中有一個(gè)Point類型的屬性,Point類里面有兩個(gè)字段,分別是x,y,int型,要求當(dāng)頁(yè)面提交時(shí)將文本框中的字符串轉(zhuǎn)換成Point對(duì)象,當(dāng)Action返回到頁(yè)面時(shí)將Point轉(zhuǎn)換成字符串顯示
1、首先做一個(gè)jsp用于輸入和提交:point.jsp
<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、再建一個(gè)用于顯示的jsp-converter.jsp
<s:property value="point"/><br>
<s:property value="point.x"/><br>
<s:property value="point.y"/><br>
</body>
3、Action類 ConverterAction
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
class="com.test.struts2.action.ConverterAction">
<result name="success">/converter.jsp</result>
<result name="input">/point.jsp</result>
</action>
5、轉(zhuǎn)換類 ConverterPoint
@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、配置轉(zhuǎn)換
在ConverterAction 類的同一目錄下定義屬性文件ConverterAction-conversion.properties,注意文件名,-conversion.properties為固定不變,ConverterAction為Action的名字,文件內(nèi)容:
point=com.test.struts2.action.ConverterPoint
Ok了,這樣就完工了