ajax的json傳值方式在jsp頁面中的應(yīng)用
更新時(shí)間:2013年03月08日 17:38:26 作者:
ajax的json傳值想必大家早有所耳聞了吧,本文介紹下jsp頁面中json傳值應(yīng)用,感興趣的你可以參考下哈,希望可以幫助到你
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);
}
}
}
}
}
});
}
此處由于此代碼實(shí)現(xiàn)的功能是通過ajax定時(shí)訪問后臺Hashtable,所以為了是系統(tǒng)區(qū)別url不同、ajax返回值不同,特意在url后加上r=+Math.random()
java后臺處理方法:
/**
* 雙服務(wù)器九宮格顯示
*
* @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;
}
}
關(guān)鍵要把傳到j(luò)sp的頁面存到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();
}
}
復(fù)制代碼 代碼如下:
$(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);
}
}
}
}
}
});
}
此處由于此代碼實(shí)現(xiàn)的功能是通過ajax定時(shí)訪問后臺Hashtable,所以為了是系統(tǒng)區(qū)別url不同、ajax返回值不同,特意在url后加上r=+Math.random()
java后臺處理方法:
復(fù)制代碼 代碼如下:
/**
* 雙服務(wù)器九宮格顯示
*
* @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;
}
}
關(guān)鍵要把傳到j(luò)sp的頁面存到map中,在通過json傳值。
SendMessage.sendObject(map)方法所在類及方法:
復(fù)制代碼 代碼如下:
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();
}
}
您可能感興趣的文章:
相關(guān)文章
往xml中更新節(jié)點(diǎn)的實(shí)例代碼
下面小編就為大家?guī)硪黄鵻ml中更新節(jié)點(diǎn)的實(shí)例代碼。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-07-07深入淺析AjaxFileUpload實(shí)現(xiàn)單個(gè)文件的 Ajax 文件上傳庫
jQuery.AjaxFileUpload.js是一款jQuery插件,用于通過ajax上傳文件。本文給大家介紹AjaxFileUpload實(shí)現(xiàn)單個(gè)文件的 Ajax 文件上傳庫,對此感興趣的朋友一起學(xué)習(xí)吧2016-04-04登錄超時(shí)給出提示跳到登錄頁面(ajax、導(dǎo)入、導(dǎo)出)
這篇文章主要介紹了登錄超時(shí)給出提示跳到登錄頁面(ajax、導(dǎo)入、導(dǎo)出)的相關(guān)資料,需要的朋友可以參考下2016-02-02使用Ajax技術(shù)通過XMLHttpRequest對象完成首頁登錄功能
這篇文章主要介紹了使用Ajax技術(shù)通過XMLHttpRequest對象完成首頁登錄功能,很不錯(cuò)的嘗試,需要的朋友可以參考下2014-08-08