更新时间:2023-01-07 12:54:24
@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>