如何使用 ajax 响应列表作为 struts2 迭代器值? [英] How to use ajax response list as struts2 iterator value?

查看:22
本文介绍了如何使用 ajax 响应列表作为 struts2 迭代器值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下 ajax 函数:

 $.ajax({网址:用户主加载",内容类型:'应用程序/json',类型:'POST',数据类型:'json',异步:真,成功:函数(res){alert(res[i].name+" "+res[i].rollNo);} });

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

<s:iterator value="list">

<s:property value='name'></s:property><s:property value="rollNo"></s:property></div></s:迭代器>

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

解决方案

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

Main.jsp(完整)

<%@ taglib prefix="s" uri="struts-tags.tld" %><html><头><脚本>$(函数(){$('#loader').on("按键点击", function(e) {$.ajax({url: "<s:url action='ajaxAction'/>",}).done(函数(结果){$("#target").html(结果);});});});</脚本></头><身体><input type="button" id="loader"/><div id="target"></div><身体></html>

Struts.xml(相关)

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

AjaxAction(相关)

私有字符串 testString;/* Getter 和 Setter */公共字符串执行(){testString = "我加载了 AJAX";返回成功;}

Snippet.jsp(完整)

<%@ taglib prefix="s" uri="struts-tags.tld" %><!-- 这是结果页面.你可以在这里使用 Struts 标签,生成的 HTML 将附加到目标 div.-->测试字符串:<s:property value="testString"/>

输出:

<input type="button" id="loader"/><div id="target">TestString:我加载了AJAX</div><身体>

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?

解决方案

@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>

这篇关于如何使用 ajax 响应列表作为 struts2 迭代器值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆