3

I need load the content of a Object JSON in many divs, but in parts. For example,

My JSON structure:

{"Example": {
   "Hi": "hi",
   "Bye": "bye"
 }
}

Assuming that the JSON string successfully load my JSP page. I am trying to load the contents of the JSON like this:

(For the attribute Hi and Bye)

<sj:div id="div1" dataType = "json">
    <s:property value="Example.Hi"/>
</sj:div>

<sj:div id="div2" dataType = "json">
    <s:property value="Example.Bye"/>
</sj:div>

Struts.xml:

    <action name="name" class="class" method="method">
        <result type="json">
            <param name="root">
                Example
            </param>
        </result>
    </action>

but this doesn't work... What I can do?

I'm using: Struts2 Jquery Library

Roman C
  • 49,761
  • 33
  • 66
  • 176
Ivan
  • 231
  • 1
  • 5
  • 14
  • Do you want to load the same content or different? – Roman C Jan 16 '14 at 20:41
  • The structure of JSON is the same in all the cases, but the content is different, becouse the Object JSON changes with a click in some row of a grid. For example, when i click other row the grid, the content JSON return is other: {"Example": { "Hi": "hi2", "Bye": "bye2" } } – Ivan Jan 17 '14 at 12:41
  • The different content can be loaded by different URL. – Roman C Jan 17 '14 at 13:14
  • Yes, yes. That rigth. Whats the solucion for that? – Ivan Jan 17 '14 at 13:20

1 Answers1

2

You should build the URL like

<s:url var="remoteurl1" action="name"><s:param name="div1" value="true"/></s:url>
<sj:div id="div1" href="%{#remoteurl1}" dataType = "json"/>

<s:url var="remoteurl2" action="name"><s:param name="div2" value="true"/></s:url>
<sj:div id="div2" href="%{#remoteurl2}" dataType = "json"/>

Then in your action you check if isDiv1() or isDiv2() and return corresponding result.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • and where specified that in the div1 should load the "Hi" attribute of the Object "Example"? and div2 should load "Bye" of the Object "Example"? – Ivan Jan 17 '14 at 13:55
  • The content is loaded to the tag body. It's a bit unclear about `dataType` attribute from the plugin wiki. But if you need to handle JSON as a normal `div` target you can subscribe `onSuccessTopics` and get JSON from the function data. – Roman C Jan 17 '14 at 15:45
  • I was trying, trying, trying and not make it. I have these problems: With your answer you say. That for every div, the action "name" has to run 2 times? because there are 2 divs. So if I want to upgrade 40 div in a jsp page, the action "name" must be executed 40 times?? – Ivan Jan 19 '14 at 14:21
  • This is because you tried to load divs in that way, you might choose another way but it's not related to the question you asked. – Roman C Jan 19 '14 at 14:46
  • You're right, I deviate from the topic because I want to learn. But your answer is correct one. But I would beg to give me the solution to what I seek. If you want to open another question. – Ivan Jan 19 '14 at 15:08
  • You can open question if it's not the same question and not a duplicate to another question not exactly yours. Questions are open to everyone who likes to answer them, you might answer your own question if you have a better solution. This way it makes it fun and everybody wins. – Roman C Jan 19 '14 at 15:48
  • Ok. Now make a new question later, but you can help me one last time in this post? How do I call ajax to execute the action "name" from code javascript? – Ivan Jan 19 '14 at 16:25
  • You have been very kind – Ivan Jan 19 '14 at 18:21
  • I asked a new question, if you would be so kind please come to visit my post. http://stackoverflow.com/questions/21235676/json-ajax-struts2-render-json-in-html – Ivan Jan 20 '14 at 13:56