1

I need reload the content of a div with a part of an object JSON. The code below does it perfectly, but the resulting div get all the JSON object and between {}. I need the content to appear only one variable.

JSP:

<s:url id="ajax1" value="ajax1.action"/>
<sj:div id="div1" 
    href="%{ajax1}" 
    reloadTopics="reloaddiv1">
</sj:div>
<sj:a 
    id="refreshlink" 
    onClickTopics="refreshdiv" 
    button="true">Refresh Div
</sj:a>

JavaScript:

    <script type="text/javascript">
        $.subscribe('refreshdiv', function(event,data) {
            $.publish('reloaddiv1');
        });
    </script>

My action:

<package name="default" extends="struts-default,json-default" namespace="/">
    <result-types>
        <result-type name="json" class="org.apache.struts2.json.JSONResult" />
    </result-types>

    <action name="ajax1" class="action.Div">
        <result type="json"/>
    </action>

</package>

MY CLASS action.Div

String hi = "hi world";
String bye = "bye world";

@Override
public String execute() throws Exception {
    return SUCCESS;
} 

/* getters and setters*/

Output shown in my div:

{"bye":"bye world","hi":"hi world"}

How do I make you see just "hi world"?

pagid
  • 13,559
  • 11
  • 78
  • 104
Ivan
  • 231
  • 1
  • 5
  • 14

1 Answers1

1
  • Struts2 JSON Plugin serializes the entire Action, so if you need only one variable, you need to specify a root object ("hi", in your case) in struts.xml.

    Reading this answer could be useful: https://stackoverflow.com/a/17149414/1654265

  • You will then get "hi world". If you want the value without the double quotes, you need to instruct JQuery plugin to make it aware that it's dealing with JSON Data; add dataType = "json" to your <sj:div> as described here, and you will get hi world.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • tnx for your answer. That answer generated me some problems, I Have an Object "cliente" that replace to "Hi" and i have many Divs. In each div i load for example "cliente.name" in other div i load "cliente.surname" and all in the same page. What can i do? The problem is in: Load all objet cliente in each the div – Ivan Jan 15 '14 at 15:35
  • I did not understand, that means I should ask another question in another post? or the same? – Ivan Jan 15 '14 at 15:42
  • Ask another question expanding what you was saying in the comment. This is a legit question, and already has a correct answer; changing the question after it was answered won't help future visitors. – Andrea Ligios Jan 15 '14 at 15:44
  • http://stackoverflow.com/questions/21144327/load-many-divs-from-a-grid-struts2-jquery-plugin – Ivan Jan 15 '14 at 17:32
  • I try to explain as much as possible so that you achieve understand me, I hope you achieve read all and can help me, I searched for all the web and do not achieve. So help me plz. – Ivan Jan 15 '14 at 18:30
  • Hi, I have reconsidered the question for you is easier to help. read it please http://stackoverflow.com/questions/21167826/load-parts-of-a-json-in-many-divs-struts2 – Ivan Jan 16 '14 at 16:53
  • 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:57