基于javaweb+mysql的ssm新闻发布管理系统(java+ssm+jsp+bootstrap+jquery+mysql)
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb+mysql的SSM新闻发布管理系统(java+ssm+jsp+bootstrap+jquery+mysql)
项目介绍
本项目分为前后台,分为管理员与普通用户角色,前台为普通用户登录,后台为管理员登录;
用户角色包含以下功能: 新闻搜索,查看新闻,用户首页,评论新闻等功能。
管理员角色包含以下功能: 分类管理,新闻管理,管理员登录,评论管理等功能。
环境需要
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项目:否;
技术栈
- 后端:Spring+SpringMVC+Mybatis 2. 前端:JSP+CSS+JavaScript+jquery+bootstrap
使用说明
- 使用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 登录
}
/**
* 菜单修改
* @param menu
* @return
*/
@RequestMapping(value="/edit",method=RequestMethod.POST)
@ResponseBody
public Map<String, String> edit(Menu menu){
Map<String, String> ret = new HashMap<String, String>();
if(menu == null){
ret.put("type", "error");
ret.put("msg", "请选择正确的菜单信息!");
return ret;
}
if(StringUtils.isEmpty(menu.getName())){
ret.put("type", "error");
ret.put("msg", "请填写菜单名称!");
return ret;
}
if(StringUtils.isEmpty(menu.getIcon())){
ret.put("type", "error");
ret.put("msg", "请填写菜单图标类!");
return ret;
}
if(menu.getParentId() == null){
menu.setParentId(0l);
}
if(menuService.edit(menu) <= 0){
ret.put("type", "error");
ret.put("msg", "修改失败,请联系管理员!");
return ret;
}
ret.put("type", "success");
ret.put("msg", "修改成功!");
return ret;
}
/**
* 删除菜单信息
* @param id
* @return
*/
@RequestMapping(value="/delete",method=RequestMethod.POST)
@ResponseBody
public Map<String, String> delete(
@RequestParam(name="id",required=true) Long id
){
Map<String, String> ret = new HashMap<String, String>();
if(id == null){
ret.put("type", "error");
Map<String, Object> queryMap = new HashMap<String, Object>();
queryMap.put("offset", 0);
queryMap.put("pageSize", 10);
model.addObject("newsCategoryList", newsCategoryService.findAll());
model.addObject("newsList", newsService.findList(queryMap));
model.setViewName("home/index/index");
return model;
}
/**
* 获取网站基本信息
* @return
*/
@RequestMapping(value="/get_site_info",method=RequestMethod.POST)
@ResponseBody
public Map<String, Object> getSiteInfo(){
Map<String, Object> retMap = new HashMap<String, Object>();
retMap.put("type", "success");
Map<String, Object> queryMap = new HashMap<String, Object>();
queryMap.put("offset", 0);
queryMap.put("pageSize", 99999);
retMap.put("totalArticle", newsService.getTotal(queryMap));
return retMap;
}
private long getDays(String data){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = null;
try {
startDate = sdf.parse(data);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Date endDate = new Date();
long time = (endDate.getTime() - startDate.getTime())/1000/3600/24;
return time;
}
@RequestMapping(value="/user_login",method=RequestMethod.GET)
@ResponseBody
public ModelAndView toUserLogin(ModelAndView model){
model.setViewName("home/user/login");
// 去除敏感正文
if (SensitiveWord.checkSenstiveWord(comment.getContent())) {
comment.setContent(SensitiveWord.filterInfoAfter(comment
.getContent()));
}
return comment;
}
}
/**
* 菜单管理控制器
*
*/
@RequestMapping("/admin/menu")
@Controller
public class MenuController {
@Autowired
private MenuService menuService;
/**
* 菜单管理列表页
ret.put("type", "error");
ret.put("msg", "选择要删除的数据!");
return ret;
}
if(ids.contains(",")){
ids = ids.substring(0,ids.length()-1);
}
if(userService.delete(ids) <= 0){
ret.put("type", "error");
ret.put("msg", "用户删除失败,请联系管理员!");
return ret;
}
ret.put("type", "success");
ret.put("msg", "用户删除成功!");
return ret;
}
/**
* 上传图片
* @param photo
* @param request
* @return
*/
@RequestMapping(value="/upload_photo",method=RequestMethod.POST)
@ResponseBody
public Map<String, String> uploadPhoto(MultipartFile photo,HttpServletRequest request){
Map<String, String> ret = new HashMap<String, String>();
if(photo == null){
ret.put("type", "error");
ret.put("msg", "选择要上传的文件!");
return ret;
}
if(photo.getSize() > 1024*1024*1024){
ret.put("type", "error");
ret.put("msg", "文件大小不能超过10M!");
return ret;
}
//获取文件后缀
String suffix = photo.getOriginalFilename().substring(photo.getOriginalFilename().lastIndexOf(".")+1,photo.getOriginalFilename().length());
if(!"jpg,jpeg,gif,png".toUpperCase().contains(suffix.toUpperCase())){
ret.put("type", "error");
ret.put("msg", "请选择jpg,jpeg,gif,png格式的图片!");
return ret;
}
String savePath = request.getServletContext().getRealPath("/") + "/resources/upload/";
File savePathFile = new File(savePath);
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;
}
/**
* 分页获取某一文章的评论
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 page
* @param cid
* @return
*/
@RequestMapping(value="/get_recommed_list",method=RequestMethod.GET)
@ResponseBody
public Map<String, Object> getRecommedList(Page page,
@RequestParam(name="id") Long id
){
Map<String, Object> ret = new HashMap<String, Object>();
List<News> returNews = new ArrayList<News>();
if(id != -1) {
News news = newsService.find(id);
String[] tags = news.getTags().split(",");
Map<String, Object> queryMap = new HashMap<String, Object>();
queryMap.put("offset", 0);
queryMap.put("pageSize", 999);
List<News> newsList = newsService.findList(queryMap);
for(String tag:tags) {
for(News temp:newsList) {
if(temp.getTags().contains(tag)&&temp.getId()!=id) {
returNews.add(temp);
if(returNews.size() == 5) {
break;
}
}
}
}
}else {
returNews = newsService.findMostViewList(5);
}
if(returNews.size()<5) {
List<News> subList = newsService.findLastCommentList(5-returNews.size());
returNews.addAll(subList);
}
/**
* 日志管理控制器
*
*/
@RequestMapping("/admin/log")
@Controller
public class LogController {
@Autowired
private LogService logService;
/**
* 日志列表页面
* @param model
* @return
*/
@RequestMapping(value="/list",method=RequestMethod.GET)
public ModelAndView list(ModelAndView model){
model.setViewName("log/list");
return model;
}
/**
* 获取日志列表
* @param page
* @param content
* @param roleId
* @param sex
* @return
*/
@RequestMapping(value="/list",method=RequestMethod.POST)
@ResponseBody
public Map<String, Object> getList(Page page,
@RequestParam(name="content",required=false,defaultValue="") String content
/**
* 后台登录拦截器
*
*/
public class LoginInterceptor implements HandlerInterceptor {
@Override
public void afterCompletion(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
// TODO Auto-generated method stub
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2, ModelAndView arg3) throws Exception {
// TODO Auto-generated method stub
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object arg2) throws Exception {
// TODO Auto-generated method stub
String requestURI = request.getRequestURI();
Object admin = request.getSession().getAttribute("admin");
if(admin == null){
//表示未登录或者登录失效
System.out.println("链接"+requestURI+"进入拦截器!");
for(File f:listFiles){
if(f!= null && f.getName().contains("png")){
icons.add("icon-" + f.getName().substring(0, f.getName().indexOf(".")).replace("_", "-"));
}
}
ret.put("type", "success");
ret.put("content", icons);
return ret;
}
/**
* 菜单添加
* @param menu
* @return
*/
@RequestMapping(value="/add",method=RequestMethod.POST)
@ResponseBody
public Map<String, String> add(Menu menu){
Map<String, String> ret = new HashMap<String, String>();
if(menu == null){
ret.put("type", "error");
ret.put("msg", "请填写正确的菜单信息!");
return ret;
}
if(StringUtils.isEmpty(menu.getName())){
ret.put("type", "error");
ret.put("msg", "请填写菜单名称!");
return ret;
}
if(StringUtils.isEmpty(menu.getIcon())){
ret.put("type", "error");
ret.put("msg", "请填写菜单图标类!");
return ret;
}
if(menu.getParentId() == null){
menu.setParentId(0l);
}
if(menuService.add(menu) <= 0){
ret.put("type", "error");
ret.put("msg", "添加失败,请联系管理员!");
return ret;
}
ret.put("type", "success");
ret.put("msg", "添加成功!");
return ret;
}
/**
* 菜单修改
* @param menu
* @return
*/
return true;
}
}
/**
* 角色role控制器
*
*/
@RequestMapping("/admin/role")
@Controller
public class RoleController {
@Autowired
private RoleService roleService;
ret.put("msg", "新闻内容不能为空!");
return ret;
}
if (newsService.edit(news) <= 0) {
ret.put("type", "error");
ret.put("msg", "修改失败,请联系管理员!");
return ret;
}
// 去除敏感信息
NewsController.minganInfo(news);
ret.put("type", "success");
ret.put("msg", "修改成功!");
return ret;
}
/**
* 删除新闻
*
* @param id
* @return
*/
@RequestMapping(value = "/delete", method = RequestMethod.POST)
@ResponseBody
public Map<String, String> delete(Long id) {
Map<String, String> ret = new HashMap<String, String>();
if (id == null) {
ret.put("type", "error");
ret.put("msg", "请选择要删除的信息!");
return ret;
}
try {
if (newsService.delete(id) <= 0) {
ret.put("type", "error");
ret.put("msg", "删除失败,请联系管理员!");
return ret;
}
} catch (Exception e) {
ret.put("type", "error");
ret.put("msg", "该新闻下有评论信息,不可删除!");
return ret;
}
ret.put("type", "error");
ret.put("msg", "请填写正确的菜单信息!");
return ret;
}
if(StringUtils.isEmpty(menu.getName())){
ret.put("type", "error");
ret.put("msg", "请填写菜单名称!");
return ret;
}
if(StringUtils.isEmpty(menu.getIcon())){
ret.put("type", "error");
ret.put("msg", "请填写菜单图标类!");
return ret;
}
if(menu.getParentId() == null){
menu.setParentId(0l);
}
if(menuService.add(menu) <= 0){
ret.put("type", "error");
ret.put("msg", "添加失败,请联系管理员!");
return ret;
}
ret.put("type", "success");
ret.put("msg", "添加成功!");
return ret;
}
/**
* 菜单修改
* @param menu
* @return
*/
@RequestMapping(value="/edit",method=RequestMethod.POST)
@ResponseBody
public Map<String, String> edit(Menu menu){
Map<String, String> ret = new HashMap<String, String>();
if(menu == null){
ret.put("type", "error");
ret.put("msg", "请选择正确的菜单信息!");
return ret;
ret.put("type", "error");
ret.put("msg", "请填写用户名!");
return ret;
}
if(StringUtils.isEmpty(user.getPassword())){
ret.put("type", "error");
ret.put("msg", "请填写密码!");
return ret;
}
if(user.getRoleId() == null){
user.setRoleId(2L);
}
if(isExist(user.getUsername(), 0l)){
ret.put("type", "error");
ret.put("msg", "该用户名已经存在,请重新输入!");
return ret;
}
if(userService.add(user) <= 0){
ret.put("type", "error");
ret.put("msg", "用户添加失败,请联系管理员!");
return ret;
}
ret.put("type", "success");
ret.put("msg", "角色添加成功!");
return ret;
}
/**
* 添加用户
* @param user
* @return
*/
@RequestMapping(value="/user/login",method=RequestMethod.POST)
@ResponseBody
public Map<String, String> login(User user,String cpacha,HttpServletRequest request){
Map<String, String> ret = new HashMap<String, String>();
Object loginCpacha = request.getSession().getAttribute("loginCpacha");
if(!cpacha.toUpperCase().equals(loginCpacha.toString().toUpperCase())){
ret.put("type", "error");
ret.put("msg", "验证码错误!");
logService.add("用户名为"+user.getUsername()+"的用户登录时输入验证码错误!");
return ret;
}
@RequestParam(name="vl",required=false,defaultValue="4") Integer vcodeLen,
@RequestParam(name="w",required=false,defaultValue="100") Integer width,
@RequestParam(name="h",required=false,defaultValue="30") Integer height,
@RequestParam(name="type",required=true,defaultValue="loginCpacha") String cpachaType,
HttpServletRequest request,
HttpServletResponse response){
CpachaUtil cpachaUtil = new CpachaUtil(vcodeLen, width, height);
String generatorVCode = cpachaUtil.generatorVCode();
request.getSession().setAttribute(cpachaType, generatorVCode);
BufferedImage generatorRotateVCodeImage = cpachaUtil.generatorRotateVCodeImage(generatorVCode, true);
try {
ImageIO.write(generatorRotateVCodeImage, "gif", response.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
try {
ImageIO.write(generatorRotateVCodeImage, "gif", response.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 用户管理控制器
*
*/
@RequestMapping("/admin/user")
@Controller
public class UserController {
@Autowired
private UserService userService;
@Autowired
private RoleService roleService;
/**
* 用户列表页面
* @param model
return ret;
}
/**
* 新闻评论信息编辑
*
* @param newsCategory
* @return
*/
@RequestMapping(value = "/edit", method = RequestMethod.POST)
@ResponseBody
public Map<String, String> edit(Comment comment) {
// 去除敏感信息
CommentController.minganInfo(comment);
Map<String, String> ret = new HashMap<String, String>();
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;
}
if (commentService.edit(comment) <= 0) {
ret.put("type", "error");
ret.put("msg", "评论修改失败,请联系管理员!");
return ret;
}
ret.put("type", "success");
ret.put("msg", "修改成功!");
return ret;
}
/**
* 删除新闻评论
*
* @param id
* @return
Map<String, String> ret = new HashMap<String, String>();
if(user == null){
ret.put("type", "error");
ret.put("msg", "请填写正确的用户信息!");
return ret;
}
if(StringUtils.isEmpty(user.getUsername())){
ret.put("type", "error");
ret.put("msg", "请填写用户名!");
return ret;
}
if(StringUtils.isEmpty(user.getPassword())){
ret.put("type", "error");
ret.put("msg", "请填写密码!");
return ret;
}
if(user.getRoleId() == null){
ret.put("type", "error");
ret.put("msg", "请选择所属角色!");
return ret;
}
if(isExist(user.getUsername(), 0l)){
ret.put("type", "error");
ret.put("msg", "该用户名已经存在,请重新输入!");
return ret;
}
if(userService.add(user) <= 0){
ret.put("type", "error");
ret.put("msg", "用户添加失败,请联系管理员!");
return ret;
}
ret.put("type", "success");
ret.put("msg", "角色添加成功!");
return ret;
}
/**
* 编辑用户
* @param user
* @return
*/
@RequestMapping(value="/edit",method=RequestMethod.POST)
@ResponseBody
public Map<String, String> edit(User user){
Map<String, String> ret = new HashMap<String, String>();
if(user == null){
ret.put("type", "error");
ret.put("msg", "请填写正确的用户信息!");
return ret;
}
if(StringUtils.isEmpty(user.getUsername())){
ret.put("type", "error");
ret.put("msg", "请填写用户名!");
@RequestParam(name="name",required=false,defaultValue="") String name,
Page page
){
Map<String,Object> ret = new HashMap<String, Object>();
Map<String,Object> queryMap = new HashMap<String, Object>();
queryMap.put("name", name);
queryMap.put("offset", page.getOffset());
queryMap.put("pageSize", page.getRows());
ret.put("rows", newsCategoryService.findList(queryMap));
ret.put("total", newsCategoryService.getTotal(queryMap));
return ret;
}
}
/**
* 日志管理控制器
*
*/
@RequestMapping("/admin/log")
@Controller
public class LogController {
@Autowired
private LogService logService;
/**
* 日志列表页面
* @param model
* @return
*/
@RequestMapping(value="/list",method=RequestMethod.GET)
public ModelAndView list(ModelAndView model){
model.setViewName("log/list");
return model;
}