技术点:Java、JSP、SSM框架,实现了个人博客系统
用户角色分为:普通用户、管理员、系统管理员
功能:发博客、博客分类、博客删除、评论、博客管理、日志等
看完本博客,你可以得到:
1> 学会SSM框架,相当于一个学习例子,可以自己拿来改
2> 拥有自己的私人博客系统
一、业务逻辑层:
博客用户逻辑类:
Blogger
/* 1: */ package com.blog.service.impl;
/* 2: */
/* 3: */ import com.blog.dao.BloggerDao;
/* 4: */ import com.blog.entity.Blogger;
/* 5: */ import com.blog.service.BloggerService;
/* 6: */ import javax.annotation.Resource;
/* 7: */ import org.springframework.stereotype.Service;
/* 8: */
/* 9: */ @Service("bloggerService")
/* 10: */ public class BloggerServiceImpl
/* 11: */ implements BloggerService
/* 12: */ {
/* 13: */ @Resource
/* 14: */ private BloggerDao bloggerDao;
/* 15: */
/* 16: */ public Blogger find()
/* 17: */ {
/* 18:23 */ return this.bloggerDao.find();
/* 19: */ }
/* 20: */
/* 21: */ public Blogger getByUserName(String userName)
/* 22: */ {
/* 23:27 */ return this.bloggerDao.getByUserName(userName);
/* 24: */ }
/* 25: */
/* 26: */ public Integer update(Blogger blogger)
/* 27: */ {
/* 28:31 */ return this.bloggerDao.update(blogger);
/* 29: */ }
/* 30: */ }
/* Location: D:\classes\
* Qualified Name: com.blog.service.impl.BloggerServiceImpl
* JD-Core Version: 0.7.0.1
*/
博客类型逻辑类:
BlogType
/* 1: */ package com.blog.service.impl;
/* 2: */
/* 3: */ import com.blog.dao.BlogTypeDao;
/* 4: */ import com.blog.entity.BlogType;
/* 5: */ import com.blog.service.BlogTypeService;
/* 6: */ import java.util.List;
/* 7: */ import java.util.Map;
/* 8: */ import javax.annotation.Resource;
/* 9: */ import org.springframework.stereotype.Service;
/* 10: */
/* 11: */ @Service("blogTypeService")
/* 12: */ public class BlogTypeServiceImpl
/* 13: */ implements BlogTypeService
/* 14: */ {
/* 15: */ @Resource
/* 16: */ private BlogTypeDao blogTypeDao;
/* 17: */
/* 18: */ public List<BlogType> countList()
/* 19: */ {
/* 20:26 */ return this.blogTypeDao.countList();
/* 21: */ }
/* 22: */
/* 23: */ public List<BlogType> list(Map<String, Object> map)
/* 24: */ {
/* 25:30 */ return this.blogTypeDao.list(map);
/* 26: */ }
/* 27: */
/* 28: */ public Long getTotal(Map<String, Object> map)
/* 29: */ {
/* 30:34 */ return this.blogTypeDao.getTotal(map);
/* 31: */ }
/* 32: */
/* 33: */ public Integer add(BlogType blogType)
/* 34: */ {
/* 35:38 */ return this.blogTypeDao.add(blogType);
/* 36: */ }
/* 37: */
/* 38: */ public Integer update(BlogType blogType)
/* 39: */ {
/* 40:42 */ return this.blogTypeDao.update(blogType);
/* 41: */ }
/* 42: */
/* 43: */ public Integer delete(Integer id)
/* 44: */ {
/* 45:46 */ return this.blogTypeDao.delete(id);
/* 46: */ }
/* 47: */ }
/* Location: D:\classes\
* Qualified Name: com.blog.service.impl.BlogTypeServiceImpl
* JD-Core Version: 0.7.0.1
*/
评论逻辑类:
Comment
/* 1: */ package com.blog.service.impl;
/* 2: */
/* 3: */ import com.blog.dao.CommentDao;
/* 4: */ import com.blog.entity.Comment;
/* 5: */ import com.blog.service.CommentService;
/* 6: */ import java.util.List;
/* 7: */ import java.util.Map;
/* 8: */ import javax.annotation.Resource;
/* 9: */ import org.springframework.stereotype.Service;
/* 10: */
/* 11: */ @Service("commentService")
/* 12: */ public class CommentServiceImpl
/* 13: */ implements CommentService
/* 14: */ {
/* 15: */ @Resource
/* 16: */ private CommentDao commentDao;
/* 17: */
/* 18: */ public int add(Comment comment)
/* 19: */ {
/* 20:26 */ return this.commentDao.add(comment);
/* 21: */ }
/* 22: */
/* 23: */ public List<Comment> list(Map<String, Object> map)
/* 24: */ {
/* 25:30 */ return this.commentDao.list(map);
/* 26: */ }
/* 27: */
/* 28: */ public Long getTotal(Map<String, Object> map)
/* 29: */ {
/* 30:34 */ return this.commentDao.getTotal(map);
/* 31: */ }
/* 32: */
/* 33: */ public Integer delete(Integer id)
/* 34: */ {
/* 35:38 */ return this.commentDao.delete(id);
/* 36: */ }
/* 37: */
/* 38: */ public int update(Comment comment)
/* 39: */ {
/* 40:42 */ return this.commentDao.update(comment);
/* 41: */ }
/* 42: */ }
/* Location: D:\classes\
* Qualified Name: com.blog.service.impl.CommentServiceImpl
* JD-Core Version: 0.7.0.1
*/
二、控制层:
博客控制层
BlogController
package com.blog.controller;
import com.blog.entity.Blog;
import com.blog.lucene.BlogIndex;
import com.blog.service.BlogService;
import com.blog.service.CommentService;
import com.blog.util.StringUtil;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
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.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping({"/blog"})
public class BlogController
{
@Resource
private BlogService blogService;
@Resource
private CommentService commentService;
private BlogIndex blogIndex = new BlogIndex();
@RequestMapping({"/articles/{id}"})
public ModelAndView details(@PathVariable("id") Integer id, HttpServletRequest request)
throws Exception
{
ModelAndView mav = new ModelAndView();
Blog blog = this.blogService.findById(id);
String keyWords = blog.getKeyWord();
if (StringUtil.isNotEmpty(keyWords))
{
String[] arr = keyWords.split(" ");
mav.addObject("keyWords", StringUtil.filterWhite(Arrays.asList(arr)));
}
else
{
mav.addObject("keyWords", null);
}
mav.addObject("blog", blog);
blog.setClickHit(Integer.valueOf(blog.getClickHit().intValue() + 1));
this.blogService.update(blog);
Map<String, Object> map = new HashMap();
map.put("blogId", blog.getId());
map.put("state", Integer.valueOf(1));
mav.addObject("commentList", this.commentService.list(map));
mav.addObject("pageCode", genUpAndDownPageCode(this.blogService.getLastBlog(id), this.blogService.getNextBlog(id), request.getServletContext().getContextPath()));
mav.addObject("mainPage", "foreground/blog/view.jsp");
mav.addObject("pageTitle", blog.getTitle() + "_Java开源博客系统");
mav.setViewName("mainTemp");
return mav;
}
@RequestMapping({"/q"})
public ModelAndView search(@RequestParam(value="q", required=false) String q, @RequestParam(value="page", required=false) String page, HttpServletRequest request)
throws Exception
{
if (StringUtil.isEmpty(page)) {
page = "1";
}
ModelAndView mav = new ModelAndView();
mav.addObject("mainPage", "foreground/blog/result.jsp");
List<Blog> blogList = this.blogIndex.searchBlog(q.trim());
Integer toIndex = Integer.valueOf(blogList.size() >= Integer.parseInt(page) * 10 ? Integer.parseInt(page) * 10 : blogList.size());
mav.addObject("blogList", blogList.subList((Integer.parseInt(page) - 1) * 10, toIndex.intValue()));
mav.addObject("pageCode", genUpAndDownPageCode(Integer.valueOf(Integer.parseInt(page)), Integer.valueOf(blogList.size()), q, Integer.valueOf(10), request.getServletContext().getContextPath()));
mav.addObject("q", q);
mav.addObject("resultTotal", Integer.valueOf(blogList.size()));
mav.addObject("pageTitle", "搜索关键字'" + q + "'结果页面_Java开源博客系统");
mav.setViewName("mainTemp");
return mav;
}
private String genUpAndDownPageCode(Blog lastBlog, Blog nextBlog, String projectContext)
{
StringBuffer pageCode = new StringBuffer();
if ((lastBlog == null) || (lastBlog.getId() == null)) {
pageCode.append("上一篇:没有了
");
} else {
pageCode.append("上一篇:"
+ lastBlog.getTitle() + "");
}
if ((nextBlog == null) || (nextBlog.getId() == null)) {
pageCode.append("下一篇:没有了
");
} else {
pageCode.append("下一篇:"
+ nextBlog.getTitle() + "");
}
return pageCode.toString();
}
private String genUpAndDownPageCode(Integer page, Integer totalNum, String q, Integer pageSize, String projectContext)
{
long totalPage = totalNum.intValue() % pageSize.intValue() == 0 ? totalNum.intValue() / pageSize.intValue() : totalNum.intValue() / pageSize.intValue() + 1;
StringBuffer pageCode = new StringBuffer();
if (totalPage == 0L) {
return "";
}
pageCode.append(");
pageCode.append(""
);
if (page.intValue() > 1) {
pageCode.append("上一页 ");
} else {
pageCode.append("上一页 ");
}
if (page.intValue() < totalPage) {
pageCode.append("下一页 ");
} else {
pageCode.append("下一页 ");
}
pageCode.append("");
pageCode.append("");
return pageCode.toString();
}
}
博客用户控制层
BloggerController
/* 1: */ package com.blog.controller;
/* 2: */
/* 3: */ import com.blog.entity.Blogger;
/* 4: */ import com.blog.service.BloggerService;
/* 5: */ import com.blog.util.CryptographyUtil;
/* 6: */ import javax.annotation.Resource;
/* 7: */ import javax.servlet.http.HttpServletRequest;
/* 8: */ import org.apache.shiro.SecurityUtils;
/* 9: */ import org.apache.shiro.authc.UsernamePasswordToken;
/* 10: */ import org.apache.shiro.subject.Subject;
/* 11: */ import org.springframework.stereotype.Controller;
/* 12: */ import org.springframework.web.bind.annotation.RequestMapping;
/* 13: */ import org.springframework.web.servlet.ModelAndView;
/* 14: */
/* 15: */ @Controller
/* 16: */ @RequestMapping({"/blogger"})
/* 17: */ public class BloggerController
/* 18: */ {
/* 19: */ @Resource
/* 20: */ private BloggerService bloggerService;
/* 21: */
/* 22: */ @RequestMapping({"/login"})
/* 23: */ public String login(Blogger blogger, HttpServletRequest request)
/* 24: */ {
/* 25:37 */ Subject subject = SecurityUtils.getSubject();
/* 26:38 */ UsernamePasswordToken token = new UsernamePasswordToken(blogger.getUserName(), CryptographyUtil.md5(blogger.getPassword(), "java1234"));
/* 27: */ try
/* 28: */ {
/* 29:40 */ subject.login(token);
/* 30:41 */ return "redirect:/admin/main.jsp";
/* 31: */ }
/* 32: */ catch (Exception e)
/* 33: */ {
/* 34:43 */ e.printStackTrace();
/* 35:44 */ request.setAttribute("blogger", blogger);
/* 36:45 */ request.setAttribute("errorInfo", "用户名或密码错误!");
/* 37: */ }
/* 38:46 */ return "login";
/* 39: */ }
/* 40: */
/* 41: */ @RequestMapping({"/aboutMe"})
/* 42: */ public ModelAndView aboutMe()
/* 43: */ throws Exception
/* 44: */ {
/* 45:57 */ ModelAndView mav = new ModelAndView();
/* 46:58 */ mav.addObject("blogger", this.bloggerService.find());
/* 47:59 */ mav.addObject("mainPage", "foreground/blogger/info.jsp");
/* 48:60 */ mav.addObject("pageTitle", "关于博主_Java开源博客系统");
/* 49:61 */ mav.setViewName("mainTemp");
/* 50:62 */ return mav;
/* 51: */ }
/* 52: */ }
/* Location: D:\classes\
* Qualified Name: com.blog.controller.BloggerController
* JD-Core Version: 0.7.0.1
*/
评论控制层
CommentController:
/* 1: */ package com.blog.controller;
/* 2: */
/* 3: */ import com.blog.entity.Blog;
/* 4: */ import com.blog.entity.Comment;
/* 5: */ import com.blog.service.BlogService;
/* 6: */ import com.blog.service.CommentService;
/* 7: */ import com.blog.util.ResponseUtil;
/* 8: */ import javax.annotation.Resource;
/* 9: */ import javax.servlet.http.HttpServletRequest;
/* 10: */ import javax.servlet.http.HttpServletResponse;
/* 11: */ import javax.servlet.http.HttpSession;
/* 12: */ import net.sf.json.JSONObject;
/* 13: */ import org.springframework.stereotype.Controller;
/* 14: */ import org.springframework.web.bind.annotation.RequestMapping;
/* 15: */ import org.springframework.web.bind.annotation.RequestParam;
/* 16: */
/* 17: */ @Controller
/* 18: */ @RequestMapping({"/comment"})
/* 19: */ public class CommentController
/* 20: */ {
/* 21: */ @Resource
/* 22: */ private CommentService commentService;
/* 23: */ @Resource
/* 24: */ private BlogService blogService;
/* 25: */
/* 26: */ @RequestMapping({"/save"})
/* 27: */ public String save(Comment comment, @RequestParam("imageCode") String imageCode, HttpServletRequest request, HttpServletResponse response, HttpSession session)
/* 28: */ throws Exception
/* 29: */ {
/* 30:45 */ String sRand = (String)session.getAttribute("sRand");
/* 31:46 */ JSONObject result = new JSONObject();
/* 32:47 */ int resultTotal = 0;
/* 33:48 */ if (!imageCode.equals(sRand))
/* 34: */ {
/* 35:49 */ result.put("success", Boolean.valueOf(false));
/* 36:50 */ result.put("errorInfo", "验证码填写错误!");
/* 37: */ }
/* 38: */ else
/* 39: */ {
/* 40:52 */ String userIp = request.getRemoteAddr();
/* 41:53 */ comment.setUserIp(userIp);
/* 42:54 */ if (comment.getId() == null)
/* 43: */ {
/* 44:55 */ resultTotal = this.commentService.add(comment);
/* 45: */
/* 46:57 */ Blog blog = this.blogService.findById(comment.getBlog().getId());
/* 47:58 */ blog.setReplyHit(Integer.valueOf(blog.getReplyHit().intValue() + 1));
/* 48:59 */ this.blogService.update(blog);
/* 49: */ }
/* 50:63 */ if (resultTotal > 0) {
/* 51:64 */ result.put("success", Boolean.valueOf(true));
/* 52: */ } else {
/* 53:66 */ result.put("success", Boolean.valueOf(false));
/* 54: */ }
/* 55: */ }
/* 56:69 */ ResponseUtil.write(response, result);
/* 57:70 */ return null;
/* 58: */ }
/* 59: */ }
/* Location: D:\classes\
* Qualified Name: com.blog.controller.CommentController
* JD-Core Version: 0.7.0.1
*/
博客管理员控制层:
BlogAdminController:
/* 1: */ package com.blog.controller.admin;
/* 2: */
/* 3: */ import com.blog.entity.Blog;
/* 4: */ import com.blog.entity.PageBean;
/* 5: */ import com.blog.lucene.BlogIndex;
/* 6: */ import com.blog.service.BlogService;
/* 7: */ import com.blog.util.ResponseUtil;
/* 8: */ import com.blog.util.StringUtil;
/* 9: */ import java.util.Date;
/* 10: */ import java.util.HashMap;
/* 11: */ import java.util.List;
/* 12: */ import java.util.Map;
/* 13: */ import javax.annotation.Resource;
/* 14: */ import javax.servlet.http.HttpServletResponse;
/* 15: */ import net.sf.json.JSONArray;
/* 16: */ import net.sf.json.JSONObject;
/* 17: */ import net.sf.json.JsonConfig;
/* 18: */ import org.springframework.stereotype.Controller;
/* 19: */ import org.springframework.web.bind.annotation.RequestMapping;
/* 20: */ import org.springframework.web.bind.annotation.RequestParam;
/* 21: */
/* 22: */ @Controller
/* 23: */ @RequestMapping({"/admin/blog"})
/* 24: */ public class BlogAdminController
/* 25: */ {
/* 26: */ @Resource
/* 27: */ private BlogService blogService;
/* 28: 40 */ private BlogIndex blogIndex = new BlogIndex();
/* 29: */
/* 30: */ @RequestMapping({"/save"})
/* 31: */ public String save(Blog blog, HttpServletResponse response)
/* 32: */ throws Exception
/* 33: */ {
/* 34: 51 */ int resultTotal = 0;
/* 35: 52 */ if (blog.getId() == null)
/* 36: */ {
/* 37: 53 */ resultTotal = this.blogService.add(blog).intValue();
/* 38: 54 */ this.blogIndex.addIndex(blog);
/* 39: */ }
/* 40: */ else
/* 41: */ {
/* 42: 56 */ resultTotal = this.blogService.update(blog).intValue();
/* 43: 57 */ this.blogIndex.updateIndex(blog);
/* 44: */ }
/* 45: 59 */ JSONObject result = new JSONObject();
/* 46: 60 */ if (resultTotal > 0) {
/* 47: 61 */ result.put("success", Boolean.valueOf(true));
/* 48: */ } else {
/* 49: 63 */ result.put("success", Boolean.valueOf(false));
/* 50: */ }
/* 51: 65 */ ResponseUtil.write(response, result);
/* 52: 66 */ return null;
/* 53: */ }
/* 54: */
/* 55: */ @RequestMapping({"/list"})
/* 56: */ public String list(@RequestParam(value="page", required=false) String page, @RequestParam(value="rows", required=false) String rows, Blog s_blog, HttpServletResponse response)
/* 57: */ throws Exception
/* 58: */ {
/* 59: 80 */ PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
/* 60: 81 */ Map<String, Object> map = new HashMap();
/* 61: 82 */ map.put("title", StringUtil.formatLike(s_blog.getTitle()));
/* 62: 83 */ map.put("start", Integer.valueOf(pageBean.getStart()));
/* 63: 84 */ map.put("size", Integer.valueOf(pageBean.getPageSize()));
/* 64: 85 */ List<Blog> blogList = this.blogService.list(map);
/* 65: 86 */ Long total = this.blogService.getTotal(map);
/* 66: 87 */ JSONObject result = new JSONObject();
/* 67: 88 */ JsonConfig jsonConfig = new JsonConfig();
/* 68: 89 */ jsonConfig.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor("yyyy-MM-dd"));
/* 69: 90 */ JSONArray jsonArray = JSONArray.fromObject(blogList, jsonConfig);
/* 70: 91 */ result.put("rows", jsonArray);
/* 71: 92 */ result.put("total", total);
/* 72: 93 */ ResponseUtil.write(response, result);
/* 73: 94 */ return null;
/* 74: */ }
/* 75: */
/* 76: */ @RequestMapping({"/delete"})
/* 77: */ public String delete(@RequestParam("ids") String ids, HttpServletResponse response)
/* 78: */ throws Exception
/* 79: */ {
/* 80:106 */ String[] idsStr = ids.split(",");
/* 81:107 */ for (int i = 0; i < idsStr.length; i++)
/* 82: */ {
/* 83:108 */ this.blogService.delete(Integer.valueOf(Integer.parseInt(idsStr[i])));
/* 84:109 */ this.blogIndex.deleteIndex(idsStr[i]);
/* 85: */ }
/* 86:111 */ JSONObject result = new JSONObject();
/* 87:112 */ result.put("success", Boolean.valueOf(true));
/* 88:113 */ ResponseUtil.write(response, result);
/* 89:114 */ return null;
/* 90: */ }
/* 91: */
/* 92: */ @RequestMapping({"/findById"})
/* 93: */ public String findById(@RequestParam("id") String id, HttpServletResponse response)
/* 94: */ throws Exception
/* 95: */ {
/* 96:126 */ Blog blog = this.blogService.findById(Integer.valueOf(Integer.parseInt(id)));
/* 97:127 */ JSONObject jsonObject = JSONObject.fromObject(blog);
/* 98:128 */ ResponseUtil.write(response, jsonObject);
/* 99:129 */ return null;
/* 100: */ }
/* 101: */ }
/* Location: D:\classes\
* Qualified Name: com.blog.controller.admin.BlogAdminController
* JD-Core Version: 0.7.0.1
*/
系统管理员控制层:
SystemAdminController
/* 1: */ package com.blog.controller.admin;
/* 2: */
/* 3: */ import com.blog.entity.Blog;
/* 4: */ import com.blog.entity.BlogType;
/* 5: */ import com.blog.entity.Blogger;
/* 6: */ import com.blog.entity.Link;
/* 7: */ import com.blog.service.BlogService;
/* 8: */ import com.blog.service.BlogTypeService;
/* 9: */ import com.blog.service.BloggerService;
/* 10: */ import com.blog.service.LinkService;
/* 11: */ import com.blog.util.ResponseUtil;
/* 12: */ import java.util.List;
/* 13: */ import javax.annotation.Resource;
/* 14: */ import javax.servlet.ServletContext;
/* 15: */ import javax.servlet.http.HttpServletRequest;
/* 16: */ import javax.servlet.http.HttpServletResponse;
/* 17: */ import net.sf.json.JSONObject;
/* 18: */ import org.springframework.stereotype.Controller;
/* 19: */ import org.springframework.web.bind.annotation.RequestMapping;
/* 20: */ import org.springframework.web.context.WebApplicationContext;
/* 21: */ import org.springframework.web.servlet.support.RequestContextUtils;
/* 22: */
/* 23: */ @Controller
/* 24: */ @RequestMapping({"/admin/system"})
/* 25: */ public class SystemAdminController
/* 26: */ {
/* 27: */ @Resource
/* 28: */ private BloggerService bloggerService;
/* 29: */ @Resource
/* 30: */ private BlogTypeService blogTypeService;
/* 31: */ @Resource
/* 32: */ private BlogService blogService;
/* 33: */ @Resource
/* 34: */ private LinkService linkService;
/* 35: */
/* 36: */ @RequestMapping({"/refreshSystem"})
/* 37: */ public String refreshSystem(HttpServletResponse response, HttpServletRequest request)
/* 38: */ throws Exception
/* 39: */ {
/* 40:55 */ ServletContext application = RequestContextUtils.getWebApplicationContext(request).getServletContext();
/* 41:56 */ Blogger blogger = this.bloggerService.find();
/* 42:57 */ blogger.setPassword(null);
/* 43:58 */ application.setAttribute("blogger", blogger);
/* 44: */
/* 45:60 */ List<BlogType> blogTypeCountList = this.blogTypeService.countList();
/* 46:61 */ application.setAttribute("blogTypeCountList", blogTypeCountList);
/* 47: */
/* 48:63 */ List<Blog> blogCountList = this.blogService.countList();
/* 49:64 */ application.setAttribute("blogCountList", blogCountList);
/* 50: */
/* 51:66 */ List<Link> linkList = this.linkService.list(null);
/* 52:67 */ application.setAttribute("linkList", linkList);
/* 53: */
/* 54:69 */ JSONObject result = new JSONObject();
/* 55:70 */ result.put("success", Boolean.valueOf(true));
/* 56:71 */ ResponseUtil.write(response, result);
/* 57:72 */ return null;
/* 58: */ }
/* 59: */ }
/* Location: D:\classes\
* Qualified Name: com.blog.controller.admin.SystemAdminController
* JD-Core Version: 0.7.0.1
*/
完整源码,交流Q:3459067873