Announcement Announcement Module
Collapse
No announcement yet.
pass object from flex to spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • pass object from flex to spring

    hi guys

    I am using flex in UI with java backend (using spring blazeds integration)


    In my backend i have my object as

    Public class Person extends Base
    {
    String personrace;
    String personage;
    String email;
    set();
    get();
    }

    Public class Base{
    String id;
    String name;
    set();
    get();
    }

    basically its a Person object with person related fields.

    Now in my mxml i want to perform a save operation.

    how do i go about it ?
    this is what i have so far

    <fx:Script>
    <![CDATA[
    import mx.rpc.events.ResultEvent;
    protected function save():void
    {
    personService.save(PERSON OBJECT); //i want to pass the Person object here to the spring service
    ]]>
    </fx:Script>

    <fx : Declarations>
    <s:CallResponder/>
    <services:PersonService id="personService">
    <services:channelSet>
    <!--connect to backend using channel -->
    <s:ChannelSet>
    <s:AMFChannel uri="http://localhost:9080/userweb/messagebroker/amf"
    <s:ChannelSet>
    </services:channelSet>
    </services:Personservice>
    </fx : Declarations>


    <mx:Form x="22" y="10" width="300">
    <mx:FormItem>
    <s:Label text="Person name" />
    <s:TextInput id="personname"/>
    </mx:FormItem>
    <mx:FormItem>
    <s:Label text="Person id"/>
    <s:TextInput id="id"/>
    </mx:FormItem>
    <mx:FormItem>
    <s:Label text="Email Address" />
    <s:TextInput id="email"/>
    </mx:FormItem>
    <s:Button label="Save" click="save()"/>
    </mx:Form>

    PersonService is my service class on the spring side. but how do i pass the Person object from the above code to my personService.save method?

    thanks
    Last edited by jaggernat; Feb 10th, 2011, 04:07 PM.

  • #2
    Something like following should work

    Code:
    		var personServiceRO:RemoteObject = new RemoteObject();
    		
    		personServiceRO.destination = "personService";
    		
    		var token:AsyncToken = personServiceRO.save(new Person("Your name"));
    		token.addResponder(
    			new AsyncResponder(
    				function(event:ResultEvent, token:Object = null):void {
    					// Result
    				},
    				function(event:FaultEvent, token:Object = null):void {
    					// Fault
    				}
    			)
    		);

    Comment


    • #3
      ok thats cool

      I was thinking i have to define a Person.as file in the flex side and map the fields from person.as to Person.java. something like..


      [Bindable]
      public final class Person
      {
      public var id:String;
      public var name:String;
      public var personrace:String;
      public var personage:String;
      public var email:String;

      //the above fields map to the fields in Person.java file.
      ...

      }
      so dont i need Person.as file ?
      I was reading in some link that i need the .as file for serialization and deserialization.

      Also i dint understnad the line >>var token:AsyncToken = personServiceRO.save(new Person("Your name"));
      Person("Your name") --> Person is the Person.java file and what is "Your name" ?

      thanks for the response.
      Last edited by jaggernat; Feb 10th, 2011, 05:22 PM.

      Comment


      • #4
        I was thinking i have to define a Person.as file in the flex side
        You need a Person.as in flex-side

        map the fields from person.as to Person.java
        Your Person.as,

        Code:
        [RemoteClass(alias="com.yourapp.domain.Person")]
        public class Person
        {
        }
        Red colored class is the fully qualified name of your Person.java class. This metadata tag will create an ActionScript object that maps directly to the Java object.

        Person is the Person.java file and what is "Your name" ?
        Person is the actionscript class. I just assumed you might have a constructor with name as a argument

        Code:
        [RemoteClass(alias="com.yourapp.domain.Person")]
        public class Person
        {
               public function Person(name:String){}
        }

        Comment


        • #5
          ok thanks.

          I was thinking if we can reuse the Person java class in the flex side aswell (reusing the model rather than creating a second Model object (person.as) on the flex side which is redundant). But i guess thats the standard way.

          Comment


          • #6
            I am not able to get it to work, not sure why, here is my code
            i have in my mxml:

            protected function save():void
            {

            var personServiceRO:RemoteObject = new RemoteObject();
            personServiceRO.destination = "personService";
            var token:AsyncToken = personServiceRO.save(new Person(id:Number)); -- Getting error here :: Expecting rightparen before colon
            token.addResponder(
            new AsyncResponder(
            function(event:ResultEvent, token:Object = null):void {
            // Result
            },
            function(event:FaultEvent, token:Object = null):void {
            // Fault
            }
            )
            );


            <fx : Declarations>
            <s:CallResponder/>
            <services:PersonService id="personService">
            <services:channelSet>
            <!--connect to backend using channel -->
            <s:ChannelSet>
            <s:AMFChannel uri="http://localhost:9080/userweb/messagebroker/amf"
            <s:ChannelSet>
            </services:channelSet>
            </services:Personservice>
            </fx : Declarations>

            <mx:TextInput id="id" x="269" y="58"/>
            <mx:TextInput id="owner" x="269" y="93"/>
            <!-- id and owner are fields in Person.as and Person.java -->

            and this is my Person.as

            import mx.collections.ArrayCollection;
            [RemoteClass(alias="com.javaproject.TestPerson")]
            [Bindable]
            public class Person
            {
            //constructor
            public function Person(id:Number)
            {
            this.id=id;
            }
            public var id:Number = NaN;
            public var owner:String;
            public var work:ArrayCollection = new ArrayCollection();

            }
            TestPerson on the java side has the same structure as Person.as.

            Do i need to define <mx:RemoteObject aswell ?

            thanks
            Last edited by jaggernat; Feb 11th, 2011, 03:02 PM.

            Comment


            • #7
              Code:
              var token:AsyncToken = personServiceRO.save(new Person(id:Number));
              Hey above line is incorrect. It has to be,

              Code:
              var token:AsyncToken = personServiceRO.save(new Person(8));
              A number (8 or any other number) has to be passed to Person constructor.

              Comment


              • #8
                But how do I pass the number? I can't
                hard-code 8 or any other number.
                I have to pass the <mx:TextInput id='id'> which is defined
                in the mxml, right?

                Thanks for help.
                Last edited by jaggernat; Feb 12th, 2011, 10:40 AM.

                Comment


                • #9
                  I have to pass the <mx:TextInput id='id'> which is defined in the mxml
                  Code:
                  var token:AsyncToken = personServiceRO.save(new Person(parseInt(id)));

                  Comment

                  Working...
                  X