如何使用ajax响应列表作为struts2迭代器值?

时间:2014-06-12 07:05:37

标签: ajax struts2

我正在使用以下ajax函数:

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

我在警告框中收到了正确的结果。 现在我想在struts迭代器中使用此ajax返回的列表,如下所示:

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

但屏幕上没有显示任何内容。谁能告诉我怎么办?

2 个答案:

答案 0 :(得分:1)

  

@AndreaLigios请解释第二类结果,即JSP片段。我不知道如何使用JSP片段作为ajax响应。

Main.jsp(完整)

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

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

AjaxAction(相关)

private String testString;
/* Getter and Setter */

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

Snippet.jsp(完整)

<%@ 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" />

输出:

<body>
    <input type="button" id="loader" />
    <div id="target">TestString: I'm loaded with AJAX</div>
<body>

答案 1 :(得分:0)

实际上幕后发生的事情是在你的ajax调用之后,你只获得JSON对象但是&#34; list&#34;

的值
<s:iterator value="list">
OGNL没有将

从valuestack映射到结果页面,因为这种方法在你的情况下工作,你必须使用JS设置属性。

正如Andrea正确解释的那样,如果将结果作为JSP返回,则操作对象的所有属性都将由OGNL映射到相应的UI标记。