Skip to content

Commit

Permalink
添加文章单独启用评论功能,调整优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou committed Jun 15, 2015
1 parent d05690c commit 1ae27cd
Show file tree
Hide file tree
Showing 25 changed files with 105 additions and 88 deletions.
48 changes: 29 additions & 19 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
Expand All @@ -9,11 +9,11 @@
<packaging>war</packaging>
<name>Zblog</name>
<description/>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>javax.servlet.jsp</groupId>
Expand All @@ -32,7 +32,12 @@
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>

<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
Expand Down Expand Up @@ -85,14 +90,14 @@
<version>3.2.5.RELEASE</version>
<scope>test</scope>
</dependency>

<!-- jsoup -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.8.1</version>
</dependency>

<!-- lucene -->
<dependency>
<groupId>org.apache.lucene</groupId>
Expand Down Expand Up @@ -125,7 +130,7 @@
<artifactId>IKAnalyzer</artifactId>
<version>2012FF_u1</version>
</dependency>

<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
Expand All @@ -137,20 +142,20 @@
<artifactId>mybatis-spring</artifactId>
<version>1.2.0</version>
</dependency>

<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.8</version>
</dependency>

<!-- freemarker -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.19</version>
</dependency>

<!-- commons -->
<dependency>
<groupId>commons-dbcp</groupId>
Expand All @@ -167,7 +172,7 @@
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
Expand All @@ -179,7 +184,7 @@
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>

<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
Expand All @@ -195,7 +200,7 @@
<!-- 项目编译时不需要 -->
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
Expand All @@ -208,7 +213,7 @@
<artifactId>fastjson</artifactId>
<version>1.1.31</version>
</dependency>

<!-- shiro -->
<dependency>
<groupId>org.apache.shiro</groupId>
Expand All @@ -225,18 +230,19 @@
<artifactId>shiro-web</artifactId>
<version>1.2.3</version>
</dependency>

<!-- xmlrpc支持 -->
<dependency>
<groupId>org.kohsuke.redstone</groupId>
<artifactId>redstone</artifactId>
<version>1.1.1</version>
</dependency>

</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
Expand All @@ -245,12 +251,16 @@
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<version>2.4</version>
<configuration>
<version>3.0</version>
<failOnMissingWebXml>false</failOnMissingWebXml>
<warName>zblog</warName>
<!-- 打包时忽略目录 -->
<packagingExcludes>log/**,post/**</packagingExcludes>
<includeEmptyDirectories>true</includeEmptyDirectories>
</configuration>
</plugin>
<plugin>
Expand All @@ -262,7 +272,7 @@
<httpConnector>
<port>9080</port>
</httpConnector>
<scanIntervalSeconds>20</scanIntervalSeconds>
<scanIntervalSeconds>10</scanIntervalSeconds>
<webApp>
<contextPath>/</contextPath>
</webApp>
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/zblog/biz/CommentManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public void setStatus(String commentid, String newStatus){
commentService.setStatus(commentid, newStatus);
int count = CommentConstants.TYPE_APPROVE.equals(newStatus) ? 1 : -1;
postService.addCcount(commentid, count);
if(count > 1){

}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/zblog/core/dal/entity/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Post extends BaseEntity{
/* 文章状态 */
private String pstatus = PostConstants.POST_PUBLISH;
/* 评论状态 */
private String cstatus = PostConstants.COMMENT_CLOSE;
private String cstatus = PostConstants.COMMENT_OPEN;
/* 评论数 */
private int ccount = 0;
/* 阅读数 */
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/zblog/core/dal/mapper/CommentMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface CommentMapper extends BaseMapper{

List<CommentVO> listRecent();

public List<MapContainer> listCountByGroupStatus();
List<MapContainer> listCountByGroupStatus();

/**
* 根据postid获取被批准的评论和指定creator的评论
Expand All @@ -27,6 +27,6 @@ public interface CommentMapper extends BaseMapper{
*
* @param commentid
*/
void setStatus(@Param("commentid") String commentid, @Param("status") String status);
int setStatus(@Param("commentid") String commentid, @Param("status") String status);

}
3 changes: 2 additions & 1 deletion src/main/java/com/zblog/core/dal/mybatis/PostMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
</insert>

<update id="update" parameterType="Post">
update post set title=#{title},excerpt=#{excerpt}, content=#{content, jdbcType=CLOB},parent=#{parent}, pstatus=#{pstatus}, categoryid=#{categoryid},lastUpdate=#{lastUpdate}
update post set title=#{title},excerpt=#{excerpt}, content=#{content, jdbcType=CLOB},parent=#{parent}, pstatus=#{pstatus},
cstatus=#{cstatus},categoryid=#{categoryid},lastUpdate=#{lastUpdate}
where id=#{id}
</update>

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/zblog/core/util/ServletUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static boolean isMultipartContent(HttpServletRequest request){
if(!"post".equals(request.getMethod().toLowerCase())){
return false;
}

String contentType = request.getContentType();
return contentType != null && contentType.toLowerCase().startsWith("multipart/");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ private PostConstants(){
public static final String TYPE_POST = "post";
public static final String TYPE_PAGE = "page";
/* 文章评论状态 */
public static final String COMMENT_POST = "open";
public static final String COMMENT_OPEN = "open";
public static final String COMMENT_CLOSE = "close";
/* 文章状态,发布、回收站 */
public static final String POST_PUBLISH = "publish";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.multipart.MultipartFile;
Expand All @@ -21,9 +22,9 @@
*
*/
public class ServletRequestReader{
HttpServletRequest request;
ServletRequest request;

public ServletRequestReader(HttpServletRequest request){
public ServletRequestReader(ServletRequest request){
this.request = request;
}

Expand Down Expand Up @@ -96,11 +97,11 @@ public boolean getAsBoolean(String key, boolean defaults){
// 获取基于虚拟路径的绝对路径
public String getRealPath(String path){
// 注: request.getServletContext().getRealPath(path);在Servlet3.0规范下才定义的
return request.getSession(true).getServletContext().getRealPath(path);
return request.getServletContext().getRealPath(path);
}

public String getDomain(){
return ServletUtils.getDomain(request);
return ServletUtils.getDomain((HttpServletRequest) request);
}

public Map<String, String> getQuerys(){
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/zblog/service/CommentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public List<CommentVO> listByPost(String postid, String creator){
return commentMapper.listByPost(postid, creator);
}

public void setStatus(String commentid, String newStatus){
commentMapper.setStatus(commentid, newStatus);
public int setStatus(String commentid, String newStatus){
return commentMapper.setStatus(commentid, newStatus);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import com.zblog.service.vo.OSInfo;
import com.zblog.web.backend.form.LoginForm;
import com.zblog.web.backend.form.validator.LoginFormValidator;
import com.zblog.web.filter.CookieRemberManager;
import com.zblog.web.support.CookieRemberManager;

@Controller
@RequestMapping("/backend")
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/zblog/web/filter/LoginFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import com.zblog.core.util.web.WebContext;
import com.zblog.core.util.web.WebContextFactory;
import com.zblog.service.shiro.StatelessToken;
import com.zblog.web.support.CookieRemberManager;
import com.zblog.web.support.Global;

/**
* 继承OncePerRequestFilter保证一次请求只过滤一次(以兼容不同的servlet container)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@
import org.springframework.web.util.HtmlUtils;

import com.zblog.core.dal.entity.Comment;
import com.zblog.core.dal.entity.Post;
import com.zblog.core.plugin.MapContainer;
import com.zblog.core.util.CookieUtil;
import com.zblog.core.util.IdGenerator;
import com.zblog.core.util.JsoupUtils;
import com.zblog.core.util.ServletUtils;
import com.zblog.core.util.StringUtils;
import com.zblog.core.util.constants.OptionConstants;
import com.zblog.core.util.constants.PostConstants;
import com.zblog.service.CommentService;
import com.zblog.service.OptionsService;
import com.zblog.service.PostService;
import com.zblog.web.front.validator.CommentValidator;

@Controller
Expand All @@ -30,14 +33,13 @@ public class CommentController{
@Autowired
private CommentService commentService;
@Autowired
private PostService postService;
@Autowired
private OptionsService optionsService;

@ResponseBody
@RequestMapping(method = RequestMethod.POST)
public Object post(Comment comment, HttpServletRequest request, HttpServletResponse response){
if(!"true".equals(optionsService.getOptionValue(OptionConstants.ALLOW_COMMENT)))
return new MapContainer("success", false).put("msg", "当前禁止评论");

CookieUtil cookieUtil = new CookieUtil(request, response);
if(StringUtils.isBlank(comment.getCreator())){
comment.setCreator(cookieUtil.getCookie("comment_author"));
Expand All @@ -47,11 +49,20 @@ public Object post(Comment comment, HttpServletRequest request, HttpServletRespo

MapContainer form = CommentValidator.validate(comment);
if(!form.isEmpty()){
return form.put("success", true);
return form.put("success", false);
}

if(StringUtils.isBlank(comment.getParent()))
if(!"true".equals(optionsService.getOptionValue(OptionConstants.ALLOW_COMMENT)))
return new MapContainer("success", false).put("msg", "当前禁止评论");

Post post = postService.loadById(comment.getPostid());
if(post == null || PostConstants.COMMENT_CLOSE.equals(post.getCstatus())){
return new MapContainer("success", false).put("msg", "当前禁止评论");
}

if(StringUtils.isBlank(comment.getParent())){
comment.setParent(null);
}

/* 根据RFC-2109中的规定,在Cookie中只能包含ASCII的编码 */
cookieUtil.setCookie("comment_author", comment.getCreator(), "/", false, 365 * 24 * 3600, true);
Expand Down
14 changes: 2 additions & 12 deletions src/main/java/com/zblog/web/listener/InitApplicationListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,14 @@
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.zblog.core.plugin.ApplicationContextUtil;
import com.zblog.core.util.constants.CategoryConstants;
import com.zblog.core.util.constants.WebConstants;
import com.zblog.service.CategoryService;

public class InitApplicationListener implements ServletContextListener{
@Override
public void contextInitialized(ServletContextEvent sce){
WebConstants.APPLICATION_PATH = sce.getServletContext().getRealPath("/");

initApp();
}

private void initApp(){
CategoryService categoryService = ApplicationContextUtil.getBean(CategoryService.class);
if(categoryService.loadByName(CategoryConstants.ROOT) == null){
categoryService.init();
}
/* 给log4j设置环境变量,必须要在jvm加载log4j.properties前设置 */
System.setProperty("log4jHome", WebConstants.APPLICATION_PATH);
}

@Override
Expand Down
Loading

0 comments on commit 1ae27cd

Please sign in to comment.