3 Replies Latest reply on Oct 18, 2006 1:48 AM by Freeseb

    How to access a HttpRequest variable

      I'm a beginner to this new technology. I am trying to build a ecard application using Flex and Rails. I wish to pass a variable to the the flex page
      for example http://localhost:3000/examples/SampleFlexToRubyProject.html?phto_id=1
      How can I access this variable phto_id from flex?
      Please help
        • 1. Re: How to access a HttpRequest variable
          Hey AjithRajD ,
          I had a similar question about accessing a variable that was set in the request scope, but I didn't get an answer. After digging around it has come to my attention, that the flex app needs to receive the data in an xml format. Which means that you would have to store that variable in an xml layout. I am using struts and flex, I haven't heard of rails. See below for an example:

          Lets say i have a variable that i create in my action class:
          private String str="hey";

          Instead of storing in a request scope, i would place that variable in an xml element tag (i used dom4j to create the xml document). i would then store the xml document in a request attribute see below:

          Once the struts controller forwards to myXML.jsp, the jsp would then out.println the request attribute. see below:
          <%out.println( (String) request.getAttribute)%>

          see below for example xml output:
          <childElement>Hey</childElement> -- Remember the “hey” value was stored in the str variable.

          Flex would then read the xmlDocument via the httpService. see below for example
          <httpService id="someId" url="/webroot/something.do" Result="xmlResponse(event)"/> -- u could also just pass in the actual xml document file in the url also. The current url between the quotes is what triggers the struts controller. The "Result" is an event that gets triggerd once flex receives a response from the server.
          import mx.rpc.events.ResultEvent;-- I could be wrong on this declation, i don't have the code in front of me.
          import mx.controls.Array.ArrayCollection;-- I could be wrong on this declation, i don't have the code in front of me.
          private var xml:ArrayCollection;
          private var xmlString:String;
          public function xmlReponse(event:ResultEvent):void
          xml=event.result.rootElement.parentElement; --This will parse the xml document.
          xmlString=(xml.getCharAt(0).childElement; --This will access the first parentElement/childElement property.
          mx.controls.Alert.show(xmlString, "Message box"); -- this is a message box. What will pop up is your variable that you saved in the childElement in the xml document.

          When an xml is returned from the server the flex application automatically stores it as a an array collection, but bear in mind that the xml must have repeating parent values before the xml is stored as an array collection, if there are no repeating parent elements then the xml will be stored as an object instead. See below for example

          This will be returned as an object. Which means if you try to store this in an ArrayCollections object, you will get a type conversion error
          <childElement>Hey</childElement> -- Remember the hey value was stored in the str variable.

          This will be returned as an arrayCollection.
          <childElement>Hey</childElement> -- Remember the hey value was stored in the str variable.
          <childElement>You</childElement> -- Remember the hey value was stored in the str variable.

          I am a newbie also. I started working with this flex off and on for about a month. You should consider purchasing the Flex Total Training DVD by James Talbot. It is a great entry level DVD and I hope they bring out more volumes. The price is a bit steep 150.00, but if you want to get into this adventurous application, you are going to need a tour guide. I just had hoped that the DVD discussed flex and struts, but I just know that when my struts application forwards the information to flex that it will have to be an in xml layout.

          I apologize for the long post. I know people hate reading long posts. Let me know how you make out.

          • 2. Re: How to access a HttpRequest variable
            AjithRajD Level 1
            Hi java_hero,

            Thank u for ur advice. Basically I am java programmer. So I got the idea to do in Rails.
            Thanks u.
            • 3. Re: How to access a HttpRequest variable
              Freeseb Level 1
              to pass DIRECTLY variables to a flex application you can to it susing this simple way :


              and in the mxml file :