How to use ajax answer list as iterator struts2 value?

I am using the 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 get the correct result in the warning field. Now I want to use the list returned by this ajax in the 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 displayed on the screen. Can someone tell me how can I do this?

+3
source share
2 answers

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

Main.jsp (full)

<%@ 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 (full)

<%@ 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>
+1
source

, , ajax JSON, "list"

<s:iterator value="list">

OGNL , JS.

, JSP, OGNL .

0

All Articles