Skip to content

Commit

Permalink
增加评论
Browse files Browse the repository at this point in the history
  • Loading branch information
shuzheng committed Mar 26, 2017
1 parent fd40de0 commit 291bfb4
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.zheng.cms.web.controller;

import com.zheng.cms.dao.model.CmsArticle;
import com.zheng.cms.dao.model.CmsComment;
import com.zheng.cms.dao.model.CmsCommentExample;
import com.zheng.cms.rpc.api.CmsArticleService;
import com.zheng.cms.rpc.api.CmsCommentService;
import com.zheng.common.base.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -12,6 +15,8 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
* 文章控制器
* Created by shuzheng on 2017/3/26.
Expand All @@ -25,10 +30,21 @@ public class ArticleController extends BaseController {
@Autowired
private CmsArticleService cmsArticleService;

@Autowired
private CmsCommentService cmsCommentService;

@RequestMapping(value = "/{articleId}", method = RequestMethod.GET)
public String index(@PathVariable("articleId") int articleId, Model model) {
CmsArticle article = cmsArticleService.selectByPrimaryKey(articleId);
model.addAttribute("article", article);
// 评论列表
CmsCommentExample cmsCommentExample = new CmsCommentExample();
cmsCommentExample.createCriteria()
.andArticleIdEqualTo(articleId)
.andStatusEqualTo((byte) 1);
cmsCommentExample.setOrderByClause("ctime desc");
List<CmsComment> comments = cmsCommentService.selectByExampleWithBLOBs(cmsCommentExample);
model.addAttribute("comments", comments);
return thymeleaf("/article/index");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.zheng.cms.web.controller;

import com.zheng.cms.common.constant.CmsResult;
import com.zheng.cms.common.constant.CmsResultConstant;
import com.zheng.cms.dao.model.CmsComment;
import com.zheng.cms.rpc.api.CmsCommentService;
import com.zheng.common.base.BaseController;
import com.zheng.common.util.RequestUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
* 评论控制器
* Created by shuzheng on 2017/3/26.
*/
@Controller
@RequestMapping(value = "/comment")
public class CommentController extends BaseController {

private static Logger _log = LoggerFactory.getLogger(CommentController.class);

@Autowired
private CmsCommentService cmsCommentService;

@RequiresPermissions("cms:comment:create")
@RequestMapping(value = "/create/{articleId}", method = RequestMethod.POST)
@ResponseBody
public Object create(@PathVariable("articleId") int articleId, CmsComment cmsComment, HttpServletRequest request) {
long time = System.currentTimeMillis();
cmsComment.setCtime(time);
cmsComment.setArticleId(articleId);
cmsComment.setUserId(1);
cmsComment.setStatus((byte) 1);
cmsComment.setIp(RequestUtil.getIpAddr(request));
cmsComment.setAgent(request.getHeader("User-Agent"));
int count = cmsCommentService.insertSelective(cmsComment);
return new CmsResult(CmsResultConstant.SUCCESS, count);
}

}
19 changes: 19 additions & 0 deletions zheng-ui/src/zheng-cms-web/article/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@
<div class="container" style="padding-bottom:80px;">
<h3 th:text="${article.title}">标题</h3>
<div th:utext="${article.content}">内容</div>
<form id="commentForm" th:action="@{'/comment/create/' + ${article.articleId}}" method="post">
<textarea id="" name="content" style="width:100%;height:50px;"></textarea>
<input id="submit" type="button" value="评论"/><!--{"code":1,"message":"success","data":1}-->
</form>
<dl>
<dt>评论列表</dt>
<dd th:each="comment : ${comments}" th:utext="${comment.content}">评论内容</dd>
</dl>
</div>
<!-- /.container -->
<!-- footer -->
Expand All @@ -102,5 +110,16 @@ <h3 th:text="${article.title}">标题</h3>
<script src="../js/jquery-1.11.2.min.js" th:src="@{${uiPath} + ${appName} + '/js/jquery-1.11.2.min.js'}"></script>
<script src="../js/bootstrap.min.js" th:src="@{${uiPath} + ${appName} + '/js/bootstrap.min.js'}"></script>
<script src="../js/main.js" th:src="@{${uiPath} + ${appName} + '/js/main.js'}"></script>
<script>
$('#submit').click(function () {
$.post($('#commentForm').attr('action'), $('#commentForm').serialize(), function (json) {
if (json.code == 1) {
location.reload();
} else {
alert(json.data);
}
})
});
</script>
</body>
</html>

0 comments on commit 291bfb4

Please sign in to comment.