作者主页:源码空间站2022
简介: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版本;
技术栈
1. 后端:Spring+SpringMVC+Mybatis
2. 前端:HTML+CSS+JavaScript+jsp
使用说明
1. 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件;
2. 使用IDEA/Eclipse/MyEclipse导入项目,Eclipse/MyEclipse导入时,若为maven项目请选择maven;若为maven项目,导入成功后请执行maven clean;maven install命令,然后运行;
3. 将项目中application.yml配置文件中的数据库配置改为自己的配置;
4. 运行项目,输入localhost:8080/ 登录
运行截图
相关代码
博客Controller
package com.june.web.controller.admin;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import com.june.lucene.BlogIndex;
import com.june.model.Blog;
import com.june.model.BlogType;
import com.june.model.PageBean;
import com.june.service.BlogService;
import com.june.service.BlogTypeService;
import com.june.service.CommentService;
import com.june.util.Constants;
import com.june.util.DateUtils;
import com.june.util.PageUtils;
import com.june.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
/**
* 博客Controller
*/
@Controller
@RequestMapping("/blog")
public @Slf4j class BlogAdminController {
@Resource
private BlogService blogService;
@Resource
private BlogTypeService blogTypeService;
@Resource
private CommentService commentService;
@Resource
private BlogIndex blogIndex;
@RequestMapping("/list")
public String list(@RequestParam(defaultValue = "1") Integer page,
@RequestParam(defaultValue = Constants.DEFAULT_PAGE_SIZE - 1 + "") Integer pageSize,
String firstDate, String secondDate, Integer typeId, String title, Model model,
HttpServletRequest request) {
Map<String, Object> map = new HashMap<>(6);
map.put("typeId", typeId);
map.put("title", title);
map.put("firstDate", firstDate);
map.put("secondDate", secondDate);
int totalCount = blogService.getCount(map);
PageBean pageBean = new PageBean(totalCount, page, pageSize);
map.put("start", pageBean.getStart());
map.put("size", pageSize);
model.addAttribute("pagination", pageBean);
List<BlogType> blogTypeList = blogTypeService.getTypeList();
model.addAttribute("blogTypeList",blogTypeList);
StringBuilder param = new StringBuilder(); // 分页查询参数
param.append(StringUtils.isEmpty(title) ? "" : "title=" + title);
param.append(StringUtils.isEmpty(firstDate) ? "" : "&firstDate=" + firstDate);
param.append(StringUtils.isEmpty(secondDate) ? "" : "&secondDate=" + secondDate);
param.append(typeId == null ? "" : "&typeId=" + typeId);
String pageCode = PageUtils.genPagination(request.getContextPath() + "/blog/list.do",
pageBean, param.toString());
model.addAttribute("pageCode", pageCode);
model.addAttribute("entry", map);
model.addAttribute("blogList", blogService.getBlogList(map));
return "blog/list";
}
@RequestMapping("/toAdd")
public String toAdd(Model model) {
model.addAttribute("blogTypeList", blogTypeService.getTypeList());
return "blog/add";
}
@RequestMapping("/toUpdate")
public String toUpdate(Integer id, Model model) {
model.addAttribute("blogTypeList", blogTypeService.getTypeList());
Blog blog = blogService.findById(id);
model.addAttribute("blog", blog);
BlogType blogType = blog.getBlogType();
if(blogType != null){
model.addAttribute("typeId", blogType.getTypeId());
}
return "blog/update";
}
@RequestMapping("/add")
public void add(Blog blog, @RequestParam(value = "img") MultipartFile file,
Model model) throws Exception {
// 获取原始文件名
String fileName = file.getOriginalFilename();
int index = fileName.indexOf(".");
String imageUrl = null;
String imagePath = DateUtils.getTimeStrForImage();
if (index != -1) {
//生成新文件名
imageUrl = imagePath + fileName.substring(index);
log.info("add {}", imagePath);
handleFileUpload(file, imageUrl);
blog.setImage(imageUrl);
}
// 添加博客及索引
int result = blogService.add(blog);
model.addAttribute("msg", result > 0 ? "保存成功" : "保存失败");
}
@RequestMapping("/update")
public void update(Blog blog, @RequestParam(value = "img", required=false) MultipartFile file,
Model model) throws Exception {
if (file != null) { //上传图片
// 获取原始文件名
String fileName = file.getOriginalFilename();
int index = fileName.indexOf(".");
String imageUrl = null;
String imagePath = DateUtils.getTimeStrForImage();
if(index != -1){
//生成新文件名
imageUrl = imagePath + fileName.substring(index);
log.info("update {}", imagePath);
handleFileUpload(file,imageUrl);
blog.setImage(imageUrl);
}
}
//更新博客及索引
int result = blogService.update(blog);
model.addAttribute("msg", result > 0 ? "保存成功" : "保存失败");
}
@RequestMapping("/delete")
public String delete(Integer id) throws IOException {
// 删除博客、索引及评论
blogService.delete(id);
return "redirect:/blog/list.do";
}
@RequestMapping("/deletes")
public String deletes(String ids) throws IOException {
String[] idArr = org.springframework.util.StringUtils.commaDelimitedListToStringArray(ids);
int len = idArr.length;
Integer[] blogIds = new Integer[len];
for (int i = 0; i < len; i++) {
blogIds[i] = Integer.parseInt(idArr[i]);
}
blogService.batchDelete(blogIds);
return "redirect:/blog/list.do";
}
private void handleFileUpload(MultipartFile file, String imageUrl) {
try (InputStream is = file.getInputStream()) {
// 获取输入流
String filePath = Thread.currentThread().getContextClassLoader().getResource("").getPath().substring(0,Thread.currentThread().getContextClassLoader().getResource("").getPath().length()-16)+Constants.COVER_DIR + imageUrl;
File dir = new File(filePath.substring(0, filePath.lastIndexOf("/")));
//判断上传目录是否存在
if (!dir.exists()) {
dir.mkdirs();
}
try (FileOutputStream fos = new FileOutputStream(new File(filePath))) {
byte[] buffer = new byte[1024];
int len = 0;
// 读取输入流中的内容
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
}
} catch (Exception e) {
log.error("图片上传失败", e);
}
}
}
如果也想学习本系统,下面领取。关注并回复:128ssm