没啥好说的,就是一个表结构的对象化表述,不过有两个问题。
1、既然牵涉到跨语言的对象传输为什么没实现序列化接口?
为了让自己和大家了解,昨天我专门请教了据说是Sun公司还是IBM公司的名叫toad的达人,解惑曰(未找到确凿证据,仅仅是技术层面的推测):不实现序列化接口是因为BlazeDS在进行序列化的时候并未使用Java自己的机制。如果各位有更好的解释,尤其是看了BlazeDS源码的同学请过来share。
2、BlazeDS官方文档所要求成员变量必须为public(参看http://livedocs.adobe.com/blazeds/1/blazeds_devguide/serialize_data_3.html,这些白纸黑字写着:”Private properties, constants, static properties, and read-only properties, and so on, are not serialized. “)?
的确,声明为private的成员变量也不会出问题,这个问题我想是因为官方文档滞后的原因,另外就算是私有成员明显可以通过反射获取其值。
接下来是接口“ListDAO.java”,注意,按照规范,暴露给Flex调用的不能包含以下保留的方法名:
getOperation()
hasOwnProperty()
initialized()
isPrototypeOf()
logout()
propertyIsEnumerable()
setCredentials()
setPropertyIsEnumerable()
setRemoteCredentials()
toString()
valueOf()
如果你不小心用到了,可以参考BlazeDS官方文档“RemoteObject component”章节,看如何解决。getData()方法是供Flex端调用的,传入参数是Flex构造的FlexGroup对象实例:
import java.util.List;
/**
* DAO Interface
* @author rosen jiang
* @since 2009/06/21
*
*/
public interface ListDAO {
/**
* .
*/
public List<FlexGroup> getData(FlexGroup fg);
}
然后是实现类“ListDAOImpl.java”,该类利用了JdbcDaoSupport的模板方法来处理JDBC操作:
package org.rosenjiang.flex;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
/**
* DAO Impl
* @author rosen jiang
* @since 2009/06/21
*
*/
public class ListDAOImpl extends JdbcDaoSupport implements ListDAO {
/**
* .
*/
public List<FlexGroup> getData(FlexGroup fg){
//test properties
System.out.println(fg.getGroupName());
System.out.println(fg.getMember1());
System.out.println(fg.getMember2());
System.out.println(fg.getMember3());
System.out.println(fg.getMember4());
System.out.println(fg.getMember5());
//Jdbc Template
JdbcTemplate jt = this.getJdbcTemplate();
List<FlexGroup> list = jt.query("select * from flex_group",
new RowMapper() {
public FlexGroup mapRow(ResultSet rs, int rowNum) throws SQLException {
FlexGroup fg = new FlexGroup();
fg.setGroupName(rs.getString("group_name"));
fg.setMember1(rs.getString("member1"));
fg.setMember2(rs.getString("member2"));
fg.setMember3(rs.getString("member3"));
fg.setMember4(rs.getString("member4"));
fg.setMember5(rs.getString("member5"));
return fg;
}
});
return list;
}
}
开始写“web.xml”文件,注意,由于使用了Spring BlazeDS Integration,所以内容和单纯使用BlazeDS不一样,以往通过messagebroker获取的请求,现在交由Spring处理了:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all /messagbroker requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/messagebroker/*</url-pattern>
</servlet-mapping>
</web-app>
最后是“applicationContext.xml”:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:flex="http://www.springframework.org/schema/flex"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/flex
http://www.springframework.org/schema/flex/spring-flex-1.0.xsd">
<!-- Dada Source -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"/>
<property name="url" value="jdbc:jtds:sqlserver://localhost:1433/cfd"/>
<property name="username" value="sa"/>
<property name="password" value="123456"/>
</bean>
<!-- Message Broker -->
<flex:message-broker services-config-path="/WEB-INF/flex/services-config.xml"/>
<!-- List Data Bean -->
<bean id="listDAO" class="org.rosenjiang.flex.ListDAOImpl">
<property name="dataSource">
<ref bean="dataSource"></ref>
</property>
<flex:remoting-destination/>
</bean>
</beans>
这里引入了flex命名空间,拜Spring BlazeDS Integration所赐,仅仅需要一个“<flex:remoting-destination/>”节点,就能让这个“bean”暴露给远端Flex调用。另外还需要配置“message-broker”,来指定BlazeDS配置文件。以上“web.xml”和“applicationContext.xml”相关配置可参考“Configuring and Using the BlazeDS MessageBroker with Spring”章节,到此,Java部分代码就完成了。
Flex部分就两个文件,首先是和Java相对应的“FlexGroup.as”:
package{
[Bindable]
[RemoteClass(alias="org.rosenjiang.flex.FlexGroup")]
public class FlexGroup{
public var groupName:String;
public var member1:String;
public var member2:String;
public var member3:String;
public var member4:String;
public var member5:String;
}
}
利用[RemoteClass(alias="")]元数据映射Java端的FlexGroup对象,注意这里的成员名一定要一致,详情可参考“Explicitly mapping ActionScript and Java objects”章节。
最后一个是“JavaFlexMapping.mxml”文件,也就是我们的主程序界面:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" backgroundColor="black" fontSize="12"
verticalAlign="middle" horizontalAlign="center" color="#000506" creationComplete="init()">
<mx:RemoteObject id="lo" destination="listDAO" result="handleResult(event)" fault="handleFault(event)">
<mx:channelSet>
<mx:ChannelSet>
<mx:channels>
<mx:AMFChannel
uri="http://127.0.0.1:8080/JavaFlexMapping/messagebroker/amf"/>
</mx:channels>
</mx:ChannelSet>
</mx:channelSet>
</mx:RemoteObject>
<mx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
import mx.controls.Alert;
import mx.collections.ArrayCollection;
var fg:FlexGroup;
var ac:ArrayCollection;
private function init():void{
//create FlexGroup instance for remote DAO
fg = new FlexGroup();
fg.groupName = "测试组";
fg.member1 = "成员1";
fg.member2 = "成员2";
fg.member3 = "成员3";
fg.member4 = "成员4";
fg.member5 = "成员5";
}
private function handleResult(event:ResultEvent):void{
ac = ArrayCollection(event.result);
dg.dataProvider = ac;
}
private function handleFault(event:FaultEvent):void{
Alert.show(event.fault.message, "出错了");
}
]]>
</mx:Script>
<mx:Panel height="364" width="592" title="Java Flex对象透明映射非常好的实践" horizontalAlign="center" verticalAlign="middle">
<mx:VBox>
<mx:ApplicationControlBar dock="true" width="551">
<mx:Button label="赶快点我 我就给数据" click="lo.getData(fg)"/>
</mx:ApplicationControlBar>
<mx:DataGrid height="255" width="551" id="dg">
<mx:columns>
<mx:DataGridColumn headerText="组名" dataField="groupName"/>
<mx:DataGridColumn headerText="成员一" dataField="member1"/>
<mx:DataGridColumn headerText="成员二" dataField="member2"/>
<mx:DataGridColumn headerText="成员三" dataField="member3"/>
<mx:DataGridColumn headerText="成员四" dataField="member4"/>
<mx:DataGridColumn headerText="成员五" dataField="member5"/>
</mx:columns>
</mx:DataGrid>
</mx:VBox>
</mx:Panel>
</mx:Application>
在这里引入了“RemoteObject”对象,并使用运行时注册机制指定AMFChannel,详情可参考“Accessing dynamic components with a Flex client application”章节。Flex程序的运行步骤是这样的:当界面都创建完毕后,执行init()方法构造FlexGroup对象实例并填充其成员;程序进入监听状态,当点击“快点我我就给数据”的按钮后,通过“RemoteObject”实例调用Java端的getData()方法,并传入FlexGroup对象实例供Java端测试;最后由于注册了异步的handleResult()方法,所以当Java端返回List对象实例后立即转换为ArrayCollection数据类型,并填充到DataGrid进行显示。运行截图如下: