0

我有一个 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 并发送。这是我可以接近的正确方法吗?请帮忙

4

1 回答 1

1

我认为 servlet 不会在这里达到您的目的。由于 servletprintln()将字符串写入输出流。如果将对象传递给println(),则toString()调用该对象的函数并将结果输出到println()。正如@RIAstar 建议的那样,您应该使用 AMF 远程处理。我在这里介绍使用 BlazeDS 使用 AMF 远程处理的步骤。

首先将 servlet 中的所有逻辑转移到某个 POJO 类,并定义一个返回员工列表的函数。

我的 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类。否则ReferenceError: Error #1056: Cannot create property id on Employee,所有 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 端应用程序逻辑以使用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 节点添加到标签下WEB-INF/flex文件夹中的remoting-config.xml中。<service></service>

<destination id="SOTestEmployee">
        <properties>
            <source>com.rit.test.java.TestMain</source>
        </properties>
        <adapter ref="java-object"/>
    </destination>

我想这就是现在的全部。在网上搜索一些关于 Flash 远程处理的好教程。可能我不是一个好老师……哈哈。

于 2013-08-01T10:31:00.800 回答