从servlet获取对象到flex



我有一个flex web应用程序,它请求一个servlet, servlet返回一个数组列表。我在结果事件中得到字符串形式的数据。我想在伸缩端得到arraycollection。我该怎么做呢?同时请告诉我,是否可以按照我的方式来做?下面是代码

flex结束:

<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 end中得到的警报是:"[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文件,我将其转换成flex arraycollection在java中end和send。这是我可以接近的正确方式吗?请帮助

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

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

我的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 雇员。as类如下。否则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节点添加到WEB-INF/flex文件夹下<service></service>标签下的remoting-config.xml

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

我想现在就这些了。在网上搜索一些关于flash远程控制的好教程。也许我不是一个好老师,哈哈。

相关内容

  • 没有找到相关文章

最新更新