作者主页:夜未央5788
简介:Java领域优质创作者、Java项目、学习资料、技术互助
文末获取源码
项目介绍
本项目分为前后台,分为管理员与普通用户角色,前台为普通用户登录,后台为管理员登录;
用户角色包含以下功能:
新闻搜索,查看新闻,用户首页,评论新闻等功能。
管理员角色包含以下功能:
分类管理,新闻管理,管理员登录,评论管理等功能。
由于本程序规模不大,可供课程设计,毕业设计学习演示之用
环境需要
1.运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。
2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA;
3.tomcat环境:Tomcat 7.x,8.x,9.x版本均可
4.硬件环境:windows 7/8/10 1G内存以上;或者 Mac OS;
5.数据库:MySql 5.7版本;
6.是否Maven项目:否;
技术栈
1. 后端:Spring+SpringMVC+Mybatis
2. 前端:JSP+CSS+JavaScript+jquery+bootstrap
使用说明
1. 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件;
2. 使用IDEA/Eclipse/MyEclipse导入项目,Eclipse/MyEclipse导入时,若为maven项目请选择maven;
若为maven项目,导入成功后请执行maven clean;maven install命令,然后运行;
3. 将项目中config/db.properties配置文件中的数据库配置改为自己的配置;
4. 运行项目,输入http://localhost:8080/News 登录
管理员账号/密码:admin/admin
用户账号/密码:小青年/123456
核心代码
/**
* 前台新闻控制器
* @author llq
*
*/
@RequestMapping("/news")
@Controller
public class HomeNewsController {
@Autowired
private NewsCategoryService newsCategoryService;
@Autowired
private NewsService newsService;
@Autowired
private CommentService commentService;
/**
* 查看新闻详情
* @param model
* @param id
* @return
*/
@RequestMapping(value="/detail",method=RequestMethod.GET)
public ModelAndView detail(ModelAndView model,Long id){
model.addObject("newsCategoryList", newsCategoryService.findAll());
News news = newsService.find(id);
model.addObject("news", news);
model.addObject("title", news.getTitle());
model.addObject("tags", news.getTags().split(","));
model.setViewName("home/news/detail");
//查看数加1
newsService.updateViewNumber(id);
return model;
}
/**
* 按照分类显示新闻列表
* @param model
* @param cid
* @return
*/
@RequestMapping(value="/category_list",method=RequestMethod.GET)
public ModelAndView categoryList(ModelAndView model,
@RequestParam(name="cid",required=true) Long cid,
Page page
){
Map<String, Object> queryMap = new HashMap<String, Object>();
queryMap.put("offset", 0);
queryMap.put("pageSize", 10);
queryMap.put("categoryId", cid);
model.addObject("newsCategoryList", newsCategoryService.findAll());
model.addObject("newsList", newsService.findList(queryMap));
NewsCategory newsCategory = newsCategoryService.find(cid);
model.addObject("newsCategory", newsCategory);
model.addObject("title", newsCategory.getName() + "分类下的新闻信息");
model.setViewName("home/news/category_list");
return model;
}
/**
* 获取按评论数排序的最新n条信息
* @return
*/
@RequestMapping(value="/last_comment_list",method=RequestMethod.POST)
@ResponseBody
public Map<String, Object> lastCommentList(){
Map<String, Object> ret = new HashMap<String, Object>();
ret.put("type", "success");
ret.put("newsList", newsService.findLastCommentList(10));
return ret;
}
/**
* 分页获取某个分类下的文章
* @param page
* @param cid
* @return
*/
@RequestMapping(value="/get_category_list",method=RequestMethod.POST)
@ResponseBody
public Map<String, Object> getCategoryList(Page page,
@RequestParam(name="cid",required=true) Long cid
){
Map<String, Object> ret = new HashMap<String, Object>();
Map<String, Object> queryMap = new HashMap<String, Object>();
queryMap.put("offset", page.getOffset());
queryMap.put("pageSize", page.getRows());
queryMap.put("categoryId", cid);
ret.put("type", "success");
ret.put("newsList", newsService.findList(queryMap));
return ret;
}
/**
* 获取搜索列表
* @param model
* @param keyword
* @param page
* @return
*/
@RequestMapping(value="/search_list",method=RequestMethod.GET)
public ModelAndView searchList(ModelAndView model,
@RequestParam(name="keyword",required=false,defaultValue="") String keyword,
Page page
){
Map<String, Object> queryMap = new HashMap<String, Object>();
try {
keyword= new String(keyword.getBytes("ISO-8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
queryMap.put("offset", 0);
queryMap.put("pageSize", 10);
queryMap.put("title", keyword);
model.addObject("newsCategoryList", newsCategoryService.findAll());
model.addObject("newsList", newsService.findList(queryMap));
model.addObject("title", keyword + "关键字下的新闻信息");
model.addObject("keyword", keyword);
model.setViewName("home/news/search_list");
return model;
}
/**
* 分页加载搜索列表
* @param page
* @param keyword
* @return
*/
@RequestMapping(value="/get_search_list",method=RequestMethod.POST)
@ResponseBody
public Map<String, Object> getSearchList(Page page,
@RequestParam(name="keyword",required=false,defaultValue="") String keyword
){
Map<String, Object> ret = new HashMap<String, Object>();
Map<String, Object> queryMap = new HashMap<String, Object>();
queryMap.put("offset", page.getOffset());
queryMap.put("pageSize", page.getRows());
queryMap.put("title", keyword);
ret.put("type", "success");
ret.put("newsList", newsService.findList(queryMap));
return ret;
}
/**
* 添加评论
* @param comment
* @return
*/
@RequestMapping(value="/comment_news",method=RequestMethod.POST)
@ResponseBody
public Map<String, Object> commentNews(Comment comment){
Map<String, Object> ret = new HashMap<String, Object>();
if(comment == null){
ret.put("type", "error");
ret.put("msg", "请填写完整的评论信息!");
return ret;
}
if(comment.getNewsId() == null){
ret.put("type", "error");
ret.put("msg", "请选择一个文章进行评论!");
return ret;
}
if(StringUtils.isEmpty(comment.getNickname())){
ret.put("type", "error");
ret.put("msg", "请填写昵称!");
return ret;
}
if(StringUtils.isEmpty(comment.getContent())){
ret.put("type", "error");
ret.put("msg", "请填写评论内容!");
return ret;
}
comment.setCreateTime(new Date());
if(commentService.add(comment) <= 0){
ret.put("type", "error");
ret.put("msg", "评论失败,请联系管理员!");
return ret;
}
//文章评论数加1
newsService.updateCommentNumber(comment.getNewsId());
ret.put("type", "success");
ret.put("createTime", comment.getCreateTime());
return ret;
}
运行截图
前台界面
后台界面
源码获取
如果也想学习本系统,下面领取。回复:154SSM
👇🏻 源码可通过搜索下方 公众号 获取👇🏻