我有一个flex web应用程序,它请求servlet和servlet发回一个arraylist。我在结果事件中获取了字符串中的数据。我想在flex端获得arraycollection。我该怎么做同时请告诉我,是否可以按照我的方式完成?下面是代码
flex end:
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.HTTPService;
import mx.utils.ObjectUtil;
protected function callBackEnd(event:MouseEvent):void
{
var params:Object = {name: 'debarshi',password:'banerjee'};
rawFileServlet.send(params);
rawFileServlet.addEventListener(ResultEvent.RESULT,onResult);
}
private function onResult(ev:ResultEvent):void
{
Alert.show(ObjectUtil.toString(ev.result));
}
]]>
</fx:Script>
<fx:Declarations>
<s:HTTPService url="http://192.168.1.66:8484/HelloWorldServlet/TestServlet"
id="rawFileServlet"
method="POST"
showBusyCursor="true"/>
</fx:Declarations>
<s:Button click="callBackEnd(event)" label="Click"/>
</s:Application>
flex end中的对象类:
package
{
[Bindable]
[RemoteClass(alias="com.rit.test.java.model.Employee")]
public class Employee
{
private var id:int;
private var name:String;
private var address:String;
private var salary:int;
public function setId(_id:int):void
{
this.id = _id;
}
public function getId():int
{
return this.id;
}
public function setName(_name:String):void
{
this.name = _name;
}
public function getName():String
{
return this.name;
}
public function setAddress(_address:String):void
{
this.address = _address;
}
public function getAddress():String
{
return this.address;
}
public function setSalary(_sal:int):void
{
this.salary = _sal;
}
public function getSalary():int
{
return this.salary;
}
public function Employee()
{
}
}
}
java servlet类:
package com.rit.test.java;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import flex.messaging.io.ArrayCollection;
import flex.messaging.io.amf.translator.ASTranslator;
import com.rit.test.java.model.Employee;
/**
* Servlet implementation class TestServlet
*/
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private ArrayList<Employee> list = new ArrayList<>();
/**
* @see HttpServlet#HttpServlet()
*/
public TestServlet() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
populateList();
ASTranslator ast = new ASTranslator();
out.println(ast.convert(list, ArrayCollection.class));
out.flush();
}
private void populateList(){
Employee emp = new Employee();
emp.setId(1);
emp.setName("Benay");
emp.setAddress("chinar park");
emp.setSalary(100);
list.add(emp);
emp = new Employee();
emp.setId(2);
emp.setName("Debarghya");
emp.setAddress("baguihati");
emp.setSalary(70);
list.add(emp);
emp = new Employee();
emp.setId(3);
emp.setName("Debarshi");
emp.setAddress("garia");
emp.setSalary(60);
list.add(emp);
emp = new Employee();
emp.setId(4);
emp.setName("Tamal");
emp.setAddress("howrah");
emp.setSalary(50);
list.add(emp);
emp = new Employee();
emp.setId(5);
emp.setName("Sanket");
emp.setAddress("keshtopur");
emp.setSalary(40);
list.add(emp);
}
}
object class in java end:
package com.rit.test.java.model;
public class Employee {
private int id;
private String name;
private String address;
private int salary;
public void setId(int _id)
{
this.id = _id;
}
public int getId()
{
return this.id;
}
public void setName(String _name)
{
this.name = _name;
}
public String getName()
{
return this.name;
}
public void setAddress(String _address)
{
this.address = _address;
}
public String getAddress()
{
return this.address;
}
public void setSalary(int _sal)
{
this.salary = _sal;
}
public int getSalary()
{
return this.salary;
}
}
我在flex端得到的警报是:
“[com.rit.test.java.model.Employee@1ae209,com.rit.test.java.model.Employee@edd06a,com.rit.test.java.model.Employee@10df737,com.rit.test。 java.model.Employee@1e17d0b,com.rit.test.java.model.Employee @ d89588]
“
我在我的java lib文件夹中添加了五个blaze DS .jar文件(flex-messaging-common,flex-messaging-core,flex-messaging-opt,flex-messaging-proxy,flex-messaging-remoting)。使用这五个.jar文件,我在java端转换为flex arraycollection并发送。这是我能接近的正确方法吗?请帮忙
答案 0 :(得分:1)
我认为servlet不会满足您的需求。当servlet的println()
将字符串写入输出流时。如果将对象传递给println()
,则调用该对象的toString()
函数,并将结果输出到println()
。由于@RIAstar建议你应该使用AMF远程处理。我在这里介绍使用BlazeDS进行AMF远程处理的步骤。
首先将servlet中的所有逻辑转移到某个POJO类,并定义一个返回Employees列表的函数。
我的POJO类名为 TestMain.java
package com.rit.test.java;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.rit.test.java.model.Employee;
public class TestMain {
private ArrayList<Employee> list = new ArrayList<Employee>();
public TestMain()
{
populateList();
}
public List<Employee> getEmployeeList()
{
return list;
}
private void populateList(){
Employee emp = new Employee();
emp.setId(1);
emp.setName("Benay");
emp.setAddress("chinar park");
emp.setSalary(100);
list.add(emp);
emp = new Employee();
emp.setId(2);
emp.setName("Debarghya");
emp.setAddress("baguihati");
emp.setSalary(70);
list.add(emp);
emp = new Employee();
emp.setId(3);
emp.setName("Debarshi");
emp.setAddress("garia");
emp.setSalary(60);
list.add(emp);
emp = new Employee();
emp.setId(4);
emp.setName("Tamal");
emp.setAddress("howrah");
emp.setSalary(50);
list.add(emp);
emp = new Employee();
emp.setId(5);
emp.setName("Sanket");
emp.setAddress("keshtopur");
emp.setSalary(40);
list.add(emp);
}
}
您的 Employee.java 就是这样。什么都没有改变。
package com.rit.test.java.model;
public class Employee {
private int id;
private String name;
private String address;
private int salary;
public void setId(int _id)
{
this.id = _id;
}
public int getId()
{
return this.id;
}
public void setName(String _name)
{
this.name = _name;
}
public String getName()
{
return this.name;
}
public void setAddress(String _address)
{
this.address = _address;
}
public String getAddress()
{
return this.address;
}
public void setSalary(int _sal)
{
this.salary = _sal;
}
public int getSalary()
{
return this.salary;
}
}
更改您的Flex Employee.as 类,如下所示。否则,所有Employee属性都会出现ReferenceError: Error #1056: Cannot create property id on Employee
错误。
package
{
[Bindable]
[RemoteClass(alias="com.rit.test.java.model.Employee")]
public class Employee
{
public var id:int;
public var name:String;
public var address:String;
public var salary:int;
public function Employee()
{
}
}
}
将您的Flex-end 应用逻辑更改为使用RemoteObject
,如下所示。
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.controls.DataGrid;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.HTTPService;
import mx.utils.ObjectUtil;
protected function callBackEnd(event:MouseEvent):void
{
ro.getEmployeeList();
}
private function onResult(ev:ResultEvent):void
{
var ac:ArrayCollection = ev.result as ArrayCollection;
trace(ac);
for each(var emp:Employee in ac)
trace(ObjectUtil.toString(emp))
}
private function onFault(evt:FaultEvent):void
{
Alert.show(""+evt.message);
}
]]>
</fx:Script>
<fx:Declarations>
<!--<s:HTTPService url="http://192.168.1.66:8484/HelloWorldServlet/TestServlet"
id="rawFileServlet"
method="POST"
showBusyCursor="true"/>-->
<s:RemoteObject id="ro" destination="SOTestEmployee"
result="onResult(event)" fault="onFault(event)" >
<s:channelSet>
<s:ChannelSet>
<s:channels>
<s:AMFChannel url="http://localhost:8400/blazeds/messagebroker/amf" />
</s:channels>
</s:ChannelSet>
</s:channelSet>
</s:RemoteObject>
</fx:Declarations>
<s:Button click="callBackEnd(event)" label="Click"/>
</s:Application>
现在将所有java * .class文件放在WEB-INF/classes
文件夹中,并附上其包结构。
现在将以下XML节点添加到<service></service>
标记下 WEB-INF / flex 文件夹中的 remoting-config.xml 。
<destination id="SOTestEmployee">
<properties>
<source>com.rit.test.java.TestMain</source>
</properties>
<adapter ref="java-object"/>
</destination>
我认为现在一切都好。在网上搜索关于flash remoting的一些很好的教程。可能我不是一个好老师......哈哈。