3

I am using following ajax function :

 $.ajax({
       url: "userhomeonload",
       contentType: 'application/json',
       type: 'POST',
      datatype:'json', 
       async: true,
       success: function (res) {
                 alert(res[i].name+" "+res[i].rollNo);
    }  });

I am getting correct result in alert box. Now I want to use the list returned by this ajax in struts iterator as follows :

<s:iterator value="list">
   <div>
     <s:property value='name'></s:property>
     <s:property value="rollNo"></s:property>
   </div>
</s:iterator> 

But nothing is getting displayed on screen. Can anyone tell me how can I do so?

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
user3542411
  • 33
  • 1
  • 6
  • return a JSP, not a JSON, and iterate there. – Andrea Ligios Jun 12 '14 at 08:42
  • Or use js to add elements. – Aleksandr M Jun 12 '14 at 08:56
  • @Andrea Ligios ,I don't get your point.Please explain what are you saying.Also how can I do so? – user3542411 Jun 12 '14 at 09:00
  • When you perform an AJAX call in Struts2, the two main type of results that are usually returned are: 1) **JSON** - preferred, newer, smaller, and handy. You can't use struts tags on the object, you need to manipulate it, or iterate it, with JavaScript; 2) **JSP** - a JSP snippet (only your iterator for example, and the declaration of the struts-tags taglib), and apply it to an element in your page, for example a div. That way, you can use struts tags on the result before it is applied to the page. Got it ? :) – Andrea Ligios Jun 12 '14 at 09:08
  • How can I implement your 2nd option? – user3542411 Jun 12 '14 at 09:11
  • @AndreaLigios please explain 2nd type of result i.e. JSP snippet.I don't know how to use JSP snippet as ajax response. – user3542411 Jun 12 '14 at 09:57

2 Answers2

1

@AndreaLigios please explain 2nd type of result i.e. JSP snippet.I don't know how to use JSP snippet as ajax response.

Main.jsp (complete)

<%@ taglib prefix="s" uri="struts-tags.tld" %>
<html>
    <head>
        <script>
            $(function(){   
                $('#loader').on("keypress click", function(e) {
                    $.ajax({
                        url: "<s:url action='ajaxAction'/>",
                    }).done(function(result) {
                        $("#target").html(result);
                    });
                });
            });
        </script>   
    </head>
    <body>
        <input type="button" id="loader" />
        <div id="target"></div>
    <body>
</html>

Struts.xml (relevant)

<action name="ajaxAction" class="foo.bar.AjaxAction">
    <result>Snippet.jsp</result>
</action>

AjaxAction (relevant)

private String testString;
/* Getter and Setter */

public String execute(){
   testString = "I'm loaded with AJAX";
   return SUCCESS;
}

Snippet.jsp (complete)

<%@ taglib prefix="s" uri="struts-tags.tld" %>

<!-- This is the result page. You can use Struts tags here, 
the generated HTML will be appended to the target div. -->

TestString: <s:property value="testString" />

Output:

<body>
    <input type="button" id="loader" />
    <div id="target">TestString: I'm loaded with AJAX</div>
<body>
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • This is working.I want to ask 1 more question.The content in my Snippet.jsp is to be included inside a showcase.Now the showcase plugin is not working in Main.jsp.Can you suggest me any solution – user3542411 Jun 12 '14 at 12:07
  • Glad that helped. I've no idea of what a showcase plugin is or why it is not working (anymore?). But this sounds like a totally new question, so I suggest you to open a new, well described question to get better help – Andrea Ligios Jun 12 '14 at 12:13
  • I am just getting "success" instead of "my message" as response for my ajax request. my code looks almost same as above. – kumarc Mar 31 '17 at 10:00
0

Actually what is happening behind the scene is after your ajax call you are only getting the JSON object but the the value of "list"

<s:iterator value="list">

is not mapped by the OGNL from the valuestack to the result page , for this approach to work in your case you have to set the property using JS.

As Andrea correctly explained if you return your result as a JSP all the property of your action object will be mapped by OGNL to the respective UI tags.

saurabh goyal
  • 1,754
  • 8
  • 35
  • 45