基于javaweb+mysql的ssm新闻发布管理系统(java+ssm+jsp+bootstrap+jquery+mysql)
私信源码获取及调试交流
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb的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 登录
@RequestMapping(value="/get_all_menu",method=RequestMethod.POST)
@ResponseBody
public List<Menu> getAllMenu(){
Map<String, Object> queryMap = new HashMap<String, Object>();
queryMap.put("offset", 0);
queryMap.put("pageSize", 99999);
return menuService.findList(queryMap);
}
/**
* 添加权限
* @param ids
* @return
*/
@RequestMapping(value="/add_authority",method=RequestMethod.POST)
@ResponseBody
public Map<String, String> addAuthority(
@RequestParam(name="ids",required=true) String ids,
@RequestParam(name="roleId",required=true) Long roleId
){
Map<String,String> ret = new HashMap<String, String>();
if(StringUtils.isEmpty(ids)){
ret.put("type", "error");
ret.put("msg", "请选择相应的权限!");
return ret;
}
if(roleId == null){
ret.put("type", "error");
ret.put("msg", "请选择相应的角色!");
return ret;
}
if(ids.contains(",")){
ids = ids.substring(0,ids.length()-1);
}
String[] idArr = ids.split(",");
if(idArr.length > 0){
authorityService.deleteByRoleId(roleId);
}
for(String id:idArr){
Authority authority = new Authority();
authority.setMenuId(Long.valueOf(id));
authority.setRoleId(roleId);
authorityService.add(authority);
}
ret.put("type", "success");
ret.put("msg", "权限编辑成功!");
return ret;
}
){
return authorityService.findListByRoleId(roleId);
}
}
/**
* 新闻评论控制器
*
*
*/
@RequestMapping("/admin/comment")
@Controller
public class CommentController {
@Autowired
private NewsService newsService;
@Autowired
private CommentService commentService;
/**
* 新闻评论列表页面
*
* @param model
* @return
*/
}
}
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", "添加成功!");
if (news == null) {
ret.put("type", "error");
ret.put("msg", "请填写正确的信息!");
return ret;
}
if (StringUtils.isEmpty(news.getTitle())) {
ret.put("type", "error");
ret.put("msg", "新闻标题不能为空!");
return ret;
}
if (news.getCategoryId() == null) {
ret.put("type", "error");
ret.put("msg", "请选择新闻分类!");
return ret;
}
if (StringUtils.isEmpty(news.getAbstrs())) {
ret.put("type", "error");
ret.put("msg", "新闻摘要不能为空!");
return ret;
}
if (StringUtils.isEmpty(news.getTags())) {
ret.put("type", "error");
ret.put("msg", "新闻标签不能为空!");
return ret;
}
if (StringUtils.isEmpty(news.getPhoto())) {
ret.put("type", "error");
ret.put("msg", "新闻封面图片必须上传!");
return ret;
}
if (StringUtils.isEmpty(news.getAuthor())) {
ret.put("type", "error");
ret.put("msg", "新闻作者不能为空!");
return ret;
}
if (StringUtils.isEmpty(news.getContent())) {
ret.put("type", "error");
ret.put("msg", "新闻内容不能为空!");
return ret;
}
// 去除敏感信息
NewsController.minganInfo(news);
ret.put("type", "error");
ret.put("msg", "验证码错误!");
logService.add("用户名为"+user.getUsername()+"的用户登录时输入验证码错误!");
return ret;
}
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(isExistUser(user.getUsername(), user.getPassword())){
request.getSession().setAttribute("user", user.getUsername());
ret.put("type", "success");
ret.put("msg", "登录成功!");
return ret;
}
ret.put("type", "error");
ret.put("msg", "登录失败!");
return ret;
}
/**
* 判断该用户名是否存在
* @param username
* @param id
* @return
*/
private boolean isExist(String username,Long id){
User user = userService.findByUsername(username);
if(user == null)return false;
if(user.getId().longValue() == id.longValue())return false;
return true;
}
/**
* 判断该用户账号密码是否正确
* @param username
* @param id
* @return
*/
private boolean isExistUser(String username,String password){
User user = userService.findByUsername(username);
if(user == null)return false;
/**
* 新闻评论控制器
*
*
*/
@RequestMapping("/admin/comment")
@Controller
public class CommentController {
@Autowired
private NewsService newsService;
@Autowired
private CommentService commentService;
/**
* 新闻评论列表页面
*
* @param model
* @return
*/
@RequestMapping(value = "/list", method = RequestMethod.GET)
public ModelAndView list(ModelAndView model) {
Map<String, Object> queryMap = new HashMap<String, Object>();
queryMap.put("offset", 0);
queryMap.put("pageSize", 999);
model.addObject("newsList", newsService.findList(queryMap));
model.setViewName("comment/list");
return model;
}
if(StringUtils.isEmpty(user.getPassword())){
ret.put("type", "error");
ret.put("msg", "请填写密码!");
return ret;
}
if(isExistUser(user.getUsername(), user.getPassword())){
request.getSession().setAttribute("user", user.getUsername());
ret.put("type", "success");
ret.put("msg", "登录成功!");
return ret;
}
ret.put("type", "error");
ret.put("msg", "登录失败!");
return ret;
}
/**
* 判断该用户名是否存在
* @param username
* @param id
* @return
*/
private boolean isExist(String username,Long id){
User user = userService.findByUsername(username);
if(user == null)return false;
if(user.getId().longValue() == id.longValue())return false;
return true;
}
/**
* 判断该用户账号密码是否正确
* @param username
* @param id
* @return
*/
private boolean isExistUser(String username,String password){
User user = userService.findByUsername(username);
if(user == null)return false;
if(!user.getPassword().equals(password))return false;
return true;
}
}
return model;
}
/**
* 获取菜单列表
* @param page
* @param name
* @return
*/
@RequestMapping(value="/list",method=RequestMethod.POST)
@ResponseBody
public Map<String, Object> getMenuList(Page page,
@RequestParam(name="name",required=false,defaultValue="") String name
){
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("name", name);
List<Menu> findList = menuService.findList(queryMap);
ret.put("rows", findList);
ret.put("total", menuService.getTotal(queryMap));
return ret;
}
/**
* 获取指定目录下的系统icon集合
* @param request
* @return
*/
@RequestMapping(value="/get_icons",method=RequestMethod.POST)
@ResponseBody
public Map<String, Object> getIconList(HttpServletRequest request){
Map<String, Object> ret = new HashMap<String, Object>();
String realPath = request.getServletContext().getRealPath("/");
File file = new File(realPath + "\\resources\\admin\\easyui\\css\\icons");
List<String> icons = new ArrayList<String>();
if(!file.exists()){
ret.put("type", "error");
ret.put("msg", "文件目录不存在!");
return ret;
}
File[] listFiles = file.listFiles();
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;
}
*/
public static Comment minganInfo(Comment comment) {
// 去除敏感昵称
if (SensitiveWord.checkSenstiveWord(comment.getNickname())) {
comment.setNickname(SensitiveWord.filterInfoAfter(comment
.getNickname()));
}
// 去除敏感正文
if (SensitiveWord.checkSenstiveWord(comment.getContent())) {
comment.setContent(SensitiveWord.filterInfoAfter(comment
.getContent()));
}
return comment;
}
}
/**
* 菜单管理控制器
*
*/
/**
* 分页获取某一文章的评论
* @param page
* @return
*/
@RequestMapping(value="/get_comment_list",method=RequestMethod.POST)
@ResponseBody
public Map<String, Object> getCommentList(Page page,Long newsId){
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("newsId", newsId);
ret.put("type", "success");
ret.put("commentList", commentService.findList(queryMap));
return ret;
}
}
}
/**
* 本系统所有的验证码均采用此方法
* @param vcodeLen
* @param width
* @param height
* @param cpachaType:用来区别验证码的类型,传入字符串
* @param request
* @param response
*/
@RequestMapping(value="/get_cpacha",method=RequestMethod.GET)
public void generateCpacha(
@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();
}
}
}
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;
}
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(isExistUser(user.getUsername(), user.getPassword())){
request.getSession().setAttribute("user", user.getUsername());
ret.put("type", "success");
ret.put("msg", "登录成功!");
return ret;
}
ret.put("type", "error");
ret.put("msg", "登录失败!");
return ret;
}
/**
* 判断该用户名是否存在
* @param username
* @param id
* @return
*/
private boolean isExist(String username,Long id){
@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;
}
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(isExistUser(user.getUsername(), user.getPassword())){
request.getSession().setAttribute("user", user.getUsername());
ret.put("type", "success");
ret.put("msg", "登录成功!");
return ret;
}
ret.put("type", "error");
ret.put("msg", "登录失败!");
return ret;
}
/**
* 判断该用户名是否存在
* @param username
* @param id
* @return
return ret;
}
if (news.getCategoryId() == null) {
ret.put("type", "error");
ret.put("msg", "请选择新闻分类!");
return ret;
}
if (StringUtils.isEmpty(news.getAbstrs())) {
ret.put("type", "error");
ret.put("msg", "新闻摘要不能为空!");
return ret;
}
if (StringUtils.isEmpty(news.getTags())) {
ret.put("type", "error");
ret.put("msg", "新闻标签不能为空!");
return ret;
}
if (StringUtils.isEmpty(news.getPhoto())) {
ret.put("type", "error");
ret.put("msg", "新闻封面图片必须上传!");
return ret;
}
if (StringUtils.isEmpty(news.getAuthor())) {
ret.put("type", "error");
ret.put("msg", "新闻作者不能为空!");
return ret;
}
if (StringUtils.isEmpty(news.getContent())) {
ret.put("type", "error");
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 newsCategory
* @return
*/
@RequestMapping(value="/add",method=RequestMethod.POST)
@ResponseBody
public Map<String,String> add(NewsCategory newsCategory){
Map<String,String> ret = new HashMap<String, String>();
if(newsCategory == null){
ret.put("type", "error");
ret.put("msg", "请填写正确的分类信息!");
return ret;
}
if(StringUtils.isEmpty(newsCategory.getName())){
ret.put("type", "error");
ret.put("msg", "分类名称不能为空!");
return ret;
}
if(newsCategoryService.add(newsCategory) <= 0){
ret.put("type", "error");
ret.put("msg", "分类添加失败,请联系管理员!");
return ret;
}
ret.put("type", "success");
ret.put("msg", "添加成功!");
return ret;
}
/**
* 新闻分类信息编辑
* @param newsCategory
* @return
*/
@RequestMapping(value="/edit",method=RequestMethod.POST)
@ResponseBody
public Map<String,String> edit(NewsCategory newsCategory){
Map<String,String> ret = new HashMap<String, String>();
if(newsCategory == null){
ret.put("type", "error");
ret.put("msg", "请填写正确的分类信息!");
return ret;
}
if(StringUtils.isEmpty(newsCategory.getName())){
ret.put("type", "error");
ret.put("msg", "分类名称不能为空!");
return ret;
}
if(newsCategoryService.edit(newsCategory) <= 0){
ret.put("type", "error");
ret.put("msg", "分类修改失败,请联系管理员!");
return ret;
}
/**
* 用户管理控制器
*
*/
@RequestMapping("/admin/user")
@Controller
public class UserController {
@Autowired
private UserService userService;
@Autowired
private RoleService roleService;
/**
* 用户列表页面
* @param model
* @return
*/
@RequestMapping(value="/list",method=RequestMethod.GET)
public ModelAndView list(ModelAndView model){
Map<String, Object> queryMap = new HashMap<String, Object>();
model.addObject("roleList", roleService.findList(queryMap));
model.setViewName("user/list");
return model;
}
/**
* 获取用户列表
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;
}
@RequestMapping(value="/user_register",method=RequestMethod.GET)
@ResponseBody
public ModelAndView toUserRegister(ModelAndView model){
model.setViewName("home/user/register");
return model;
}
/**
* 添加用户
* @param user
* @return
*/
@RequestMapping(value="/user/add",method=RequestMethod.POST)
@ResponseBody
public Map<String, String> add(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;
}
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;
*
* @param id
* @return
*/
@RequestMapping(value = "/delete", method = RequestMethod.POST)
@ResponseBody
public Map<String, String> delete(String ids) {
Map<String, String> ret = new HashMap<String, String>();
if (ids == null) {
ret.put("type", "error");
ret.put("msg", "请选择要删除的评论信息!");
return ret;
}
if (ids.contains(",")) {
ids = ids.substring(0, ids.length() - 1);
}
if (commentService.delete(ids) <= 0) {
ret.put("type", "error");
ret.put("msg", "评论删除失败,请联系管理员!");
return ret;
}
ret.put("type", "success");
ret.put("msg", "删除成功!");
return ret;
}
/**
* 分页模糊搜索查询列表
*
* @param name
* @param page
* @return
*/
@RequestMapping(value = "/list", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> getList(
@RequestParam(name = "nickname", required = false, defaultValue = "") String nickname,
@RequestParam(name = "content", required = false, defaultValue = "") String content,
Page page) {
Map<String, Object> ret = new HashMap<String, Object>();
Map<String, Object> queryMap = new HashMap<String, Object>();
queryMap.put("nickname", nickname);
queryMap.put("content", content);
queryMap.put("offset", page.getOffset());
queryMap.put("pageSize", page.getRows());
ret.put("rows", commentService.findList(queryMap));
ret.put("total", commentService.getTotal(queryMap));
return ret;