访问Flex BlazeDS中的Java对象字段

时间:2013-03-12 14:42:02

标签: java flex object field blazeds

我正在使用BlazeDS将Flex与Java连接。我在尝试访问Flex

中的java对象字段时遇到了麻烦

这是我的Java类路径:

package intetech;

public class Path {

    public Path(){

    }
    public Path(double x1, double y1, double x2, double y2){
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
    }

    public double getX1(){
        return x1;
    }
    public double getY1(){
        return y1;
    }
    public double getX2(){
        return x2;
    }
    public double getY2(){
        return y2;
    }

    private double x1;
    private double y1;
    private double x2;
    private double y2;
}

Java类PathsService,它在getPaths方法中返回Path的ArrayList:

package intetech;

import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class PathsService {
    public PathsService(){}

    public List<Path> getPaths(){
        File svgFile = new File("D:\\work\\blazedsTurnkey\\blazeds-turnkey-4.0.0.14931\\tomcat\\webapps\\blazeds\\WEB-INF\\classes\\intetech\\test.svg");
        try{
            SvgParser svgp = new SvgParser(svgFile);
            listOtr = svgp.getList();
        } catch (SAXException ex) {

        } catch (ParserConfigurationException ex) {

        } catch (IOException ex) { }

        return listOtr;
    }

    private static List<Path> listOtr;
}

所以我尝试在flex中获取ArrayList中一条路径的坐标。  Main.mxml:

  <fx:Declarations>
    <!--setup the connection to the Java class-->
    <mx:RemoteObject
        id="PathsService"
        destination="pathsservice"
        showBusyCursor="true"
        >

        <mx:method name="getPaths" 
                   result="resultGetPaths(event)" 
                   fault="faultGetPaths(event)"/>



    </mx:RemoteObject>
</fx:Declarations>



<fx:Script>
        import mx.collections.ArrayCollection;
        import mx.controls.Alert;
        import mx.rpc.events.FaultEvent;
        import mx.rpc.events.ResultEvent;
        import mx.utils.ObjectUtil;



        private var aPath:Path ;
        private var pathsArrCol:ArrayCollection;
        private var otr:Path;


        public function resultGetPaths(event:ResultEvent):void {

            pathsArrCol = event.result as ArrayCollection;
            **otr = pathsArrCol.getItemAt(0) as Path;
            Alert.show(ObjectUtil.toString(otr.getX1()));** 
        } 


        private function faultGetPaths(event:FaultEvent):void{

            Alert.show( ObjectUtil.toString(event.fault) );


        }

</fx:Script>

和Path.as文件:

package
{
    [RemoteClass(alias="intetech.Path")]

[Bindable]
public class Path
{

    private var ax1:Number;
    private var ay1:Number;
    private var ax2:Number; 
    private var ay2:Number;




    public function getX1():Number {
        return ax1;
    }
    public function getY1():Number {
        return ay1;
    }
    public function getX2():Number {
        return ax2;
    }
    public function getY2():Number {
        return ay2;
    }



public function setX1(data:Number): void{
        ax1 = data;
    }
    public function setY1(data:Number): void{
        ax1 = data;
    }
    public function setX2(data:Number): void{
        ax1 = data;
    }
    public function setY2(data:Number): void{
        ax1 = data;
    }

}
}

运行此应用程序时,我有NaN结果,请帮助

1 个答案:

答案 0 :(得分:1)

您的flex对象中没有任何公共setter / getter:

package {

    [RemoteClass(alias="intetech.Path")]
    [Bindable]
    public class Path {

        private var ax1:Number;
        private var ay1:Number;
        private var ax2:Number; 
        private var ay2:Number;

        public function get x1():Number {
            return ax1;
        }

        public function get y1():Number {
            return ay1;
        }

        public function get x2():Number {
            return ax2;
        }

        public function get y2():Number {
            return ay2;
        }

        public function set x1(data:Number):void {
            ax1 = data;
        }

        public function set y1(data:Number):void {
            ay1 = data;
        }

        public function set x2(data:Number):void {
            ax2 = data;
        }

        public function set y2(data:Number):void {
            ay2 = data;
        }

    }
}

结果是BlazeDS无法使用Java中的序列化数据正确初始化新的Flex对象。