Flex + Hessian + Spring

這是我第一偏的文章
我是初學 FLEX
如同標題所示我一直在找 FLEX 與 JAVA 溝通的管道
Hessian 我選擇了他,簡單易用且支持很多 client 當然最近也支持了 FLEX
要怎麼上手呢看了 Hessian 網站上的範例 hessian.caucho.com/doc/flash.xtp
有了一點基礎但是要實做一個 HessianServlet 總覺得不是很好用
想說我用 spring 用習慣了來看看有沒有解決的方法想到 spring 的文檔 Remoting and web services using Spring
static.springframework.org/spring/docs/2.5.x/reference/remoting.html
剛好有對 hessian 支持
所以馬上來試用 首先當然是設定一些文檔好讓 spring remoting 能夠活起來

web.xml 代码
 
  1. < servlet >   
  2.     < servlet-name > remoting </ servlet-name >   
  3.     < servlet-class > org.springframework.web.servlet.DispatcherServlet </ servlet-class >   
  4.     < load-on-startup > 1 </ load-on-startup >   
  5. </ servlet >   
  6.   
  7. < servlet-mapping >   
  8.     < servlet-name > remoting </ servlet-name >   
  9.     < url-pattern > /remoting/* </ url-pattern >   
  10. </ servlet-mapping >   


上面的設置是必須的啦通過 servlet 的代理可直接呼叫你的 service
記住 servlet-name 與你下面檔案命名有關當然你也可以自己設定你喜歡對應的文檔名
我喜歡用 default 的值所以我的
servlet-name 為 remoting, spring 到時會去抓取
WEB-INF/remoting-servlet.xml 讀取你設定的 bean

xml 代码
 
  1. < bean   id = "accountService"   class = "example.AccountServiceImpl" >   
  2.   < property   name = "accountDao"   ref = "accountDao" />   
  3. </ bean >   
  4.   
  5. < bean   name = "/AccountService"   class = "org.springframework.remoting.caucho.HessianServiceExporter" >   
  6.   < property   name = "service"   ref = "accountService" />   
  7.   < property   name = "serviceInterface"   value = "example.AccountService" />   
  8. </ bean >   



上面的 /AccountService 就是映射你要訪問的 url 當然你的 url 不是只 /AccountService 還要加上你 servlet-mapping 的 url-pattern /remoting/*
所以對應的 url 就是 /remoting/AccountService 這就是我們 flex 端要連上的位址
當然你也可以簡化你的 url-pattern 為 /* ,那 URL 就是只對應到 /AccountService
至於 flex 端要怎麼連上來呢 ?

xml 代码
 
  1. < mx:Application   
  2.   xmlns:mx = "http://www.adobe.com/2006/mxml"   xmlns:word = "*"   
  3.   implements = "mx.rpc.IResponder" >   
  4.   
  5.   < mx:Script >   
  6.     < [CDATA[  
  7.       import hessian.client.HessianService;  
  8.   
  9.       import mx.rpc.AsyncToken;  
  10.       import mx.rpc.events.ResultEvent;  
  11.   
  12.       private var service:HessianService  =  new  HessianService("remoting/AccountService");  
  13.   
  14.       private function getAccount():void  
  15.       {  
  16.         var token:AsyncToken  =  service .getAccount.send(va.text);  
  17.         token.addResponder(this);  
  18.       }  
  19.   
  20.       public function result(data:Object):void  
  21.       {  
  22.          var event : ResultEvent  =  data  as ResultEvent;  
  23.          uplogDataGrid.dataProvider  =  event .result as Array;  
  24.       }  
  25.   
  26.       public function fault(info:Object):void  
  27.       {  
  28.   
  29.       }  
  30.     ]]>   
  31.   </ mx:Script >   
  32.   < mx:TextInput   x = "100"   y = "90"   id = "va"   text = "" />   
  33.   < mx:Button   x = "260"   y = "90"   label = "Button"   click = "getAccount()"   />   
  34.   < mx:DataGrid   id = "aDataGrid"   x = "10"   y = "120"   width = "99%"   />   
  35. </ mx:Application >   


其實很簡單只要使用 HessianService 去作遠程呼叫即可
先創造一個 HessianService 實例此實例當然要指向你要呼叫的遠端 sevice 的 url new  HessianService("remoting/AccountService"); 
然後用其 getOperation("getAccount").send();
以上面為例為 service.getOperation("getAccount").send(va.text);
也可以寫成
service.getAccount.send(va.text);
getAccount 當然是你要呼叫遠端 AccountService 的 getAccount( String name );

以下是一些 java
Account.java 代码
  1. public class Account implements Serializable{
  2. private String name;
  3. public String getName();
  4. public void setName(String name) {
  5. this .name = name;
  6. }
  7. }

AccountService. java 代码
  1. public interface AccountService {
  2. public void insertAccount(Account account);
  3. public List getAccounts(String name);
  4. }
AccountServiceImpl. java 代码
  1. public class AccountServiceImpl implements AccountService {
  2. private AccountDao accountDao;
  3. private void setAccountDao(AccountDao) {
  4. this .accountDao = accountDao;
  5. }
  6. public void insertAccount(Account acc) {
  7. // do something...
  8. }
  9. public List getAccounts(String name) {
  10. return accountDao.getAccounts(name);
  11. }
  12. }

java 代码
  1. // the implementation doing nothing at the moment
  2. public class AccountDaoImpl implements AccountDao{
  3. public void insertAccount(Account acc) {
  4. // do something...
  5. }
  6. public List getAccounts(String name) {
  7. List list = new ArrayList();
  8. Account a = new Account();
  9. a.setName(name);
  10. list.add(a);
  11. return list;
  12. }
  13. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值