我有一个JQuery - Struts 2应用程序。我通过$ .load()向struts动作发送请求,我得到一个HTML内容,一切都很好。 问题是当我需要获取HTML内容以及一个显示状态的整数时,由单个 XMLHTTPRequest获取。
实际上,就我而言,HTML内容是服务器进程的新日志,整数值是该进程的状态。
如何将整数与内容一起发回?
这是动作配置:
<action name="getProcessUpdate" class="ProcessAction" >
<result type="stream">
<param name="contentType">text/html</param>
<param name="inputName">newLogs</param>
</result>
</action>
这是在动作类中:
public class ProcessAction extends ActionSupport {
private InputStream newLogStream;
public InputStream getNewLogs() {
return newLogStream;
}
public String execute() {
newLogStream = new ByteArrayInputStream(getNewLogHTML().getBytes());
return SUCCESS;
}
private String getNewLogHTML(){
String newLong = "";
newLong = "Some new Longs";
return newLong;
}
}
这是我的jquery电话:
function getNewLogs(){
$( "#log" ).load('getProcessUpdate');
}
答案 0 :(得分:1)
使用普通结果(而不是Stream),并返回包含所需Action对象的JSP片段,然后使用$.load()
返回。
请务必使用escape="false"
阻止您在代码段中转义值。
struts.xml中
<action name="getProcessUpdate" class="ProcessAction" >
<result>snippet.jsp</result>
</action>
动作
public class ProcessAction extends ActionSupport{
private String newLog;
private Integer threadState;
/* Getters */
public String execute() {
threadState = 1337;
newLog = getNewLogHTML();
return SUCCESS;
}
}
主JSP
<script>
$(document).ready(function getNewLogs(){
$( "#container" ).load('getProcessUpdate');
});
</script>
<div id="container"></div>
snippet.jsp
<%@taglib prefix="s" uri="/struts-tags" %>
<h3>Log file</h3>
<div id="log">
<s:property value="newLog" escape="false" />
</div>
<h3>Thread state</h3>
<div id="threadState">
<s:property value="threadState" />
</div>
答案 1 :(得分:0)
好吧,我最终选择了我的旧inputStream方法和@Andrea发布的答案的组合:这将是我将返回HTML的一部分,包括我的日志和我的状态,然后在我的java脚本代码中我将它们分开,通过帮助JQuery。
无论如何我会接受@Andrea的回答我猜是因为它鼓舞人心。
感谢。