页面可以用ajax通过事件调java方法,但有是我想把java方法执行过程中的某些提示信息自由自动的反馈给页面。该怎么办?? 请各位大哥,大姐,叔叔指点一下。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 都用到ajax了, 那这个问题还是可以用ajax来解决. dwr有的只是让方法返回执行结果.而我的打算是把方法运行的细节情况反馈给页面.请再替我想想吧! 等我晚上有空了做demo给你看. Demo来了.我用的是spring环境, 所有的controller都继承子Controller;RegisteController:用来模拟用户注册的步骤, 它会产生一些执行过程中的信息放到一个链表里;InfoController: 用来从链表中的链表中最顶端的那条信息.发送给客户.用prototype的ajax来发送请求.发送请求的同时, 发送另外一个请求用来得到执行过程中的提示信息.jsp文件, 用来发送ajax请求和显示回显信息.<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>Testing page</title> <script type="text/javascript" src="js/prototype-1.5.1.2.js"></script> <script type="text/javascript"> function postAjax(){ var myAjax = new Ajax.Request( 'register.do', { method: 'post', parameters: {username: 'username', password: 'password'}, onComplete: doComplete } ); alert("request send."); getInfo(); } function getInfo(){ var infoAjax = new Ajax.Request( 'info.do', { method: 'get', parameters: {}, onComplete: doResult } ); } function doResult(req){ if(req.responseText=='finish'){ return; } //alert(req.responseText); var myDiv = document.createElement("div"); var infoText = document.createTextNode(req.responseText); myDiv.appendChild(infoText); $(info).appendChild(myDiv); //循环发送请求得到执行信息. getInfo(); } function doComplete(){ } </script> <style type="text/css"> html,body { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11pt; margin: 0; padding: 0; border: 0 none; overflow: hidden; height: 100%; } .title{ font-weight: bold; } </style> </head> <body> <div><a href="javascript: postAjax();">点击将提交一个ajax请求, 模拟执行用户注册行为. 这个请求得执行过程将显示在下面.</a></div> <div id="info" style="width: 400px; height: 200px; overflow: scroll; border: solid red 1px;"> <div class="title">The response information comes from server: </div> </div> </body></html>RegisteController:import java.util.LinkedList;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.Controller;public class RegisteController implements Controller { private Log log = LogFactory.getLog(getClass()); public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { String username = arg0.getParameter("username"); String password = arg0.getParameter("password"); log.info("username:" + username + " password:" + password); /** * 下面得infoList将用来存放执行过程中的提示信息. * 之所以用linkedList是因为linkedList具有队列操错的特点. * 最先放进去的信息可以被取走. */ LinkedList<String> infoList = new LinkedList<String>(); /** * 这里放的信息紧紧是起到模拟作用. * 我一次性把所有的信息都放进去了.只是为了模拟效果. * 你可以根据实际需要, 在需要的时候再放入信息. */ infoList.add("username: " + username); //do something else. infoList.add("password: " + password); //do something else. infoList.add("Try to validate username......"); //do something else. infoList.add("this username is valid to use"); //do something else. infoList.add("Try to validate password......"); //do something else. infoList.add("the password is valid to use"); //do something else. infoList.add("Try to add this user into database......"); //do something else. infoList.add("try to set entitlement for this user"); //do something else. infoList.add("user registering ok."); //do something else. infoList.add("succeed......"); arg0.getSession().setAttribute("infoList", infoList); arg1.getWriter().write("succeed"); return null; }}InfoController:import java.util.LinkedList;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.Controller;/** * @author Administrator * 这个controller用来向客户端发送执行过程中产生的信息. */public class InfoController implements Controller{ public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { LinkedList<String> list = (LinkedList<String>)arg0.getSession().getAttribute("infoList"); /** * 为了模拟延迟效果, 这里睡一小会儿. */ Thread.sleep(1000); if(list==null){ arg1.getWriter().write("please wait a second."); return null; } if(list.size()==0){ arg1.getWriter().write("finish"); return null; } /** * linkedList.remove()将取走第一条信息. */ String info = list.remove(); arg1.getWriter().write(info); return null; }}spring配置文件: <bean name="/register.do" class="com.fydy.action.RegisteController" /> <bean name="/info.do" class="com.fydy.action.InfoController" />不知道里用什么环境,struts? servlet? 纯jsp. 不过都不重要. 理解做法就可以了. 真的是谢谢你了,小弟我没用过spring 有点看不懂。刚入门的,还得多多指教。 我用的是struts.真是谢谢你了。 struts也不要紧,把上面的Controller都改成Action就可以了。比如:RegisteAction:import java.util.LinkedList;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;public class RegisteAction extends Action{ private Log log = LogFactory.getLog(getClass()); public ActionForward execute(ActionMapping arg0, ActionForm arg1, HttpServletRequest arg2, HttpServletResponse arg3) throws Exception { String username = arg2.getParameter("username"); String password = arg2.getParameter("password"); log.info("username:" + username + " password:" + password); /** * 下面得infoList将用来存放执行过程中的提示信息. * 之所以用linkedList是因为linkedList具有队列操错的特点. * 最先放进去的信息可以被取走. */ LinkedList<String> infoList = new LinkedList<String>(); /** * 这里放的信息紧紧是起到模拟作用. * 我一次性把所有的信息都放进去了.只是为了模拟效果. * 你可以根据实际需要, 在需要的时候再放入信息. */ infoList.add("username: " + username); //do something else. infoList.add("password: " + password); //do something else. infoList.add("Try to validate username......"); //do something else. infoList.add("this username is valid to use"); //do something else. infoList.add("Try to validate password......"); //do something else. infoList.add("the password is valid to use"); //do something else. infoList.add("Try to add this user into database......"); //do something else. infoList.add("try to set entitlement for this user"); //do something else. infoList.add("user registering ok."); //do something else. infoList.add("succeed......"); arg2.getSession().setAttribute("infoList", infoList); arg3.getWriter().write("succeed"); return null; }} eclipse启动服务器失败 JSP后台管理的问题 web项目中加入短信功能怎么弄,请教做过的牛人 [求助]求一个简单的聊天室系统 JSP页面如何显示㎡ 出几道今天支付宝笔试题 又出现中文乱码了,接受方不肯转编码!!!我该怎么办!!! 请各位高手帮我看看什么问题? APACHE与TOMCAT整合时候的怪问题??哪为高手碰到过?解决立刻给分。。急····· db2连接很奇怪的问题,高手请进 <怎样扫描页面中的所有超链接。> 用gb2312就正常。换成utf-8就乱码(smartupload) 在线等
而我的打算是把方法运行的细节情况反馈给页面.
请再替我想想吧!
我用的是spring环境, 所有的controller都继承子Controller;
RegisteController:用来模拟用户注册的步骤, 它会产生一些执行过程中的信息放到一个链表里;
InfoController: 用来从链表中的链表中最顶端的那条信息.发送给客户.
用prototype的ajax来发送请求.
发送请求的同时, 发送另外一个请求用来得到执行过程中的提示信息.jsp文件, 用来发送ajax请求和显示回显信息.<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Testing page</title>
<script type="text/javascript" src="js/prototype-1.5.1.2.js"></script>
<script type="text/javascript">
function postAjax(){
var myAjax = new Ajax.Request(
'register.do',
{
method: 'post',
parameters: {username: 'username', password: 'password'},
onComplete: doComplete
}
);
alert("request send.");
getInfo();
}
function getInfo(){
var infoAjax = new Ajax.Request(
'info.do',
{
method: 'get',
parameters: {},
onComplete: doResult
}
);
}
function doResult(req){
if(req.responseText=='finish'){
return;
}
//alert(req.responseText);
var myDiv = document.createElement("div");
var infoText = document.createTextNode(req.responseText);
myDiv.appendChild(infoText);
$(info).appendChild(myDiv);
//循环发送请求得到执行信息.
getInfo();
}
function doComplete(){
}
</script> <style type="text/css">
html,body {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 11pt;
margin: 0;
padding: 0;
border: 0 none;
overflow: hidden;
height: 100%;
}
.title{
font-weight: bold;
}
</style>
</head>
<body>
<div><a href="javascript: postAjax();">点击将提交一个ajax请求, 模拟执行用户注册行为. 这个请求得执行过程将显示在下面.</a></div>
<div id="info" style="width: 400px; height: 200px; overflow: scroll; border: solid red 1px;">
<div class="title">The response information comes from server: </div>
</div>
</body>
</html>RegisteController:import java.util.LinkedList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;public class RegisteController implements Controller { private Log log = LogFactory.getLog(getClass());
public ModelAndView handleRequest(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
String username = arg0.getParameter("username");
String password = arg0.getParameter("password");
log.info("username:" + username + " password:" + password);
/**
* 下面得infoList将用来存放执行过程中的提示信息.
* 之所以用linkedList是因为linkedList具有队列操错的特点.
* 最先放进去的信息可以被取走.
*/
LinkedList<String> infoList = new LinkedList<String>();
/**
* 这里放的信息紧紧是起到模拟作用.
* 我一次性把所有的信息都放进去了.只是为了模拟效果.
* 你可以根据实际需要, 在需要的时候再放入信息.
*/
infoList.add("username: " + username);
//do something else.
infoList.add("password: " + password);
//do something else.
infoList.add("Try to validate username......");
//do something else.
infoList.add("this username is valid to use");
//do something else.
infoList.add("Try to validate password......");
//do something else.
infoList.add("the password is valid to use");
//do something else.
infoList.add("Try to add this user into database......");
//do something else.
infoList.add("try to set entitlement for this user");
//do something else.
infoList.add("user registering ok.");
//do something else.
infoList.add("succeed......");
arg0.getSession().setAttribute("infoList", infoList);
arg1.getWriter().write("succeed");
return null;
}}InfoController:import java.util.LinkedList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
/**
* @author Administrator
* 这个controller用来向客户端发送执行过程中产生的信息.
*/
public class InfoController implements Controller{ public ModelAndView handleRequest(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
LinkedList<String> list = (LinkedList<String>)arg0.getSession().getAttribute("infoList");
/**
* 为了模拟延迟效果, 这里睡一小会儿.
*/
Thread.sleep(1000);
if(list==null){
arg1.getWriter().write("please wait a second.");
return null;
}
if(list.size()==0){
arg1.getWriter().write("finish");
return null;
}
/**
* linkedList.remove()将取走第一条信息.
*/
String info = list.remove();
arg1.getWriter().write(info);
return null;
}}spring配置文件: <bean name="/register.do" class="com.fydy.action.RegisteController" />
<bean name="/info.do" class="com.fydy.action.InfoController" />不知道里用什么环境,struts? servlet? 纯jsp. 不过都不重要. 理解做法就可以了.
比如:RegisteAction:import java.util.LinkedList;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;public class RegisteAction extends Action{ private Log log = LogFactory.getLog(getClass()); public ActionForward execute(ActionMapping arg0,
ActionForm arg1, HttpServletRequest arg2,
HttpServletResponse arg3) throws Exception {
String username = arg2.getParameter("username");
String password = arg2.getParameter("password");
log.info("username:" + username + " password:" + password);
/**
* 下面得infoList将用来存放执行过程中的提示信息.
* 之所以用linkedList是因为linkedList具有队列操错的特点.
* 最先放进去的信息可以被取走.
*/
LinkedList<String> infoList = new LinkedList<String>();
/**
* 这里放的信息紧紧是起到模拟作用.
* 我一次性把所有的信息都放进去了.只是为了模拟效果.
* 你可以根据实际需要, 在需要的时候再放入信息.
*/
infoList.add("username: " + username);
//do something else.
infoList.add("password: " + password);
//do something else.
infoList.add("Try to validate username......");
//do something else.
infoList.add("this username is valid to use");
//do something else.
infoList.add("Try to validate password......");
//do something else.
infoList.add("the password is valid to use");
//do something else.
infoList.add("Try to add this user into database......");
//do something else.
infoList.add("try to set entitlement for this user");
//do something else.
infoList.add("user registering ok.");
//do something else.
infoList.add("succeed......");
arg2.getSession().setAttribute("infoList", infoList);
arg3.getWriter().write("succeed");
return null;
}}