Skip to content

Commit

Permalink
完善程序
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou committed Dec 31, 2014
1 parent b91c75d commit 7a70489
Show file tree
Hide file tree
Showing 22 changed files with 141 additions and 160 deletions.
9 changes: 8 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@
<version>3.2.5.RELEASE</version>
</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 @@ -110,7 +117,7 @@
<dependency>
<groupId>IKAnalyzer</groupId>
<artifactId>IKAnalyzer</artifactId>
<version>2012_u6</version>
<version>2012FF_u1</version>
</dependency>

<!-- mybatis -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,16 @@ public String login(){
}

@RequestMapping(value = "/login", method = RequestMethod.POST)
public String dashboard(String username, String pass, String code, HttpServletRequest request,
HttpServletResponse response){
public String dashboard(String username, String pass, HttpServletRequest request, HttpServletResponse response){
User user = userService.login(username, pass);
if(user == null){
request.setAttribute("msg", "用户名密码错误");
return "backend/login";
}

CookieUtil cookieUtil = new CookieUtil(request, response);
cookieUtil.setCookie(Constants.COOKIE_CONTEXT_ID, user.getId() + ":" + user.getId());
cookieUtil.setCookie(Constants.COOKIE_CONTEXT_ID, user.getId());
cookieUtil.setCookie(Constants.COOKIE_USER_NAME, username, 7 * 24 * 3600);

return "redirect:/backend/index";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import com.zblog.common.plugin.MapContainer;

@Controller
@Controller("bCommentController")
@RequestMapping("/backend/comments")
public class CommentController{

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/zblog/backend/controller/LinkController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zblog.common.dal.entity.Link;
import com.zblog.common.plugin.MapContainer;
import com.zblog.common.util.IdGenarater;
import com.zblog.service.LinkService;

Expand All @@ -34,6 +37,13 @@ public String insert(Link link){
linkService.insert(link);
return "redirect:/backend/links";
}

@ResponseBody
@RequestMapping(value="/{linkid}",method = RequestMethod.DELETE)
public Object remove(@PathVariable("linkid")String linkid){
linkService.deleteById(linkid);
return new MapContainer("success", true);
}

@RequestMapping(value = "/edit", method = RequestMethod.GET)
public String edit(Model model){
Expand Down
31 changes: 13 additions & 18 deletions src/main/java/com/zblog/backend/controller/PostController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Date;

import org.jsoup.Jsoup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
Expand All @@ -10,10 +11,12 @@
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.HtmlUtils;

import com.zblog.biz.PostManager;
import com.zblog.common.dal.entity.Post;
import com.zblog.common.plugin.MapContainer;
import com.zblog.common.plugin.PageModel;
import com.zblog.service.CategoryService;
import com.zblog.service.PostService;

Expand All @@ -29,44 +32,36 @@ public class PostController{

@RequestMapping(method = RequestMethod.GET)
public String index(@RequestParam(value = "page", defaultValue = "1") int page, Model model){
model.addAttribute("page", postService.listPost(page, 15));
PageModel pageModel = postService.listPost(page, 15);
model.addAttribute("page", pageModel);
return "backend/post/list";
}

@ResponseBody
@RequestMapping(method = RequestMethod.POST)
public Object insert(Post post, String txt, String uploadToken){
public Object insert(Post post, String uploadToken){
post.setId(postService.createPostid());
post.setLastUpdate(new Date());
post.setTitle(HtmlUtils.htmlEscape(post.getTitle().trim()));
post.setContent(post.getContent());
String cleanTxt = Jsoup.parse(post.getContent()).text();
post.setExcerpt(cleanTxt.length() > 350 ? cleanTxt.substring(0, 350) : cleanTxt);
post.setCreator("admin");
postManager.insertPost(post, uploadToken);
return new MapContainer("success", true);
}

@ResponseBody
@RequestMapping(value="/{postid}",method = RequestMethod.DELETE)
public Object remove(@PathVariable("postid")String postid){
@RequestMapping(value = "/{postid}", method = RequestMethod.DELETE)
public Object remove(@PathVariable("postid") String postid){
postManager.removePost(postid);
return new MapContainer("success", true);
}

@RequestMapping(value = "/edit", method = RequestMethod.GET)
public String edit(Model model){
model.addAttribute("categorys", categoryService.list());

return "backend/post/edit";
}

// @InitBinder
// public void initBinder(WebDataBinder binder){
// binder.registerCustomEditor(Reader.class, new PropertyEditorSupport(){
//
// @Override
// public void setAsText(String text) throws IllegalArgumentException{
// setValue(new StringReader(text));
// }
//
// });
// }

}
26 changes: 19 additions & 7 deletions src/main/java/com/zblog/backend/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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.RequestParam;
Expand All @@ -22,18 +23,29 @@ public class UserController{
private UserService userService;

@RequestMapping(method = RequestMethod.GET)
public String index(@RequestParam(value = "page", defaultValue = "1") int page,Model model){
model.addAttribute("users", userService.list(page, 15));
return "backend/users/list";
public String index(@RequestParam(value = "page", defaultValue = "1") int page, Model model){
model.addAttribute("page", userService.list(page, 15));
return "backend/user/list";
}

@ResponseBody

@RequestMapping(method = RequestMethod.POST)
public Object insert(User user){
public String insert(User user){
user.setId(IdGenarater.uuid19());
user.setCreateTime(new Date());
user.setLastUpdate(new Date());
return new MapContainer("success", userService.insert(user));
return "backend/user/list";
}

@ResponseBody
@RequestMapping(value = "/{userid}", method = RequestMethod.DELETE)
public Object remove(@PathVariable("userid") String userid){
userService.deleteById(userid);
return new MapContainer("success", true);
}

@RequestMapping(value = "/edit", method = RequestMethod.GET)
public String edit(){
return "backend/user/edit";
}

}
12 changes: 0 additions & 12 deletions src/main/java/com/zblog/biz/PostManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
import org.springframework.transaction.annotation.Transactional;

import com.zblog.common.dal.entity.Post;
import com.zblog.common.lucene.QueryBuilder;
import com.zblog.common.lucene.SearchEnginer;
import com.zblog.common.plugin.MapContainer;
import com.zblog.common.plugin.PageModel;
import com.zblog.common.util.constants.WebConstants;
import com.zblog.service.PostService;
import com.zblog.service.UploadService;
Expand Down Expand Up @@ -50,14 +47,5 @@ public void removePost(String postid){
uploadService.deleteByPostid(postid);
postService.deleteById(postid);
}

public PageModel search(String word,int pageIndex){
PageModel result = new PageModel(pageIndex, 15);
QueryBuilder builder = new QueryBuilder(SearchEnginer.postEnginer().getAnalyzer());
builder.addShould("title", word).addShould("content", word);
SearchEnginer.postEnginer().searchHighlight(builder, result);

return result;
}

}
32 changes: 20 additions & 12 deletions src/main/java/com/zblog/biz/aop/StaticTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.zblog.common.dal.entity.Category;
import com.zblog.common.dal.entity.Post;
import com.zblog.common.plugin.ApplicationContextUtil;
import com.zblog.common.plugin.MapContainer;
import com.zblog.common.util.DateUtils;
import com.zblog.common.util.constants.Constants;
import com.zblog.common.util.constants.WebConstants;
import com.zblog.service.CategoryService;
Expand Down Expand Up @@ -57,12 +57,27 @@ public void staticLinks(){
* @param post
*/
public void staticPost(Post post){
File file = new File(WebConstants.APPLICATION_PATH, DateUtils.formatDate("yyyy/MM", post.getCreateTime()));
if(!file.exists())
file.mkdirs();
FreeMarkerUtils.genHtml("/post.html", new File(file, "post-" + post.getId() + ".html"), post);
CategoryService categoryService = ApplicationContextUtil.getBean(CategoryService.class);
Category category = categoryService.loadById(post.getCategoryid());
MapContainer param = new MapContainer("domain", Constants.DOMAIN).put("post", post).put("categoryName",
category.getName());
FreeMarkerUtils.genHtml("/post.html",
new File(WebConstants.APPLICATION_PATH, "post/post-" + post.getId() + ".html"), param);
logger.info("staticPost");

staticRecent();
}

public void removeStaticPost(String postid){
String path = "post/post-" + postid + ".html";
File postFile = new File(WebConstants.APPLICATION_PATH, path);
postFile.delete();
logger.info("removeStaticPost");

staticRecent();
}

private void staticRecent(){
PostService postService = ApplicationContextUtil.getBean(PostService.class);
MapContainer param = new MapContainer("domain", Constants.DOMAIN);
param.put("posts", postService.listRecent());
Expand All @@ -71,11 +86,4 @@ public void staticPost(Post post){
logger.info("staticRecent");
}

public void removeStaticPost(String postid){
String path = "post/" + postid.substring(0, 4) + "/" + postid.substring(4, 6) + "/post-" + postid + ".html";
File postFile = new File(WebConstants.APPLICATION_PATH, path);
postFile.delete();
logger.info("removeStaticPost");
}

}
2 changes: 1 addition & 1 deletion src/main/java/com/zblog/common/dal/entity/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class Comment extends BaseEntity{
/* 内容 */
private String content;
/* 是否批准 */
private boolean approved;
private boolean approved = false;
/* 评论者的userAgent */
private String agent;
/* 父评论ID */
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/zblog/common/dal/mybatis/CategoryMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
select id, name,leftv, rightv, visible from category where name = #{name}
</select>

<select id="loadById" parameterType="java.lang.String" resultType="Category">
select id, name, visible from category where id = #{id}
</select>

<insert id="insert" parameterType="Category">
insert into category(id,name,leftv,rightv,visible,createTime)
values(#{id},#{name},#{leftv},#{rightv},#{visible},#{createTime})
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/zblog/common/dal/mybatis/PostMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</select>

<select id="listRecent" resultType="MapContainer">
select p.id, title from post where type='post' order by createTime desc limit 10
select id, title from post where type='post' order by createTime desc limit 10
</select>

<select id="listByCategory" parameterType="PageModel" resultType="MapContainer">
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/zblog/common/dal/mybatis/UserMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
</select>

<select id="list" parameterType="PageModel" resultType="MapContainer">
select id, nickName,realName, email from user
select id, nickName,realName, email, createTime from user
</select>

<insert id="insert" parameterType="User">
insert into user(id,nickName,realName,email,status,description,createTime,creator,lastUpdate)
values(#{id},#{nickName},#{#realName},#{email},#{status},#{description},#{createTime},#{creator},#{lastUpdate})
insert into user(id,nickName,realName,password,email,status,description,createTime,creator,lastUpdate)
values(#{id},#{nickName},#{realName},#{password},#{email},#{status},#{description},#{createTime},#{creator},#{lastUpdate})
</insert>

<delete id="deleteById">
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/com/zblog/common/lucene/DocConverter.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package com.zblog.common.lucene;

import java.util.Arrays;
import java.util.List;
import java.util.Collection;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexableField;

import com.zblog.common.plugin.MapContainer;

public class DocConverter{

private DocConverter(){}

private DocConverter(){
}

public static MapContainer convert(Document obj){
MapContainer mc = new MapContainer();
Expand All @@ -22,10 +23,13 @@ public static MapContainer convert(Document obj){
}

public static MapContainer convert(Document obj, String... filters){
return convert(obj, Arrays.asList(filters));
}

public static MapContainer convert(Document obj, Collection<String> filters){
MapContainer mc = new MapContainer();
List<String> list = Arrays.asList(filters);
for(IndexableField field : obj.getFields()){
if(list.contains(field.name()))
if(filters.contains(field.name()))
continue;
mc.put(field.name(), field.stringValue());
}
Expand Down
Loading

0 comments on commit 7a70489

Please sign in to comment.