這是我第一偏的文章
我是初學 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 能夠活起來
- < servlet >
- < servlet-name > remoting </ servlet-name >
- < servlet-class > org.springframework.web.servlet.DispatcherServlet </ servlet-class >
- < load-on-startup > 1 </ load-on-startup >
- </ servlet >
- < servlet-mapping >
- < servlet-name > remoting </ servlet-name >
- < url-pattern > /remoting/* </ url-pattern >
- </ servlet-mapping >
上面的設置是必須的啦通過 servlet 的代理可直接呼叫你的 service
記住 servlet-name 與你下面檔案命名有關當然你也可以自己設定你喜歡對應的文檔名
我喜歡用 default 的值所以我的 servlet-name 為 remoting, spring 到時會去抓取
WEB-INF/remoting-servlet.xml 讀取你設定的 bean
- < bean id = "accountService" class = "example.AccountServiceImpl" >
- < property name = "accountDao" ref = "accountDao" />
- </ bean >
- < bean name = "/AccountService" class = "org.springframework.remoting.caucho.HessianServiceExporter" >
- < property name = "service" ref = "accountService" />
- < property name = "serviceInterface" value = "example.AccountService" />
- </ bean >
上面的 /AccountService 就是映射你要訪問的 url 當然你的 url 不是只 /AccountService 還要加上你 servlet-mapping 的 url-pattern /remoting/*
所以對應的 url 就是 /remoting/AccountService 這就是我們 flex 端要連上的位址
當然你也可以簡化你的 url-pattern 為 /* ,那 URL 就是只對應到 /AccountService
至於 flex 端要怎麼連上來呢 ?
- < mx:Application
- xmlns:mx = "http://www.adobe.com/2006/mxml" xmlns:word = "*"
- implements = "mx.rpc.IResponder" >
- < mx:Script >
- < [CDATA[
- import hessian.client.HessianService;
- import mx.rpc.AsyncToken;
- import mx.rpc.events.ResultEvent;
- private var service:HessianService = new HessianService("remoting/AccountService");
- private function getAccount():void
- {
- var token:AsyncToken = service .getAccount.send(va.text);
- token.addResponder(this);
- }
- public function result(data:Object):void
- {
- var event : ResultEvent = data as ResultEvent;
- uplogDataGrid.dataProvider = event .result as Array;
- }
- public function fault(info:Object):void
- {
- }
- ]]>
- </ mx:Script >
- < mx:TextInput x = "100" y = "90" id = "va" text = "" />
- < mx:Button x = "260" y = "90" label = "Button" click = "getAccount()" />
- < mx:DataGrid id = "aDataGrid" x = "10" y = "120" width = "99%" />
- </ 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
- public class Account implements Serializable{
- private String name;
- public String getName();
- public void setName(String name) {
- this .name = name;
- }
- }
- public interface AccountService {
- public void insertAccount(Account account);
- public List getAccounts(String name);
- }
- public class AccountServiceImpl implements AccountService {
- private AccountDao accountDao;
- private void setAccountDao(AccountDao) {
- this .accountDao = accountDao;
- }
- public void insertAccount(Account acc) {
- // do something...
- }
- public List getAccounts(String name) {
- return accountDao.getAccounts(name);
- }
- }
- // the implementation doing nothing at the moment
- public class AccountDaoImpl implements AccountDao{
- public void insertAccount(Account acc) {
- // do something...
- }
- public List getAccounts(String name) {
- List list = new ArrayList();
- Account a = new Account();
- a.setName(name);
- list.add(a);
- return list;
- }
- }