1. 安装环境:eclipse, tomcat插件, tomcat, blazeds.war。用于部署基于blazeds的项目。
2. 将blazeds.war 文件导入eclipse,并输入项目名称。
3. 建立Server端的Java对象,用于完成服务器端的数据和逻辑操作。本例简单构建了一个EmployeeHelper的类用于返回所有雇员的基本信息,并可以根据客户端的查询请求返回某些雇员的详细信息。Java类写在上面导入的工程中的src目录下。
EmployeeHelper.java:
package com.qilin.hammer.blazeds;
import java.util.*;
public class EmployeeHelper {
private String getPhone(){
Random r = new Random(47);
String phone="";
for(int i=0;i<11;i++)
phone+=String.valueOf(Math.abs(r.nextInt())%10);
return phone;
}
/**
* Get all the employees basic info
* @return
*/
public List<Employee> getEmployees(){
List<Employee> employees = new ArrayList<Employee>();
employees.add(new Employee("hammer",25,"male","engineer"));
employees.add(new Employee("jenny",23,"female","engineer"));
employees.add(new Employee("jacky",27,"male","engineer"));
employees.add(new Employee("tracy",28,"female","manager"));
//System.out.println((ArrayList<Employee>)employees);
return employees;
}
/**
* Use map to transfer the anytype object.
* Get the detail info of employees queried.
* @param employees
* @return
*/
public ArrayList<EmployeeDetail> getDetails(List<Map<String, Object>> employees){
System.out.println(employees);
ArrayList<EmployeeDetail> result = new ArrayList<EmployeeDetail>();
for(Map<String, Object> map:employees){
String name = (String)map.get("name");
int age = (Integer)map.get("age");
String sex = (String)map.get("sexual");
String position = (String)map.get("position");
result.add(new EmployeeDetail(name, age, sex, position, name+"@gmail.com", getPhone(), name+"'s address"));
}
return result;
}
}
EmployeeDetail.java:
package com.qilin.hammer.blazeds;
import java.io.Serializable;
public class EmployeeDetail implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private int age;
private String sexual;
private String position;
//For detail
private String email = "";
private String phone = "";
private String address = "";
public EmployeeDetail(){}
public EmployeeDetail(String name, int age, String sex, String position, String email, String phone, String address){
this.name = name;
this.age = age;
this.sexual =sex;
this.position = position;
this.email = email;
this.phone = phone;
this.address = address;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public void setAge(int age){
this.age=age;
}
public int getAge(){
return this.age;
}
public void setSexual(String sex){
this.sexual=sex;
}
public String getSexual(){
return this.sexual;
}
public void setPosition(String position){
this.position=position;
}
public String getPosition(){
return this.position;
}
public void setEmail(String email){
this.email = email;
}
public String getEmail(){
return this.email;
}
public void setPhone(String phone){
this.phone = phone;
}
public String getPhone(){
return this.phone;
}
public void setAddress(String adr){
this.address = adr;
}
public String getAddress(){
return this.address;
}
}
Employee.java:
package com.qilin.hammer.blazeds;
import java.io.Serializable;
public class Employee implements Serializable{
private static final long serialVersionUID = 1L;
private String name;
private int age;
private String sexual;
private String position;
public Employee(String name, int age, String sex, String position){
this.name = name;
this.age = age;
this.sexual =sex;
this.position = position;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public void setAge(int age){
this.age=age;
}
public int getAge(){
return this.age;
}
public void setSexual(String sex){
this.sexual=sex;
}
public String getSexual(){
return this.sexual;
}
public void setPosition(String position){
this.position=position;
}
public String getPosition(){
return this.position;
}
}
4. 配置flex/remoting-config.xml,指定EmployeeHelper类的位置。
remoting-config.xml:
<destinationid="employeehelper">
<properties>
<source>com.qilin.hammer.blazeds.EmployeeHelper</source>
</properties>
</destination>
5. 将项目export为war文件,直接部署到tomcat的webapp下并启动tomcat。
6. 打开Flex builder新建一个Flex project,命名为BlazedsClient。基本功能如下:
(1) 使用remoteobject组件调用Server端的Java方法getEmployees()去获取所有雇员的基本信息并显示在datagrid中。该方法返回类型为collection,与flex端的 arraycollection相映射,从而可以将返回结果直接作为datagrid的dataprovider。
(2) 通过同样的方法调用Server端的getDetails(list<map>)方法去获取指定雇员(们)的详细信息并显示在datagrid中。该方法的形参类型为collection,用于接收flex中的arraycollection实参,返回类型同样为collection,但在flex端通过对象绑定进行了转换,从而直接得到一个actionscript对象,当然也可以省去这一步而直接将返回结果作为dataprovider。有一个疑问是:我在java中用了map来与flex中的object进行映射,我不知道有没有更加直接的方法。为了从flex传一个自定义的对象类型employee到java,如果简单地在java中用list<employee>来接受flex中employee对象的arraycollection,却不能成功。但有意思的是,从java返回的employeedetail类型的对象却直接可以通过对象绑定转换为as中的相应自定义对象employeedetail,却不需要返回一个map来与flex中的object进行映射。
BlazedsClient.mxml:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="getEmployees()">
<mx:Script>
<![CDATA[
import mx.events.ListEvent;
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
import com.qilin.hammer.blazeds.*;
[Bindable]
private var employeeDetails:ArrayCollection;
private function getEmployees():void
{
userRequest.getEmployees();
}
private function resultHandler(event:ResultEvent):void{
var ac:ArrayCollection = ArrayCollection(event.result);
employeeDetails = new ArrayCollection();
for (var i:int = 0; i<ac.length; i++){
var item:EmployeeDetail = EmployeeDetail(ac.getItemAt(i));
employeeDetails.addItem(item);
}
detailinfo.dataProvider = employeeDetails;
}
private function getDetails(event:ListEvent):void{
var employeesQueried:ArrayCollection = new ArrayCollection();
var selectedItems:Array = basicinfo.selectedItems;
for(var i:int = 0;i<selectedItems.length;i++){
var employee:Object = new Object();
employee.name = selectedItems[i]["name"];
employee.age = selectedItems[i]["age"];
employee.sexual = selectedItems[i]["sexual"];
employee.position = selectedItems[i]["position"];
employeesQueried.addItem(employee);
}
userRequest.getDetails(employeesQueried)
userRequest.addEventListener(ResultEvent.RESULT,resultHandler);
}
]]>
</mx:Script>
<mx:RemoteObject
id="userRequest"
destination="employeehelper"
source="com.qilin.hammer.blazeds.EmployeeHelper"
endpoint="http://localhost:8080/BlazedsServer/messagebroker/amf">
</mx:RemoteObject>
<mx:Canvas>
<mx:VBox>
<mx:DataGrid id="basicinfo" width="50%"
allowMultipleSelection="true"
dataProvider="{userRequest.getEmployees.lastResult}"
change="getDetails(event)"
rowCount="{userRequest.getEmployees.lastResult.length}">
<mx:columns>
<mx:DataGridColumn headerText="Name" dataField="name"/>
<mx:DataGridColumn headerText="Age" dataField="age"/>
<mx:DataGridColumn headerText="Sexual" dataField="sexual"/>
<mx:DataGridColumn headerText="Position" dataField="position"/>
</mx:columns>
</mx:DataGrid>
<mx:DataGrid id="detailinfo" width="1000" rowCount="{employeeDetails.length}">
<mx:columns>
<mx:DataGridColumn headerText="Name" dataField="name"/>
<mx:DataGridColumn headerText="Age" dataField="age"/>
<mx:DataGridColumn headerText="Sexual" dataField="sexual"/>
<mx:DataGridColumn headerText="Position" dataField="position"/>
<mx:DataGridColumn headerText="Email" dataField="email"/>
<mx:DataGridColumn headerText="Phone" dataField="phone"/>
<mx:DataGridColumn headerText="Address" dataField="address"/>
</mx:columns>
</mx:DataGrid>
</mx:VBox>
</mx:Canvas>
</mx:Application>
EmployeeDetail.as:
package com.qilin.hammer.blazeds
{
[Bindable]
[RemoteClass(alias="com.qilin.hammer.blazeds.EmployeeDetail")]
public class EmployeeDetail
{
public var name:String;
public var age:Number;
public var sexual:String;
public var position:String;
public var email:String;
public var phone:String;
public var address:String;
}
}
总结:
在做这个demo时遇到一些问题,有这样几点需要注意:
1. 为了将java返回的对象直接拷贝构造为as中的对象,要保证as中的对象没有自定义的构造函数,只需要声明绑定即可,同时需要将所有成员属性设为public。
2. 需要指明remoteobject的endpoint属性。