首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

页面刷新有关问题

2011-11-08 
页面刷新问题我完成一个动作,完成之后想通过js把页面部分自动刷新,请问怎么实现[解决办法]ajax[解决办法]

页面刷新问题
我完成一个动作,完成之后想通过js把页面部分自动刷新,请问怎么实现

[解决办法]
ajax
[解决办法]
部分刷新页面用ajax吧
[解决办法]
JS调用后台方法、在回调函数中刷新指定区域的页面部分。
[解决办法]
用AJAX,给你个原生的小例子吧
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'show.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

<script language="javascript">
//定义一个变量用于存放XMLHttpRequest对象
var xmlHttp;


//改函数用于创建一个XMLHttpRequest对象
function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
}


//这是一个启动AJAX异步通信的方法
function getServerTime(){
var now = new Date();//获取系统当前的时间

//创建一个XMLHttpRequest对象
createXMLHttpRequest();
//将状态触发器绑定到一个函数
xmlHttp.onreadystatechange= processServerTime;
//通过GET方法向指定的URL建立服务器的调用,加个临时的参数,以便表示一个全新的请求
var url="getServerTime?tmp="+now.getTime();//等写完回来看这一句话
xmlHttp.open("GET",url,true);
//发送请求
xmlHttp.send(null);
}

//这是一个用来处理状态改变的函数
function processServerTime(){
//定义一个变量用于存放从服务器返回的响应结果
var responseContext;
if(xmlHttp.readyState==4){
//如果响应成功
alert(xmlHttp.status);
if(xmlHttp.status==200){
//取出服务器的响应内容
responseContext=xmlHttp.responseText;
document.getElementById("servertime").innerHTML=responseContext;
}
}
}
/**以上是获取当前时间的**/

//这是一个启动AJAX异步通信的方法
function ajaxLogin(){
var ln = document.getElementById("loginname").value;
var lp = document.getElementById("loginpwd").value;
//创建一个XMLHttpRequest对象
createXMLHttpRequest();

//将状态绑定到一个函数
xmlHttp.onreadystatechange=processAjaxLogin;
//通过GET方法向指定的URL建立服务器的调用
var url="ajax_ajaxLogin.action?loginname="+ln+"&loginpwd="+lp;
xmlHttp.open("GET",url,true);

//发送请求
xmlHttp.send(null);
}
//这是一个用来处理状态改变的函数
function processAjaxLogin(){
//定义一个变量用于存放 从服务器返回的响应结果
var responseContext="";
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){


responseContext = xmlHttp.responseText;
alert(xmlHttp.responseText=="登录成功");
alert(responseContext.substring(0, 4)=="登录成功");
alert(responseContext.substring(0, 4).length);
document.getElementById("loginresult").innerHTML=responseContext;
}
}
}
</script>


</head>
  
<body>

<table border="1">
<tr>
<th>
1.服务器端的系统时间:
</th>
<td colspan="2">
<input type="button" value="获取服务器端时间" onclick="getServerTime()">
</td>
<td colspan="2">
<span id="servertime"></span>
 </td>
 </tr>
 <tr>
 <th align="left">
2.登陆账号:
</th>
<td>
<input type="text" id="loginname" name="loginname">
</td>
<th>
登陆密码:
</th>
<td>
<input type="password" id="loginpwd" name="loginpwd">
</td>
<td>
<div id="loginresult"></div>
</td>
</tr>
<tr>
<td>
<input type="button" value="异步登陆" onclick="ajaxLogin()">
</td>
</tr>
</table>
</body>
</html>
不过要用到struts2 还有问题的话继续问
[解决办法]
完成操作之后,进入之前显示此页面的action,就相当于刷新了当前页面了...
[解决办法]
用AJAX。在请求成功的回调函数里更新你要更新的页面(DOM)内容就可以了

热点排行