ajax定时刷新页面

前端之家收集整理的这篇文章主要介绍了ajax定时刷新页面前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

@H_403_1@1.jsp页面

用el表达式显示数据,页面为remind.jsp

<body style="width: 98%;">
		<%
			User_info ui = (User_info) session.getAttribute("userInfo");
			List<String> roleList = (List<String>) session
					.getAttribute("roleList");
		%>
		
		<a href="javascript:void(0)"
			onClick="parent.main.location.href='${basepath }/Actionsn!getAllInPage.do'"
			class="linkBLAT"><big id="xxcount">${sysnotecount}</big>条信息公告</a>


		<a href="javascript:void(0)"
			onClick="parent.main.location.href='${basepath }/ticklingAction!findMeetingBaseInfoByUserId.do'"
			class="linkBLAT"><big id="fkcount">${meetingbaseInfoCout}</big>条反馈会议</a>


		<a href="javascript:void(0)"
			onClick="parent.main.location.href='${basepath }/receiptModelAction!findMeetByUserId.do'"
			class="linkBLAT"><big id="hzcount">${meetingReceiptCount}</big>条需要回执会议</a>


		<a href="javascript:void(0)"
			onClick="parent.main.location.href='${basepath }/voteAction!findMeetingByUserId.do'"
			class="linkBLAT"><big id="dccount">${searchCount}</big>条调查</a>


		<a href="javascript:void(0)"
			onClick="parent.main.location.href='${basepath }/Alignmenttemplate!meetinglist.do'"
			class="linkBLAT"><big id="tscount">${maCount}</big>个待调试会场</a>
		<%
					if (ui.getUserid() == 0 || "1".equals(ui.getUser_type())
					|| "2".equals(ui.getUser_type())) {
		%>

		<a href="javascript:void(0)"
			onClick="parent.main.location.href='${basepath }/Alignmenttemplate!meetingSXlist.do'"
			class="linkBLAT"><big id="shcount">${shCount}</big>条待审核双向调试</a>
		<%
		}
		%>


</body>

2.javascrip

这里使用ajax实现无刷新页面,页面为remind.jsp

<script type="text/javascript">
		function fresh() {
	var xmlHttp;
	//根据不同浏览器初始化xmlHttp
	try {
		//IE 6+
		xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			//FireFox
			xmlHttp = new XMLHttpRequest();
		} catch (e) {
			try {
				//IE 5.5+
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("您的浏览器不支持Ajax!");
			}
		}
	}
	xmlHttp.open("POST","Actionmfm!freshJson.do",true);

	xmlHttp.send(null);
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
		var jsonobj = eval('(' + xmlHttp.responseText + ')');
		$("#xxcount").html(jsonobj.sysnotecount);
		$("#fkcount").html(jsonobj.meetingbaseInfoCout);		
		$("#hzcount").html(jsonobj.meetingReceiptCount);		
		$("#dccount").html(jsonobj.searchCount);		
		$("#tscount").html(jsonobj.maCount);		
		$("#shcount").html(jsonobj.shCount);						
		}
	}
}
		
		setInterval(fresh,5000); //指定2分钟刷新一次
</script>

3.java方法

@H_403_1@ajax里使用的方法

public String freshJson(){
		System.out.println("fresh开始");
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		
		User_info u = (User_info) ActionContext.getContext().getSession().get(
		"userInfo");
		int userId = u.getUserid();
		MeetingbaseInfoService infoservice = new MeetingbaseInfoService();
		//9999999999999999999
		int meetingbaseInfoCout =  modelService.findMeetingBaseInfoByUserId(u.getUserid()+"").size();
		session.setAttribute("meetingbaseInfoCout",meetingbaseInfoCout);

		meetList = modelService.findMainMeetingBaseInfoByUserId(u.getUserid()
				+ "");

		ActionContext.getContext().put("meetList",meetList);
		session.setAttribute("meetList",meetList);

		// 信息条数
		int sysnoteCount = this.sysNoticeService.getListByfour();
		System.out.println("===============sys" + sysnoteCount);
		session.setAttribute("sysnotecount",sysnoteCount);
		// 回执会议条数
		int meetingReceiptCount = this.mrms.getMeetingReceigpCount(userId);
		session.setAttribute("meetingReceiptCount",meetingReceiptCount);
		// 消息条数
		int msgCount = this.msginfoIpml.getCount(userId);
		session.setAttribute("msgCount",msgCount);
		// 获得需要调试的会议
		User_info userinfo = (User_info) request.getSession().getAttribute(
				"userInfo");
		// 需要进行调试的会议
		List malist = mad.getMeetingInfo(String.valueOf(u.getUserid()),"");
		// 需要审核的会议
		List shList = mad.getSxMeetingInfo(String.valueOf(u.getUserid()),"");
		List<Meeting_Alignment> meetingdebuglist = mas.getMeetingInfo(String
				.valueOf(userinfo.getUserid()),"");
		int searchCount = voteService.findMeetingByUserId(userId + "").size();
		session.setAttribute("searchCount",searchCount);
		session.setAttribute("sysnoticeList",sysnoticeList);
		session.setAttribute("meetingdebuglist",meetingdebuglist);
		session.setAttribute("malist",malist);
		session.setAttribute("maCount",malist.size());
		session.setAttribute("shCount",shList.size());
		session.setAttribute("shList",shList);
		String jsonStr = "{'sysnotecount':"+sysnoteCount+",'meetingbaseInfoCout':"+meetingbaseInfoCout+",'meetingReceiptCount':"+meetingReceiptCount+",'searchCount':"+searchCount+",'maCount':"+malist.size()+",'shCount':"+shList.size()+"}";
		try {
			inputStream = new ByteArrayInputStream(jsonStr.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return "remindjson";
	}

4.配置

@H_403_1@
<action name="Actionmfm!*" class="com.hb.action.meeting.MeetingFileManageAction"
			method="{1}">
			
			<result name="fresh">remind.jsp
			</result>
			 <result name="remindjson" type="stream">
				<param name="contentType">text/json</param>
				<param name="inputName">inputStream</param>
			</result>
</action>
原文链接:https://www.f2er.com/ajax/165808.html

猜你在找的Ajax相关文章