ajax的json傳值方式在jsp頁面中的應用_AJAX相關

來源:互聯網
上載者:User
jsp頁面
複製代碼 代碼如下:

$(document).ready(function() {
setInterval(function myTimer()
{
//alert('a');
getViews();
},1000);
});
//播放
function getViews(){
$.ajax({
'url':"${pageContext.request.contextPath}/video/getVideos.action?r="+Math.random()+"&open=1",
'data': '',
'dataType': 'json',
'type': 'get',
'error': function(data){
alert("error");
return false;
},
'success': function(data) {
if(null != data && '' != data){
//alert(data.updateFlag);
if(data.updateFlag==0){//如果data.updateFlag=0 不重新整理
//alert("data.updateFlag=0");
}
else{
if(data.videoIds != null && data.videoIds != ""){
var listIds=data.videoIds;
var i=0;
for(;i<listIds.length;++i){
//alert("show:"+i+"id=:"+listIds[i]);
showView(listIds[i],i);//播放
}
for(var j=listIds.length;j<9;++j){
//alert("Stop:"+j);
StopPlayVideo(j);
}
}
}
}
}
});
}

此處由於此代碼實現的功能是通過ajax定時訪問後台Hashtable,所以為了是系統區別url不同、ajax傳回值不同,特意在url後加上r=+Math.random()
java幕後處理方法
複製代碼 代碼如下:

/**
* 雙伺服器九宮格顯示
*
* @return
*/
@Action(value = "getVideos" ,results={
@Result(name = SUCCESS,location="videos2.jsp")
})
public String getVideos() {
if (open == 301) {
return SUCCESS;
} else {
try {
VideoHashTable videoHashTable = VideoHashTable.getInstance();
Hashtable<Long, Long> hashTable = videoHashTable.getRht();
Map map = new HashMap<String, List<Long>>();
if (videoHashTable.isUpdateFlag() == true) {
Enumeration en = hashTable.keys();
videoIds = new ArrayList<Long>();
while (en.hasMoreElements()) {
Long key = (Long) en.nextElement();
// vth.get(key);
videoIds.add(key);
}
map.put("videoIds", videoIds);
map.put("updateFlag", 1L);
videoHashTable.setUpdateFlag(false);
System.out.println("getVideos:" + videoIds);
SendMessage.sendObject(map);
} else {
map.put("updateFlag", 0L);
SendMessage.sendObject(map);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}

關鍵要把傳到jsp的頁面存到map中,在通過json傳值。
SendMessage.sendObject(map)方法所在類及方法
複製代碼 代碼如下:

package com.supcon.honcomb.utils;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import org.apache.http.HttpResponse;
import org.apache.struts2.ServletActionContext;
public class SendMessage {
public static void sendMessage(String responseText){
try {
PrintWriter out = ServletActionContext.getResponse().getWriter();
out.print(responseText);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void sendObject(Object obj) throws Exception {
PrintWriter pw;
String rtn = "";
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
rtn = JsonUtil.JsonFromObject(obj);
pw = response.getWriter();
pw.write(rtn);
pw.flush();
pw.close();
}
}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.