飞道的博客

Java项目:新闻推荐管理系统(java+SSM+JavaScript+Ajax+Mysql)

246人阅读  评论(0)

源码获取:俺的博客首页 "资源" 里下载!

项目介绍

本项目新闻推荐管理系统;

前台:

登录、首页、全部新闻、系统特色、猜你喜欢、分类、评论

后台:
(1)文件管理:文件列表。
(2)用户管理:用户管理。
(3)新闻管理:新闻管理。
(4)三联管理:联动管理。
(5)新闻审核:新闻的审核和管理。

技术栈

SSM+mysql+layui+CSS+JavaScript

使用说明

1. 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件;
2. 使用IDEA/Eclipse/MyEclipse导入项目,导入成功后请执行maven clean;maven install命令,然后运行;
3. 将项目中application.properties配置文件中的数据库配置改为自己的配置;
4. 运行项目,在浏览器中输入http://localhost:8080 访问

 

 

 

 用户管理控制层:


  
  1. @Controller
  2. @RequestMapping("UsersServlet")
  3. public class UsersController {
  4. private UsersDao usersDao = new UsersDao();
  5. @Autowired
  6. private HttpServletRequest request;
  7. @RequestMapping("/loginadmin")
  8. public String loginadmin () {
  9. String username = request.getParameter( "username");
  10. String password = request.getParameter( "password");
  11. Users users = usersDao.login(username, password);
  12. if (users != null) {
  13. request.getSession().setAttribute( "loginUsers", users);
  14. return "admin_index";
  15. } else {
  16. request.setAttribute( "msg", "登录失败,账号密码不匹配");
  17. return "admin_login";
  18. }
  19. }
  20. @RequestMapping("/userlogin")
  21. public String userlogin () {
  22. String userName = request.getParameter( "username");
  23. String password = request.getParameter( "password");
  24. String clientCheckcode = request.getParameter( "validateCode");
  25. String serverCheckcode = (String) request.getSession().getAttribute( "checkcode");
  26. if (clientCheckcode.equals(serverCheckcode)) {
  27. // 2. 去访问dao , 看看是否满足登录。
  28. Users Users = usersDao.userlogin(userName, password);
  29. // 3. 针对dao的返回结果,做出响应
  30. if (Users != null) {
  31. request.getSession().setAttribute( "usersLogin", Users);
  32. CategoryDao categoryDao = new CategoryDao();
  33. List<Category> categoryList = categoryDao.queryAll();
  34. request.setAttribute( "categoryList", categoryList.stream().filter(x -> x.getState().equals( "1")).collect(Collectors.toList()));
  35. return "index";
  36. } else {
  37. request.setAttribute( "error", "用户名或密码错误!");
  38. return "login";
  39. }
  40. } else {
  41. request.setAttribute( "error", "登录失败,验证码不正确!");
  42. return "login";
  43. }
  44. }
  45. @RequestMapping("/userreg")
  46. public String userreg () {
  47. String username = request.getParameter( "username");
  48. String password = request.getParameter( "password");
  49. String account = request.getParameter( "account");
  50. String email = request.getParameter( "email");
  51. String password2 = request.getParameter( "password2");
  52. if (!password.equals(password2)) {
  53. request.setAttribute( "error", "注册失败,密码与确认密码不一致!");
  54. return "reg";
  55. } else {
  56. boolean isSuccess = usersDao.isReg(account);
  57. if (!isSuccess) {
  58. request.setAttribute( "error", "注册失败,该用户名已存在!");
  59. return "reg";
  60. } else {
  61. usersDao.reg(username, account, password, email);
  62. request.setAttribute( "error", "注册成功!");
  63. return "reg";
  64. }
  65. }
  66. }
  67. @RequestMapping("/listforadmin")
  68. public String listforadmin () {
  69. List<Users> list = usersDao.getUsers();
  70. request.setAttribute( "list", list);
  71. return "listusers";
  72. }
  73. @RequestMapping("/del")
  74. public String del (Integer id) {
  75. usersDao.del(id);
  76. List<Users> list = usersDao.getUsers();
  77. request.setAttribute( "list", list);
  78. return "listusers";
  79. }
  80. }

新闻管理控制层:


  
  1. @Controller
  2. @RequestMapping( "NewsServlet")
  3. public class NewsController {
  4. @Autowired
  5. private HttpServletRequest request;
  6. private NewsDao newsDao = new NewsDao();
  7. private CategoryDao categoryDao = new CategoryDao();
  8. private CommentsDao commentsDao = new CommentsDao();
  9. @RequestMapping( "/publish")
  10. public String publish( ){
  11. String title = request. getParameter( "title");
  12. String content = request. getParameter( "content");
  13. String categoryid = request. getParameter( "categoryid");
  14. Users users = ( Users) request. getSession(). getAttribute( "loginUsers");
  15. Integer usersId = users. getId();
  16. Integer deptid = users. getDeptid();
  17. News news = new News();
  18. news. setCategoryid( Integer. parseInt(categoryid));
  19. news. setClicks( 0);
  20. news. setContent(content);
  21. news. setPbdate( new Date());
  22. news. setPbdeptid(deptid);
  23. news. setPublisher(usersId);
  24. news. setCategoryid( Integer. parseInt(categoryid));
  25. news. setTitle(title);
  26. newsDao. save(news);
  27. return "redirect:listforadmin";
  28. }
  29. @RequestMapping( "/listforadmin")
  30. public String listforadmin( ){
  31. List< News> newsList = newsDao. queryAll();
  32. request. setAttribute( "list", newsList);
  33. return "listnews";
  34. }
  35. @RequestMapping( "/toaddnews")
  36. public String toaddnews( ){
  37. List< News> newsList = newsDao. queryAll();
  38. request. setAttribute( "list", newsList);
  39. List< Category> categoryList = categoryDao. queryAll();
  40. request. setAttribute( "categoryList", categoryList);
  41. return "addnews";
  42. }
  43. @RequestMapping( "/edit")
  44. public String edit( ){
  45. String title = request. getParameter( "title");
  46. String content = request. getParameter( "content");
  47. String categoryid = request. getParameter( "categoryid");
  48. News news = new News();
  49. news. setCategoryid( Integer. parseInt(categoryid));
  50. news. setContent(content);
  51. news. setPbdate( new Date());
  52. news. setId( Integer. parseInt(request. getParameter( "id")));
  53. news. setTitle(title);
  54. newsDao. edit(news);
  55. return "redirect:listforadmin";
  56. }
  57. @RequestMapping( "/del")
  58. public String del( ){
  59. newsDao. deleteById( Integer. parseInt(request. getParameter( "id")));
  60. List< News> newsList = newsDao. queryAll();
  61. request. setAttribute( "list", newsList);
  62. return "listnews";
  63. }
  64. @RequestMapping( "/toeditnews")
  65. public String toeditnews( ){
  66. News news = newsDao. queryByid( Integer. parseInt(request. getParameter( "id")));
  67. request. setAttribute( "v", news);
  68. List< Category> categoryList = categoryDao. queryAll();
  69. request. setAttribute( "categoryList", categoryList);
  70. return "editnews";
  71. }
  72. @RequestMapping( "/queryByType")
  73. public String queryByType( ){
  74. String categoryid = request. getParameter( "categoryid");
  75. List< News> newsList = newsDao. queryAll();
  76. request. setAttribute( "list", newsList. stream()
  77. . filter(x -> x. getCategoryid(). equals( Integer. parseInt(categoryid))). collect( Collectors. toList()));
  78. List< Category> categoryList = categoryDao. queryAll();
  79. request. setAttribute( "categoryList",
  80. categoryList. stream(). filter(x -> x. getState(). equals( "1")). collect( Collectors. toList()));
  81. return "typenews";
  82. }
  83. @RequestMapping( "/detail")
  84. public String detail( ){
  85. //测试
  86. String id = request. getParameter( "id");
  87. if(id. contains( ".jsp"))
  88. {
  89. id= request. getParameter( "id"). replaceAll( ".jsp", "");
  90. }
  91. News news = newsDao. queryByid( Integer. parseInt(id));
  92. news. setClicks(news. getClicks() + 1);
  93. newsDao. setClicksIncrement(news);
  94. NewsDetail detail = new NewsDetail();
  95. detail. setNews(news);
  96. Category category = categoryDao. queryById(news. getCategoryid());
  97. detail. setCategory(category);
  98. String content = news. getContent();
  99. int length = content. length();
  100. length = length / 60;
  101. length = length * 27 + 10;
  102. request. setAttribute( "length", length);
  103. request. setAttribute( "v", detail);
  104. List< Category> categoryList = categoryDao. queryAll();
  105. List< Comments> commentsList = commentsDao. getByNewsId(news. getId());
  106. request. setAttribute( "commentsList", commentsList);
  107. request. setAttribute( "contentstr", content);
  108. request. setAttribute( "categoryList",
  109. categoryList. stream(). filter(x -> x. getState(). equals( "1")). collect( Collectors. toList()));
  110. return "detailnews";
  111. }
  112. @RequestMapping( "/search")
  113. public String search( ){
  114. String search = request. getParameter( "search");
  115. request. setAttribute( "searchStr", search);
  116. List< News> newsList = newsDao. queryAll();
  117. newsList=newsList. stream(). filter(x -> x. getTitle(). contains(search)). collect( Collectors. toList());
  118. newsList= newsList. stream(). map(x->{
  119. String title = x. getTitle();
  120. /* String[] split = title.split(search);
  121. */
  122. /*if(split.length>=2){
  123. for(int i=0;i<split.length-1;i++){
  124. title=split[i]+"<font color='red'>"+search+"</font>"+split[i+1];
  125. }
  126. }else{
  127. title=split[0]+"<font color='red'>"+search+"</font>";
  128. }*/
  129. title=title. replace(search, "<font color='red'>"+search+ "</font>");
  130. System. out. println(title);
  131. x. setTitle(title);
  132. return x;
  133. }). collect( Collectors. toList());
  134. request. setAttribute( "list",newsList
  135. );
  136. List< Category> categoryList = categoryDao. queryAll();
  137. request. setAttribute( "categoryList",
  138. categoryList. stream(). filter(x -> x. getState(). equals( "1")). collect( Collectors. toList()));
  139. return "searchnews";
  140. }
  141. }

类别管理控制层: 


  
  1. @Controller
  2. @RequestMapping( "CategoryServlet")
  3. public class CategoryController {
  4. @Autowired
  5. private HttpServletRequest request;
  6. private CategoryDao categoryDao= new CategoryDao();
  7. @RequestMapping( "/listforadmin")
  8. public String listforadmin( ){
  9. List< Category> categoryList = categoryDao. queryAll();
  10. request. setAttribute( "list", categoryList);
  11. return "listcategory";
  12. }
  13. @RequestMapping( "/show")
  14. public String show( ){
  15. List< Category> categoryList = categoryDao. queryAll();
  16. categoryList=categoryList. stream(). filter(x->x. getState(). equals( "1")). collect( Collectors. toList());
  17. if(categoryList. size()>= 5)
  18. {
  19. request. setAttribute( "list", categoryDao. queryAll());
  20. request. setAttribute( "msg", "设置栏目显示失败,前台栏目最多显示5个");
  21. return "listcategory";
  22. } else {
  23. categoryDao. show( Integer. parseInt(request. getParameter( "id")));
  24. return "redirect:listforadmin";
  25. }
  26. }
  27. @RequestMapping( "/hidden")
  28. public String hidden( ){
  29. categoryDao. hidden( Integer. parseInt(request. getParameter( "id")));
  30. return "redirect:listforadmin";
  31. }
  32. @RequestMapping( "/add")
  33. public String add( ){
  34. String name=request. getParameter( "name");
  35. String state=request. getParameter( "state");
  36. Category category= new Category();
  37. category. setName(name);
  38. category. setState(state);
  39. categoryDao. save(category);
  40. return "redirect:listforadmin";
  41. }
  42. }

 源码获取:俺的博客首页 "资源" 里下载!


转载:https://blog.csdn.net/m0_66863468/article/details/128363995
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场