5 Replies Latest reply on Jun 13, 2012 10:59 AM by Ryan Lunka

    How to pass a map value from one component to another component




      I need to pass a map<string,List<String>> from one component to another component.

      i know that,  i can pass the string value by setting the property to a component 1 as node and retrieve the property in component 2.


      But i am not able to set the property for the Type Map. only string and string[] are supported.


      How can i pass the map from one component to another?


      Help me in this.


      Thanks in advance.

        • 1. Re: How to pass a map value from one component to another component

          There are really two solutions I can think of.

          One is if you really just need a quick and dirty way to pass some data from one component to another during your request processing, you can use the setAttribute and getAttribute methods on the javax.servlet.http.HttpServletRequest object (request). This lets you assign any object to a named attribute of the request object which any component would then be able to grab. This will only last for the duration of a single request. This is really just a hack and likely indicates your components and/or data boundaries are not well defined.


          The second method is to actually persist the data into a structure in the JCR. There is no single property that can do this; based on your map template you would probably need a collection of nodes, each with a string property for the key and a string[] property for the value. You would build your map by reading in the list of child nodes and grabbing the key and value properties from each node, and persist your map by updating the same.

          • 2. Re: How to pass a map value from one component to another component
            Raja_Raju_Learner Level 1

            Hi Keith,


            Thanks alot ffor your response.


            I feel the second method is structured one. I would like to go with this. I need a small help here. I haven't created any node from the jsp using create node. so can you guide me to create the collection of nodes in the JCR?


            Thanks in advance.

            • 3. Re: How to pass a map value from one component to another component
              justin_at_adobe Adobe Employee


              I would strongly think about the thread safety and security issues around having a component's default script write data to the repository. Does that work if multiple threads are accessing the component? Does it work when the anonymous user is hitting the page?


              There are ways to make it work, but it is the type of thing that can work fine in local development and then blow up when you move the code into production.


              Raja- to your question about the JCR API, I'd suggest reading the specification.




              1 person found this helpful
              • 4. Re: How to pass a map value from one component to another component

                Depending on your data you may be able to write the data to a JSON formatted string, attach that data to the servlet request with request.setAttribute("mydata",data); then in the other component get the 'mydata' attribute and parse the JSON back to whatever you need.  This at least keeps everything in memory, I'd agree with Justin that writing to the repository to solve this kind of problem does not seem the most efficient and low cost method and may indeed be error prone depending on implementation.

                • 5. Re: How to pass a map value from one component to another component
                  Ryan Lunka Level 3

                  This has kind of a bad smell to me.  You have two separate components that need to communicate some set of data?  I think to do this, you want to use a method that fully decouples those components, otherwise they depend on one another to be present on a page...this is likely something that will not be enforced by the source code.  I didn't get the impression there was much of a security issue with the "shared" data, so what about this approach...


                  Use something of a client-side publish/subscribe paradigm where component A "broadcasts" its data as JSON via JavaScript and component B listens for that data broadcast to then do something with it.  This way neither component is necessarily dependent on the other existing.  I'm talking in abstract terms, so I don't really have a specific implementation plan in mind.  Maybe something like some shared (amongst the components) JS in a clientlib that allows both components to "register" themselves if they are on the page...

                  Regardless of specific approach, I would think you'd want to be careful to decouple these two components, and trying to force them to share data server-side during the request resolution seems fishy.