目录

摘要

一、系统开发环境

二、数据库设计

三、实现的功能

四、详细设计

五、效果展示


摘要

  • 游客可以浏览新闻,也可以通过新闻类别或新闻标题对新闻进行查找;
  • 管理员可以增加新闻、修改新闻、根据新闻类别查找新闻、删除新闻,也可以增加类别、修改类别、删除类别;在删除类别时,该类别的新闻会被同时删除。

一、系统开发环境

Eclipse2021.03

jdk1.8.0

Tomcat9.0

Mysql8.0.26

二、数据库设计

新闻表t_news

类型表t_type

用户表t_user

三、实现的功能

普通用户(游客):

(1)显示所有新闻

(2)浏览新闻

(3)根据新闻类型对新闻进行查找

(4)根据新闻标题对新闻进行查找

管理员用户:

(1)显示所有新闻

(2)增加新闻

(3)修改新闻

(4)根据新闻类型对新闻进行查找

(5)删除新闻

(6)显示所有类别

(7)增加新闻类别

(8)修改新闻类别

(9)删除新闻类别

四、详细设计

  • 开发步骤和主要代码

撰写流程:从dao —> service —> controller —> 视图

NewsDao.java

packagezsls.news.dao;importorg.apache.ibatis.annotations.Param;importzsls.news.pojo.News;importjava.util.List;publicinterfaceNewsDao {List queryALL(@Param("tid") Integer tid,@Param("title") String title,@Param("num") intnum);News queryById(Integer id);intsave(News news);intupdate(News news);intdeleteById(Integer id);intdeleteByTid(Integer tid);}

TypeDao.java

package zsls.news.dao;import org.apache.ibatis.annotations.Param;import zsls.news.pojo.Type;import java.util.List;public interface TypeDao {public List queryAllTypes();public List queryType(@Param("name")String name);public int insertType(@Param("name")String name);public int deleteByRecid(String id);Type queryById(Integer id);int update(Type type);}

UserDao.java

package zsls.news.dao;import org.apache.ibatis.annotations.Param;import zsls.news.pojo.User;public interface UserDao {public User queryUserByUsernameAndPassword(@Param("name")String username, @Param("pass")String password);}

NewsService.java

package zsls.news.service;import zsls.news.pojo.News;import java.util.List;public interface NewsService {List queryALL(Integer tid,String title,Integer pageNo);News queryById(Integer id);int save(News news);int update(News news);int deleteById(Integer id);}

NewsServiceImpl.java

package zsls.news.service;import zsls.news.dao.NewsDao;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import zsls.news.pojo.News;import java.util.List;@Servicepublic class NewsServiceImpl implements NewsService {@Autowiredprivate NewsDao newsDao;@Overridepublic List queryALL(Integer tid,String title,Integer pageNo) {if(title != null && !"".equals(title.trim())){title = "%"+title+"%";} int num = (pageNo - 1) * 10;return newsDao.queryALL(tid,title,num);}@Overridepublic News queryById(Integer id) {return newsDao.queryById(id);}@Overridepublic int save(News news) {return newsDao.save(news);}@Overridepublic int update(News news) {return newsDao.update(news);}@Overridepublic int deleteById(Integer id) {return newsDao.deleteById(id);}}

TypeService.java

package zsls.news.service;import zsls.news.pojo.Type;import java.util.List;public interface TypeService {public List queryAllTypes();public int deleteByRecid(String recid);int addType(String name);Type queryById(Integer id);int update(Type type);}

TypeServiceImpl.java

package zsls.news.service;import zsls.news.dao.NewsDao;import zsls.news.dao.TypeDao;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import zsls.news.pojo.Type;import java.util.List;@Servicepublic class TypeServiceImpl implements TypeService {@Autowiredprivate TypeDao typeDao;@Autowiredprivate NewsDao newsDao;@Overridepublic List queryAllTypes() {// TODO Auto-generated method stubreturn typeDao.queryAllTypes();}@Overridepublic int deleteByRecid(String recid) {// TODO Auto-generated method stubint i=typeDao.deleteByRecid(recid);// 删除类型,把同类的新闻也全部删除 int deleteByTid = newsDao.deleteByTid(Integer.valueOf(recid));return i;}@Overridepublic int addType(String name) {return typeDao.insertType(name);}@Overridepublic Type queryById(Integer id) {return typeDao.queryById(id);}@Overridepublic int update(Type type) {return typeDao.update(type);}}

UserService.java

package zsls.news.service;import zsls.news.pojo.User;public interface UserService {public User queryUserByUsernameAndPassword(String username, String password);}

UserServiceImpl.java

package zsls.news.service;import zsls.news.dao.UserDao;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import zsls.news.pojo.User;@Servicepublic class UserServiceImpl implements UserService {@Autowiredprivate UserDao userDao;@Overridepublic User queryUserByUsernameAndPassword(String username, String password) {// TODO Auto-generated method stubreturn userDao.queryUserByUsernameAndPassword(username, password);}}

AdminController.java

package zsls.news.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import zsls.news.pojo.News;import zsls.news.pojo.Type;import zsls.news.pojo.User;import zsls.news.service.NewsService;import zsls.news.service.TypeService;import zsls.news.service.UserService;import javax.servlet.http.HttpSession;import java.util.Date;import java.util.List;@Controllerpublic class AdminController {@Autowiredprivate UserService userService;@Autowiredprivate TypeService typeService;@Autowiredprivate NewsService newsService;@RequestMapping("login.do")public String login(String username, String password, HttpSession session, Model model){User user=userService.queryUserByUsernameAndPassword(username, password);System.out.println("当前正在执行控制器方法login...");if(user!=null){session.setAttribute("loginUser",user);return "/manager/index";}else{model.addAttribute("msg","登录失败,请重新登录!");return "/login";}}@RequestMapping("logout.do")public String logout(HttpSession session){session.invalidate();return "/login";}@RequestMapping("toLogin.do")public String toLogin(){return "/login";}@RequestMapping("admin/toIndex.do")public String toIndex(){return "/manager/index";}@RequestMapping("admin/queryAllTypes.do") //管理员方法public String AdminQueryAllTypes(Model model){List typelist=typeService.queryAllTypes();model.addAttribute("typelist",typelist);return "/manager/categoryList";}@RequestMapping("admin/toAddType.do")public String toAddType(){return "/manager/categoryAdd";}@RequestMapping("admin/addType.do")public String addType(String name,Model model){int i=typeService.addType(name);if(i>0){return "redirect:queryAllTypes.do";}else{model.addAttribute("msg","添加类型不成功!");return "/error";}}@RequestMapping("admin/toModifyType.do")public String toModifyType(Integer recid,Model model){Type type = typeService.queryById(recid);model.addAttribute("type",type);return "/manager/categoryModify";}@RequestMapping("admin/modifyType.do")public String modifyType(Type type,Model model){int i=typeService.update(type);if(i>0){return "redirect:queryAllTypes.do";}else{model.addAttribute("msg","修改类型不成功!");return "/error";}}@RequestMapping("admin/delete.do") //管理员方法public String deleteByRecid(String recid,Model model){int i=typeService.deleteByRecid(recid);if(i>0){return "redirect:queryAllTypes.do";}else{model.addAttribute("msg","删除类型不成功!");return "/error";}}@RequestMapping("admin/queryAllNews.do") //管理员方法public String AdminQueryAllNews(Integer tid,Integer pageNo,Model model){if(pageNo == null || pageNo < 1){pageNo = 1;}List newsList = newsService.queryALL(tid,null,pageNo);model.addAttribute("newsList",newsList);List typelist=typeService.queryAllTypes();model.addAttribute("typelist",typelist);model.addAttribute("pageNo1",pageNo-1);model.addAttribute("pageNo",pageNo);model.addAttribute("pageNo2",pageNo+1);model.addAttribute("tid",tid);return "/manager/newsList";}@RequestMapping("admin/toAddNews.do")public String toAddNews(Model model){List typelist=typeService.queryAllTypes();model.addAttribute("typelist",typelist);return "/manager/newsAdd";}@RequestMapping("admin/addNews.do")public String addNews(News news,Model model){if(news.getTid() == null ){model.addAttribute("msg","添加新闻类型不能为空!");return "/error";}if(news.getTitle() == null || "".equals(news.getTitle())|| news.getContent() == null || "".equals(news.getContent())){model.addAttribute("msg","添加新闻标题或内容不能为空!");return "/error";}news.setDate(new Date());int i=newsService.save(news);if(i>0){return "redirect:queryAllNews.do";}else{model.addAttribute("msg","添加新闻不成功!");return "/error";}}@RequestMapping("admin/deleteNews.do")public String deleteNews(Integer id,Model model){int i=newsService.deleteById(id);if(i>0){return "redirect:queryAllNews.do";}else{model.addAttribute("msg","删除新闻不成功!");return "/error";}}@RequestMapping("admin/toModifyNews.do")public String toModifyNews(Integer id,Model model){News news = newsService.queryById(id);model.addAttribute("news",news);return "/manager/newsModify";}@RequestMapping("admin/modifyNews.do")public String modifyNews(News news,Model model){news.setDate(new Date());int i=newsService.update(news);if(i>0){return "redirect:queryAllNews.do";}else{model.addAttribute("msg","修改新闻不成功!");return "/error";}}@RequestMapping("queryAllTypes.do")public String queryAllBooks(Model model){List typelist=typeService.queryAllTypes();model.addAttribute("typelist",typelist);return "index";}}

VisitorController.java

package zsls.news.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import zsls.news.pojo.News;import zsls.news.pojo.Type;import zsls.news.service.NewsService;import zsls.news.service.TypeService;import java.util.List;@Controllerpublic class VisitorController {@Autowiredprivate TypeService typeService;@Autowiredprivate NewsService newsService;@RequestMapping("visitor/toIndex.do")public String toIndex(){return "/index";}@RequestMapping("visitor/queryAllNews.do")public String queryAllNews(Integer tid,String title,Integer pageNo, Model model){if(pageNo == null || pageNo < 1){pageNo = 1;}List newsList = newsService.queryALL(tid,title,pageNo);model.addAttribute("newsList",newsList);List typelist=typeService.queryAllTypes();model.addAttribute("typelist",typelist);model.addAttribute("pageNo1",pageNo-1);model.addAttribute("pageNo",pageNo);model.addAttribute("pageNo2",pageNo+1);model.addAttribute("tid",tid);model.addAttribute("title",title);return "/search";}@RequestMapping("visitor/queryNews.do")public String queryNews(Integer id, Model model){News news = newsService.queryById(id);model.addAttribute("news",news);Type type = typeService.queryById(news.getTid());model.addAttribute("type",type);return "/info";}}

index.jsp (管理员)

Insert title here欢迎您,${loginUser.name}主页新闻列表类型列表注销

欢迎来到

系统的后台管理

index.jsp (游客)

Insert title here欢迎您主页新闻列表登录

欢迎来浏览新闻

LoginInterceptor.java (登录拦截器)

package zsls.news.interceptor;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;public class LoginInterceptor implements HandlerInterceptor {@Overridepublic void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)throws Exception {// 该方法是在整个请求执行即渲染结束后执行,通过此方法实现一些资源清理,记录日志信息等工作System.out.println("执行afterCompletion....");}@Overridepublic void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView mv)throws Exception {该方法是在控制器方法调用之后,且视图解析还没进行之前System.out.println("执行postHandle....");}@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {// 该方法是在控制器方法执行前执行,如果为真,则继续下一步的操作,也就是接着执行控制器方法或调用下一个拦截器;为假,则中断System.out.println("执行preHandle....");//获取当前请求的地址String uri=request.getRequestURI();if(uri.indexOf("login")>0) { //当前你的请求是login,放行return true;}HttpSession session=request.getSession();if(session.getAttribute("loginUser")!=null) {return true;}session.setAttribute("errorMsg","您还没有登录,请先登录!");request.getRequestDispatcher("/login.jsp").forward(request, response);return false;}}

login.jsp

Insert title here${sessionScope.errorMsg}username:
password:

游客主页

categoryAdd.jsp

Insert title here欢迎您,${loginUser.name}主页新闻列表类型列表注销


categoryList.jsp

Insert title here欢迎您,${loginUser.name}主页新闻列表类型列表注销

添加类型
类别编号类别名称删除编辑
${type.id}${type.name}删除编辑

categoryModify.jsp

Insert title here欢迎您,${loginUser.name}主页新闻列表类型列表注销



newsAdd.jsp

Insert title here欢迎您,${loginUser.name}主页新闻列表类型列表注销

${type.name}


newsList.jsp

Insert title here欢迎您,${loginUser.name}主页新闻列表类型列表注销

全部${type.name}添加新闻
序号标题日期编辑删除
${news.id}${news.title}编辑删除
当前是第${pageNo}页上一页下一页

newsModify.jsp

Insert title here欢迎您,${loginUser.name}主页新闻列表类型列表注销





info.jsp

Insert title here欢迎您主页新闻列表登录

${news.title}

${type.name}

${news.content}

search.jsp

Insert title here欢迎您主页新闻列表登录

全部${type.name}
序号标题日期详情
${news.id}${news.title}详情
当前是第${pageNo}页上一页下一页

五、效果展示

普通用户(游客)

(1)显示所有新闻

(2)浏览新闻

(3)根据新闻类别对新闻进行查找

(4)根据新闻标题对新闻进行查找

管理员用户

(1)显示所有新闻

(2)增加新闻

(3)修改新闻

(4)根据新闻类型对新闻进行查找

(5)删除新闻

(6)显示所有类别

(7)增加新闻类别

(8)修改新闻类别

(9)删除新闻类别