jquery.ajax与struts1.x,并附带定时调用功能

js代码:

 1 var sto;    //定时对象    
 2     function showButton(){
 3         var loginCode = <c:out value="${loginCode}" />;    //参数    
 4         $.ajax({ 
 5                type: "POST",
 6                url: "<%=path%>/homePage/wlservice/queryTaskList.do",
 7                data: "loginCode="+loginCode,
 8                //dataType:"json",
 9                success: function(msg){
10                    $("#taskDiv").html("");
11                    var json = eval('(' + msg + ')');    //解析json字符串    
12                    var html = "<table style='width:100%;  border-collapse:collapse;'>"
13                            +"<tr><th colspan='2' style='font-size:1.5em;  border:1px solid #00CCFF;  padding:3px 7px 2px 7px;'>仓储系统任务列表</th></tr>"
14                            +"<tr>"
15                            +"<th style='font-size:1.2em;  border:1px solid #00CCFF;  padding:3px 7px 2px 7px;'>任务描述</th>"
16                            +"<th style='font-size:1.2em;  border:1px solid #00CCFF;  padding:3px 7px 2px 7px;'>日期</th>"
17                            +"</tr>";
18                                                                             //遍历json list
19                    for(var i=0;i<json.list.length;i++){
20                        var temp = json.list[i];   
21                        html+="<tr>"
22                                +"<td style='font-size:1em;  border:1px solid #00CCFF;  padding:3px 7px 2px 7px;'><a href='#' style='color: blue;' onclick=\"functionForward('','"+temp.dbid+"','"+temp.name+"')\">"+temp.name+"</a></td>"
23                                +"<td style='font-size:1em;  border:1px solid #00CCFF;  padding:3px 7px 2px 7px;'>"+temp.create+"</td>"
24                                +"</tr>";
25                        
26                    }
27                    html +="</table>";
28                    $("#taskDiv").append(html);
29                },
30                error: function(XMLHttpRequest, textStatus, errorThrown) {
31                    alert("任务列表查询失败");
32                                                                             //错误时清除定时器
33                    clearTimeout(sto);
34                }
35             });
36         sto = setTimeout("showButton()", 10000);//重复定时调用
37 
38     }

struts1.x action

 1 public String  queryTaskList(){
 2         try {
 3             System.out.println("login:"+loginCode);
 4             taskList = service.queryTaskList(loginCode);
 5             
 6             
 7             //拼json字符串(因项目没有导入json对象包)
 8             String listString = "{list:[";
 9             for (int i = 0; i < taskList.size(); i++) {
10                 WlService temp = taskList.get(i);
11                 
12                 listString += "{dbid:\""+temp.getDbid()+"\""
13                         +",name:\""+temp.getName()+"\""
14                         +",create:\""+temp.getCreate()+"\"}";
15                 if (i!=(taskList.size()-1)) {
16                     listString+=",";
17                 }
18             }
19             listString += "]}";
20             System.out.println(listString);
21             getResponse().setContentType("text/html;charset=UTF-8");   //解决返回值中文乱码问题
22             getResponse().getWriter().print(listString);//输出ajax返回值
23             
24         } catch (Exception e) {
25             e.printStackTrace();
26         }
27         return null;//原本方法不需提供返回值,也不用配置转发路径
28     }

 

 

posted on 2013-04-19 11:05  看天空的星星  阅读(274)  评论(0编辑  收藏  举报

导航