需求:页面中评论部分,要求在页面所有内容加载完毕后再加载评论列表。
思路:在ready中初始化评论的内容,ready中的方法在页面初始化后才执行。
实现:ready方法最后添加代码 $('#commentDiv').load("<%=basePath%>cmntController/getCommentData.do?entityId=4&dataid="+objectId);
id为commentDiv 的div中存在评论的内容。
后台代码:
@RequestMapping(value="getCommentData")
public ModelAndView getCommentData(HttpServletRequest request, Integer entityId,
String dataid){
ModelAndView model = new ModelAndView();
// 加载评论信息,跳转到评论列表页...
model.setViewName("frame/comments");
return model;
}
comments代码:comments中可以使用包含它的页面的js包,并且可以访问到那个页面的属性、调用那个页面的方法。
<%@ page contentType="text/html;charset=UTF-8" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<div class="addlook-header">
这是异步加载出的页面内容
</div>
<div class="comments-content">
<!-- jstl循环取出评论列表,略 -->
</div>
ajax的实现不能满足要求,如:
$.get("titlenews.jsp?id=1", function(data){
$("#divitem1").html(data);
});
这种方式就算将链接改为跳转到后台,如果后台为跳转到jsp,则无法正常加载jsp数据,前台请求会走到error中,json请求更不符合要求。