From 26bd9a30c87eecf2e31c4098d6ff8eb9b3971ce6 Mon Sep 17 00:00:00 2001 From: tidezyc Date: Fri, 3 May 2013 17:49:19 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=84=E8=AE=BA=E7=B3=BB=E7=BB=9F=E6=9B=B4?= =?UTF-8?q?=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WebRoot/WEB-INF/views/comment.html | 58 ++++++++------ WebRoot/css/comnon.css | 51 ++++++++----- WebRoot/js/comment.js | 76 ++++++++++++++----- .../controller/CommentController.java | 72 ++++++++++++------ 4 files changed, 173 insertions(+), 84 deletions(-) diff --git a/WebRoot/WEB-INF/views/comment.html b/WebRoot/WEB-INF/views/comment.html index a5c38b5..5547b8e 100644 --- a/WebRoot/WEB-INF/views/comment.html +++ b/WebRoot/WEB-INF/views/comment.html @@ -1,38 +1,48 @@
- 评论    共${count}条评论 + 评论    共${comment_list.totalRow}条评论 发表评论
- <#list comment_list.list as comment> -
- - - -
-

- ${comment_index+1}楼:${comment.comment_unickname}  发布于${comment.comment_date} - - 回复 - -

-

- ${comment.comment} -

+
+ <#list comment_list.list as comment> +
+ + + +
+

+ ${(comment_list.pageNumber-1)*comment_list.pageSize+comment_index+1}楼:${comment.comment_unickname}  发布于${comment.comment_date} + + 回复 + +

+

+ ${comment.comment} +

+
+
+ +
+ <#include "/layout/_paginate.html" /> + <@paginate currentPage=comment_list.pageNumber totalPage=comment_list.totalPage actionUrl="javascript:void();#" />
- -
+
<#if auth_user??> -
-
- 发表评论 -
+
+
+
+ 发表评论 +
+
+
<#else> - 登录后评论 - + + 登录 + 后评论
diff --git a/WebRoot/css/comnon.css b/WebRoot/css/comnon.css index 72f8070..b32dc91 100644 --- a/WebRoot/css/comnon.css +++ b/WebRoot/css/comnon.css @@ -419,47 +419,60 @@ body { } .com_cnt { - margin-left:45px; + margin-left: 45px; } .com_cnt .meta { } .com_cnt .cnt { - padding-left:3px; + padding-left: 3px; } + #comment_add { padding: 10px 0 0 5px; } + #comment_add .editor { - width: 450px; - min-height: 100px; - _height: 100px; - padding:5px; - word-wrap: break-word; + width: 450px; + min-height: 100px; + _height: 100px; + padding: 5px; + word-wrap: break-word; overflow-x: hidden; overflow-y: auto; _overflow-y: visible; - border: 1px solid #a0b3d6; + border: 1px solid #a0b3d6; font-size: 12px; - outline: 0; + outline: 0; } -#comment_add .editor .metion{ - vertical-align: baseline; - cursor: text; - display: inline-block; - font-size: 1em; - border: none; - background: none; - overflow: visible; - padding: 0; - margin: 0 1px; +#comment_add .editor .metion { + vertical-align: baseline; + cursor: text; + display: inline-block; + font-size: 1em; + border: none; + background: none; + overflow: visible; + padding: 0; + margin: 0 1px; } + #comment_add .submit { margin-top: 5px; + float:left; } +#submit_info { + width: 100px; + float:left; + margin:10px; +} + +/* + * comment END + */ .footer { width: 100%; padding: 20px 0; diff --git a/WebRoot/js/comment.js b/WebRoot/js/comment.js index c9df468..e9442c7 100644 --- a/WebRoot/js/comment.js +++ b/WebRoot/js/comment.js @@ -3,24 +3,9 @@ */ $com_dis = $('#comment_display'); $com_id = $com_dis.attr('rel'); -$.ajax({ - type: "get", - url: "/comment/" + $com_id, - beforeSend: function(XMLHttpRequest){ - $com_dis.append('
'); - }, - success: function(data, status){ - $com_dis.html(data); - }, - complete: function(XMLHttpRequest, textStatus){ - //HideLoading(); - }, - error: function(){ - $com_dis.html('加载评论粗无'); - } -}); +$locked = false; -function reply(uname, nickname){ +var reply = function(uname, nickname){ $editor = $('#comment_add .editor'); var at = document.createElement('button'); at.name = '@{uname:' + uname + ',unickname:' + nickname + '}'; @@ -33,9 +18,62 @@ function reply(uname, nickname){ $editor.focus(); } -/* +var submit_form = function(){ + if ($locked) { + return; + } + $locked = true; + $clone = $('#comment_add .editor').clone(); + $button = $clone.children('button.metion'); + $name = $button.attr('name'); + $button.remove(); + $.ajax({ + type: "post", + url: "/comment/add/", + data: { + 'target': $com_id, + 'motion': $name, + 'comment': $clone.html() + }, + beforeSend: function(XMLHttpRequest){ + $('#submit_info').html("").append(''); + }, + success: function(data, status){ + $('#submit_info').html("").append(data); + comment_load(false, $('#comment_page .selected').html()); + $locked = false; + }, + error: function(){ + $('#submit_info').html("").append("评论系统出错"); + } + }); +} - * 光标最后 END +var comment_load = function(cache, page){ + $url = cache ? "/comment/" + $com_id + "-" + page : "/comment/update/" + $com_id + "-" + page; + $.ajax({ + type: "get", + url: $url, + beforeSend: function(XMLHttpRequest){ + $com_dis.append('
'); + }, + success: function(data, status){ + $com_dis.html(data); + $('#comment_add .submit').click(function(){ + submit_form(); + }); + $('#comment_page a').click(function(e){ + comment_load(true, $(this).attr('href').split('#')[1]); + }); + }, + error: function(){ + $com_dis.html('加载评论错误'); + } + }); +} +comment_load(true, 1);// 初始化加载评论 +/* + * 光标最后 END */ diff --git a/src/me/thinkjet/controller/CommentController.java b/src/me/thinkjet/controller/CommentController.java index 7e9353c..e7a411a 100644 --- a/src/me/thinkjet/controller/CommentController.java +++ b/src/me/thinkjet/controller/CommentController.java @@ -1,46 +1,74 @@ package me.thinkjet.controller; +import java.util.HashMap; +import java.util.Map; + +import me.thinkjet.auth.AuthManager; +import me.thinkjet.auth.AuthUser; import me.thinkjet.model.Comment; -import com.jfinal.aop.Before; import com.jfinal.core.Controller; import com.jfinal.ext.route.ControllerBind; -import com.jfinal.plugin.ehcache.CacheInterceptor; -import com.jfinal.plugin.ehcache.CacheName; +import com.jfinal.plugin.activerecord.Page; +import com.jfinal.plugin.ehcache.CacheKit; /** * CommonController */ @ControllerBind(controllerKey = "/comment", viewPath = "/") public class CommentController extends Controller { - @Before(CacheInterceptor.class) - @CacheName("comment") public void index() { String target = this.getPara(0); int page = this.getParaToInt(1, 1); this.setAttr("target", target); this.setAttr("page", page); this.setAttr("comment_list", Comment.dao.paginateByCache("comment", - target + "_" + page, page, 6, "select c.*,u.avatar as avatar", + target + "_" + page, page, 20, "select c.*,u.avatar as avatar", "from comment c left join users u " + "on c.comment_uname=u.username " - +"where c.target=? " - + "order by c.id asc", target)); - this.setAttr( - "count", - Comment.dao.findByCache("comment", "count_" + target, - "select count(id) from comment where target=?", target) - .get(0).getAttrValues()[0]); - System.out.println(Comment.dao.paginateByCache("comment", - target + "_" + page, page, 6, "select c.*,u.avatar", - "as avatar from comment c left join users u " - + "on c.target=? and c.comment_uname=u.username " - + "order by c.id asc", target)); + + "where c.target=? " + "order by c.id asc", target)); this.render("comment.html"); } - public void add(){ - Comment comment = getModel(Comment.class); - comment.save(); + + public void update() { + String target = this.getPara(0); + int page = this.getParaToInt(1, 1); + this.setAttr("target", target); + this.setAttr("page", page); + Page comments = Comment.dao.paginate(page, 20, + "select c.*,u.avatar as avatar", + "from comment c left join users u " + + "on c.comment_uname=u.username " + + "where c.target=? " + "order by c.id asc", target); + CacheKit.put("comment", target + "_" + page, comments); + this.setAttr("comment_list", comments); + this.render("comment.html"); + } + + public void add() { + Map attrs = new HashMap(); + if (this.getPara("target") == null || this.getPara("comment") == null + || AuthManager.getSession(this) == null) { + this.renderHtml("评论失败"); + return; + } + attrs.put("target", this.getPara("target")); + attrs.put("comment", this.getPara("comment")); + AuthUser user = AuthManager.getSession(this); + attrs.put("comment_uname", user.getUser().getStr("username")); + attrs.put("comment_unickname", user.getUser().getStr("name") == null + || user.getUser().getStr("name").equals("") ? user.getUser() + .getStr("username") : user.getUser().getStr("name")); + if (this.getPara("motion") != null) { + String motion = this.getPara("motion"); + String[] motions = motion.substring(2, motion.length() - 1).split( + ","); + String uname = motions[0].split(":")[1]; + String unickname = motions[1].split(":")[1]; + attrs.put("at_uname", uname); + attrs.put("at_unickname", unickname); + } + new Comment().setAttrs(attrs).save(); + this.renderHtml("评论成功!"); } - }