JavaWeb项目,用ajax请求(不刷新页面)每隔一段时间请求后台json数据,实现实时更新数据需求

1、javascript前端jQuery代码:

/*动态加载工厂函数*/
    $(function(){
    /*实时查询信息*/
    realTimeQuery();
    });

/*实时信息*/

    function realTimeQuery(){
    $.ajax("realTime.do",{
 type:"post",
 data:{pageSize:$("#pageSize").val()},
 dataType:"json",
 success:function(data){        //参数data为后台获取的数据
    $(".log").remove();
               for(i in data.data){        //data.data指的是数组,i为数组的索引
                       var tr;

               tr=  '<td>'+data.data[i].received+'</td>'+'<td>'+data.data[i].sourceIp+'</td>'+

                                       '<td>'+data.data[i].sourceHostname+'</td>'+'<td>'+data.data[i].facility+'</td>'+

                                       '<td>'+data.data[i].severity+'</td>'+'<td>'+data.data[i].origin+'</td>'+

                                       '<td>'+data.data[i].tag+'</td>'+

                                       '<td colspan="3">'+data.data[i].message+'</textarea></td>';

    if(i%2==0){
$("#table1").append('<tr class="log" style="background-color:lightblue;">'+tr+'</tr>');
    }else{
$("#table1").append('<tr class="log">'+tr+'</tr>');
    }
               }
},
error:function(){
console.log("实时信息,请求失败");
    clearTimeout(t);
    }
    });
       t=setTimeout('realTimeQuery()', 20000);//每隔20秒,查询一次。

    };

2、java后端代码:

/**
 * 信息处理控制层
 * @author Administrator
 *
 */
@Controller
public class SyslogController {
@Resource
private SyslogService syslogService;

        /**
* 实时信息展示
*/
@RequestMapping("/realTime.do")
public  String realTimeQuery(Integer pageSize,HttpServletRequest request,HttpServletResponse response){
//判断是否为登录用户
String userCode = (String)request.getSession().getAttribute("userCode");
if(userCode!=null&&userCode!=""){
//实时查询
Page page = new Page(1,pageSize,pageSize,"","","","","","","","");
List<Syslog> list = this.syslogService.getSyslogList(page);
//返回数据
JSONArray jsonArray = new JSONArray();
for(Syslog syslog:list){
JSONObject jsonLog = new JSONObject();
jsonLog.put("received", syslog.getReceived());
jsonLog.put("sourceIp", syslog.getSourceIp());
jsonLog.put("sourceHostname", syslog.getSourceHostname());
jsonLog.put("facility", syslog.getFacility());
jsonLog.put("severity", syslog.getSeverity());
jsonLog.put("origin", syslog.getOrigin());
jsonLog.put("tag", syslog.getTag());
jsonLog.put("message", syslog.getMessage());
jsonArray.add(jsonLog);
}
JSONObject result = new JSONObject();
result.put("data", jsonArray);
try {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json" );
PrintWriter wr = response.getWriter();
wr.write(result.toString());
wr.flush();
wr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return "/main.jsp";

}

}

3、web.xml配置文件

<!-- DispatcherServlet请求分发 -->
  <servlet>
  <servlet-name>DispatcherServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext.xml</param-value>
  </init-param>
  </servlet>
  <servlet-mapping>
  <servlet-name>DispatcherServlet</servlet-name>
  <url-pattern>*.do</url-pattern>

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页