我使用以下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> 但屏幕上没有显示任何东西。有人能告诉我怎么做吗?
发布于 2014-06-12 10:16:30
@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>发布于 2014-09-09 12:51:34
实际上,场景后面发生的事情是在ajax调用之后,您只得到了JSON对象,而得到了"list“的值
<s:iterator value="list">OGNL没有将值栈映射到结果页面,要使这种方法在您的情况下有效,您必须使用JS设置属性。
正如Andrea正确解释的那样,如果将结果作为JSP返回,则OGNL将将操作对象的所有属性映射到相应的UI标记。
https://stackoverflow.com/questions/24178374
复制相似问题