我正在使用以下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>
但是屏幕上没有任何显示。谁能告诉我该怎么做?
@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>