Jquery、Ajax、Struts2完成定時刷新的方法
更新時間:2013年05月10日 09:35:13 作者:
今天一個需求,需要在頁面上動態(tài)顯示后臺程序執(zhí)行的進(jìn)度,那么在頁面上會定時刷新,使用了Jquery、Ajax、Struts2。直接上代碼:
步驟一:導(dǎo)入jquery插件
<script type="text/javascript" src="js/easyui/jquery-1.8.0.min.js"></script>
步驟二:在<body></body>中添加信息顯示的地方
<label id="context"></label>
步驟三:在頁面中添加JS代碼
<script type="text/javascript">
setInterval("Push()",1000*60);
function Push() {
$.ajax({
type: "POST",
url: encodeURI("crmMaintenance/crmAirpolicyAction-takeAllMonitor.action"),
success: function(data) {
$('#context').text(data);
}
});}
</script>
步驟四:在Struts2中,進(jìn)行請求處理
public void takeAllMonitor() {
try {
HttpServletResponse response = ServletActionContext.getResponse();
PrintWriter out;
out = response.getWriter();
response.setCharacterEncoding("utf-8");
//...插入業(yè)務(wù)邏輯
out.print("Synchronous..." );
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
到此,就可以實現(xiàn)定時刷新頁面的效果,但是還存在一點問題,就是out.print("")中有中文的時候,有亂碼問題,如果哪位大俠知道怎么處理,還希望指點一二。
復(fù)制代碼 代碼如下:
<script type="text/javascript" src="js/easyui/jquery-1.8.0.min.js"></script>
步驟二:在<body></body>中添加信息顯示的地方
復(fù)制代碼 代碼如下:
<label id="context"></label>
步驟三:在頁面中添加JS代碼
復(fù)制代碼 代碼如下:
<script type="text/javascript">
setInterval("Push()",1000*60);
function Push() {
$.ajax({
type: "POST",
url: encodeURI("crmMaintenance/crmAirpolicyAction-takeAllMonitor.action"),
success: function(data) {
$('#context').text(data);
}
});}
</script>
步驟四:在Struts2中,進(jìn)行請求處理
復(fù)制代碼 代碼如下:
public void takeAllMonitor() {
try {
HttpServletResponse response = ServletActionContext.getResponse();
PrintWriter out;
out = response.getWriter();
response.setCharacterEncoding("utf-8");
//...插入業(yè)務(wù)邏輯
out.print("Synchronous..." );
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
到此,就可以實現(xiàn)定時刷新頁面的效果,但是還存在一點問題,就是out.print("")中有中文的時候,有亂碼問題,如果哪位大俠知道怎么處理,還希望指點一二。
相關(guān)文章
關(guān)于request.getHeader("Referer")的問題探討
request.getHeader("Referer")獲取上次訪問的URL鏈接,在什么情況下他會出現(xiàn)問題,下面為大家分享下,感興趣的朋友不要錯過2013-10-10JSP輸出HTML時產(chǎn)生的大量空格和換行的去除方法
在WEB應(yīng)用中,如果使用jsp作為view層的顯示模板,都會被空格/空換行問題所困擾.2009-10-10JSP 開發(fā)之Spring Boot 動態(tài)創(chuàng)建Bean
這篇文章主要介紹了JSP 開發(fā)之Spring Boot 動態(tài)創(chuàng)建Bean的相關(guān)資料,需要的朋友可以參考下2017-07-07request.getParameter()取值為null的解決方法
在后臺通過Request取值為null,是因為只設(shè)置了id屬性,而取值候用的是name屬性,問題就出現(xiàn)在這里2014-06-06tomcat6下jsp出現(xiàn)getOutputStream() has already been called for t
這篇文章主要介紹tomcat6下jsp出現(xiàn)getOutputStream() has already been called for this response異常的原因和解決方法,需要的朋友可以參考下。2016-06-06