我有一个 flex Web 应用程序,它请求一个 servlet,而 servlet 发回一个数组列表。我在结果事件中以字符串形式获取数据。我想在 flex 端获取 arraycollection。我该怎么做。同时让我知道,可以像我那样做吗?下面是代码
弯曲端:
<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 端的对象类:
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 并发送。这是我可以接近的正确方法吗?请帮忙