問題描述
我正在使用以下 ajax 函數(shù):
$.ajax({網址:用戶主加載",內容類型:'應用程序/json',類型:'POST',數(shù)據(jù)類型:'json',異步:真,成功:函數(shù)(res){alert(res[i].name+" "+res[i].rollNo);} });
我在警告框中得到了正確的結果.現(xiàn)在我想在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><頭><腳本>$(函數(shù)(){$('#loader').on("按鍵點擊", function(e) {$.ajax({url: "<s:url action='ajaxAction'/>",}).done(函數(shù)(結果){$("#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 */公共字符串執(zhí)行(){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 迭代器值?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯(lián)網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!