ajax的json傳值方式在jsp頁(yè)面中的應(yīng)用
更新時(shí)間:2013年03月08日 17:38:26 作者:
ajax的json傳值想必大家早有所耳聞了吧,本文介紹下jsp頁(yè)面中json傳值應(yīng)用,感興趣的你可以參考下哈,希望可以幫助到你
jsp頁(yè)面:
$(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)的功能是通過(guò)ajax定時(shí)訪問(wèn)后臺(tái)Hashtable,所以為了是系統(tǒng)區(qū)別url不同、ajax返回值不同,特意在url后加上r=+Math.random()
java后臺(tái)處理方法:
/**
* 雙服務(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的頁(yè)面存到map中,在通過(guò)json傳值。
SendMessage.sendObject(map)方法所在類(lèi)及方法:
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)的功能是通過(guò)ajax定時(shí)訪問(wèn)后臺(tái)Hashtable,所以為了是系統(tǒng)區(qū)別url不同、ajax返回值不同,特意在url后加上r=+Math.random()
java后臺(tái)處理方法:
復(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的頁(yè)面存到map中,在通過(guò)json傳值。
SendMessage.sendObject(map)方法所在類(lèi)及方法:
復(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();
}
}
您可能感興趣的文章:
- JAVASCRIPT實(shí)現(xiàn)的WEB頁(yè)面跳轉(zhuǎn)以及頁(yè)面間傳值方法
- JS父頁(yè)面與子頁(yè)面相互傳值方法
- javascript靜態(tài)頁(yè)面?zhèn)髦档娜N方法分享
- javascript不同頁(yè)面?zhèn)髦档母倪M(jìn)版
- JS中Iframe之間傳值及子頁(yè)面與父頁(yè)面應(yīng)用
- JavaScript實(shí)現(xiàn)在頁(yè)面間傳值的方法
- 詳解vuejs幾種不同組件(頁(yè)面)間傳值的方式
- 靜態(tài)頁(yè)面html中跳轉(zhuǎn)傳值的JS處理技巧
- JSP頁(yè)面間的傳值方法總結(jié)
- JS實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)并傳值
相關(guān)文章
ajax簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了ajax基礎(chǔ)知識(shí)點(diǎn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
原生ajax調(diào)用數(shù)據(jù)實(shí)例講解
這篇文章主要向大家介紹了原生ajax調(diào)用數(shù)據(jù)實(shí)例,具有一定的參考價(jià)值,感興趣的朋友可以參考一下2016-01-01
JQuery ajax 返回json時(shí)出現(xiàn)中文亂碼該如何解決
最近有朋友反映使用jquery.ajax調(diào)用返回json時(shí)出現(xiàn)中文亂碼,遇到此問(wèn)題,非常棘手,下面小編幫大家解決JQuery ajax 返回json時(shí)出現(xiàn)中文亂碼問(wèn)題,感興趣的朋友跟著一起了解了解吧2015-09-09
AJAX避免用戶重復(fù)提交請(qǐng)求實(shí)現(xiàn)方案
為了避免因某些原因用戶同時(shí)多次點(diǎn)擊按鈕,提交重復(fù)的請(qǐng)求,我們需要禁用請(qǐng)求提交按鈕,接下來(lái)與大家一起分享下實(shí)現(xiàn)方法2013-04-04
Ajax?請(qǐng)求隊(duì)列解決方案并結(jié)合elementUi做全局加載狀態(tài)
這篇文章主要介紹了Ajax?請(qǐng)求隊(duì)列解決方案并結(jié)合elementUi做全局加載狀態(tài),使用 消息隊(duì)列 制作 請(qǐng)求防抖,防止重復(fù)請(qǐng)求印象服務(wù)器,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10
Ajax添加數(shù)據(jù)與刪除篇實(shí)現(xiàn)代碼
Hello 大家好!這個(gè)ajax系列教程講到這里已經(jīng)完成50%了.第4篇講了ajax向數(shù)據(jù)庫(kù)添加數(shù)據(jù),第5篇講了ajax修改數(shù)據(jù)庫(kù)中的數(shù)據(jù).今天我們來(lái)講ajax添加數(shù)據(jù)與刪除并存的實(shí)例效果.2010-10-10
SpringMVC環(huán)境下實(shí)現(xiàn)的Ajax異步請(qǐng)求JSON格式數(shù)據(jù)
這篇文章主要介紹了SpringMVC環(huán)境下實(shí)現(xiàn)的Ajax異步請(qǐng)求JSON格式數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2016-05-05

