ajax嵌套实现默认点赞显示:
function postList() {
<%--获取评论列表--%>
var html = "";
$.ajax({
//async: false,
type: "POST",
dataType: 'json',
url: "comment/getPost",//content:textareaContent,userName:userName,siteID:firmId,articleID:contentId
data: {
articleID: '${contentDetail.contentId}',
siteID: '${contentDetail.firmId}',
userName: '${userInfo.loginName}',
nickName: '${userInfo.nickName}',
uCenterUId: '${userInfo.id}',
articleTitle: '${contentDetail.title}',
pno: 1
},
success: function (data) {
$.each(data.list, function (i, item) {
var nowUserID = $('#userIDSpan').html();
html += '<li id = "del' + item.id + '" class="dis-li">';
html += '<h3 οnlοad="getUserName(' + item.userID + ')" class="dis-li-per">' + item.nickName + '</p>'; //这里有一个动态获取用户名的逻辑
//发送jasx请求根据用户的id查询用户名
html += '<p class="dis-li-cont">' + item.content + '</p>';
html += '<div class="dis-li-bot">';
html += '<span>' + item.createTime + '</span>';
html += '<span class="dis-li-botr">';
//============点赞
html += '<a οnclick="likePost(' + item.id + ',' + item.topicID + ',' + item.siteID + ',' + item.uCenterUId + ')" class="dis-zan" >';
$.ajax({
async: false,
type: "POST",
dataType: 'json',
url: "comment/isLiked",
//url: BBS_SERVICE_URL+"topic/",//comment/topic/
data: {
postID: item.id,
uCenterUId: '${userInfo.id}',
},
success: function (data) {
if (data == 0)
html += '<img id = "img' + item.id + '" src="resources/cms/default/www/images/discuss/zan.png" alt="">';
else
html += '<img id = "img' + item.id + '" src="resources/cms/default/www/images/discuss/like.png" alt="">';
}
});
html += '<span id = "ilike' + item.id + '">' + item.iLike + '<span/></a>';
//=============删除
if (item.userID == nowUserID) {
html += '<a οnclick="delConfirm(' + item.id + ',' + item.topicID + ',' + item.siteID + ')" class="dis-reply">删除</a>';
}
//=============举报
html += '<a οnclick="reportP(' + item.id + ',' + item.uCenterUId + ',' + item.siteID + ')" id = "report' + item.id + '" class="dis-report">举报</a>';
html += '</span>';
html += '</div>';
html += '</li>';
})
$(".new-dis-cont").html(html);
}
});
}
后台接口实现:
"comment/getPost":
//查询评论列表 @RequestMapping("/getPost") @ResponseBody public Object getPost(HttpServletRequest request, Model model, Comment commet) { //contentDetail[0].content_id }',siteID:'${contentDetail[0].firm_id }',userName:'${userInfo.loginName}',pno:1 UserInfo userInfo = super.getUserFromSession(); CmsSite site = CmsUtils.getSite(request); if (null != site) { commet.setSiteID(site.getFirmId()); } if (null != userInfo) { commet.setNickName(userInfo.getNickName()); commet.setuCenterUId(userInfo.getId()); commet.setUserName(userInfo.getLoginName()); } String url = BBS_SERVICE + "topic"; String postList = null; try { postList = HttpClientUtils.postParameters(url, MapUtil.objectToMap(commet, null)); } catch (Exception e) { e.printStackTrace(); } return postList; }
url : "comment/isLiked" ,:/** * 判断用户是否已点赞 * * @return */ @RequestMapping("/isLiked") @ResponseBody public Object isLiked(HttpServletRequest request, Comment commet) { String userPost = "0"; try { if (!StringUtils.isEmpty(commet.getuCenterUId())) { String url = BBS_SERVICE + "post/isThumbsUpPost/" + commet.getuCenterUId() + "-" + commet.getPostID(); userPost = HttpClientUtils.get(url); } } catch (Exception e) { e.printStackTrace(); } return userPost; }
经测试有效