我用ajax实现了一个定时跳转的功能,是跳到action中,再执行action中的代码(代码中写了几个if判断),但是在action用return mapping.findForward("aa")(aa是配置到了另一个页面)跳转确不好用(通过调试,这句话也走了),但是根本就返回不到指定的页面,struts-config.xml配置的也没有问题啊,我也不知道是怎么回事,看似没问题的代码,弄了好多天了,请各位帮帮忙,帮我分析一下,谢谢了。
代码如下:
var XMLHttpReq; function createXMLHttpRequest()
{
if(window.XMLHttpRequest)
{ XMLHttpReq = new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
try
{
XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{}
}
}
} function sendRequest()
{
createXMLHttpRequest();
var errcode = document.getElementById("code").value;
var url = "confirmorderinfo.do?method=add";
XMLHttpReq.open("post", url, true);
XMLHttpReq.send(null);
} window.onload = function()
{
setTimeout('sendRequest()',2000);
}
代码如下:
var XMLHttpReq; function createXMLHttpRequest()
{
if(window.XMLHttpRequest)
{ XMLHttpReq = new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
try
{
XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{}
}
}
} function sendRequest()
{
createXMLHttpRequest();
var errcode = document.getElementById("code").value;
var url = "confirmorderinfo.do?method=add";
XMLHttpReq.open("post", url, true);
XMLHttpReq.send(null);
} window.onload = function()
{
setTimeout('sendRequest()',2000);
}
解决方案 »
- 数据报送系统,自定义报表进行报送
- JSP out.rpintln("<font color="blue">123</font>")
- 大家讨论一下项目中对异常的控制
- 使用jfreechart画折线图时,如何设定线条的粗细啊?
- dispaly tag在struts中翻页和排序的问题
- 为什么打开jsp,进度几分钟都在加载东西似的
- servlet中request.getRequestDispatcher("index.jsp")跳转到jsp页面没有任何反应
- struts1 标签问题
- 无法显示中文名字的图片,网上搜了各种方法都不行
- 在JSP中如何输出特殊字符?
- filterservlet过滤器不管用
- 看了2天的Hibernate,还是不太了解持久化概念。
页面事件出发getCourseData(element)方法,看看这个例子吧
var xmlHttp;
function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
//alert("ok");
}
else if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
}
function getCourseData(element){
createXMLHttpRequest();
var url = "ToolTipServlet";
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange = callback;
xmlHttp.send(null);
}
function callback(){
if(xmlHttp.readyState == 4){
if(xmlHttp.status == 200){
//alert(courseData.getElementsByTagName("length")[0]);
setData(xmlHttp.responseXML);//显示处理信息的方法
}
}
}
然后return null就可以了
比如
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Content-Type", "text/xml; charset=gb2312");
PrintWriter pw = response.getWriter();
pw.write(“需要返回的值!”);
pw.close();
return null;
这是个人见解,希望不要误导你,呵呵
要说名的是:在action中的return mapping.findForward("a")这段代码根本就一点用都没有,还给我造成了很大的误解。在action加一个response.getWriter().write(info);再在ajax中加一个回调函数接收一下值,就解决了,谢谢各位的帮忙!
如果你用的是xmlhttprequest.responseXML你action指定得页面里必须是一个xml
如果你用的是xmlhttprequest.responseText你action指定得页面里可以是一个有输出信息得简单的html或jsp
有点小小的分析,希望能帮助你
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
{
if(0==0)
{
response.getWriter().write("要显示在页面上的信息");//此时可以在回调函数中判断:
}
return null; //这里必须返回null,如果想跳转到其他页面似乎是不可能的
}ajax.jsp中是这样写的:
function createRequest()
{
if(window.ActiveXObject)
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
} function dogetName(url)
{
createRequest();
xmlhttp.onreadystatechange=getNameCall;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
} function go()
{
var url="a.do";//这里写你要跳转到的路径
dogetName(url);
} function getNameCall()
{
if(xmlhttp.readyState==1)
{
document.getElementById("info").innerHTML ="<div id='ff'>系统正在处理中,请稍候...</div>";
}
if(xmlhttp.readyState==4)
{
if(xmlhttp.status==200)
{
setJump();
}
}
}
window.onload = function()
{
setTimeout('go()',1000);//定时器 自动执行
}
function setJump(){
document.getElementById('ff').innerHTML="<table id='gameli' style='margin-top:5px;' width='180' border='0' cellpadding='0' cellspacing='0'";
var text =xmlhttp.responseText;//接收传回来的值
}
}
</script>
应该差不多了 不明白再问吧