package blog.controller;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.github.pagehelper.PageInfo;
import blog.entity.Article;
import blog.entity.Category;
import blog.entity.Comment;
import blog.entity.Tag;
import blog.entity.User;
import blog.service.ArticleService;
import blog.service.CategoryService;
import blog.service.CommentService;
import blog.service.TagService;
import cn.hutool.http.HtmlUtil;
@Controller
@RequestMapping("/article")
public class ArticleController {
@Resource(name = "articleServiceImpl")
ArticleService articleService;
@Resource(name = "categoryServiceImpl")
CategoryService categoryService;
@Resource(name = "tagServiceImpl")
private TagService tagService;
@Resource(name = "commentServiceImpl")
private CommentService commentService;
@RequestMapping("/{pageIndex}") // 设置分页的大小和第几页参数
public String index(@RequestParam(required = false, defaultValue = "9") Integer pageSize, // 显示所有文章
@PathVariable("pageIndex") Integer pageIndex, ModelMap map, String articleTitle, String categoryId,
String tagId, String articleStatus) {
// 分页
PageInfo<Article> articePageInfo = articleService.getPageArticleList(pageSize, pageIndex, articleTitle,
categoryId, tagId, articleStatus);
// 查询所有分类
List<Category> categoryList = categoryService.getCategoryList();
// 查询所有文章然后跳转到全部文章界面
// PageInfo<Article>
// articePageInfo=articleService.getPageArticleList(pageSize,pageIndex);
// 转向用的url
map.put("pageUrlPrefix", "article/");
// 分页用的pageinfo
map.put("pageInfo", articePageInfo);
// 标签的id
map.put("tagId", tagId);
// 多条件查询的分类因为是下拉菜单所有手动put回去
map.put("categoryId", categoryId);
// 分类列表
map.put("categoryList", categoryList);
// 文章的状态
map.put("articleStatus", articleStatus);
return "Article/article-list";
}
@RequestMapping("/go") // 添加文章的转向
public String goAdd(ModelMap map) {
// 标签数据和分类数据
List<Tag> tagList = tagService.getTagList();
List<Category> parentCategoryList = categoryService.getParentCategoryList();
map.put("tagList", tagList);
map.put("parentCategoryList", parentCategoryList);
return "Article/article-add";
}
@ResponseBody
@RequestMapping("/category") // 返回json格式
public List<Category> categoryAjax(Integer parentCategoryId) {
List<Category> categoryList = categoryService.getCategoryListByPid(parentCategoryId);
return categoryList;
}
@RequestMapping("/add") // 真正的添加文章
public String add(HttpServletRequest request, Article article) {
// 文章的标题,内容,文章状态都会自动带过来
// 当前用户的id
User user = (User) request.getSession().getAttribute("session_user");
if (user != null)
article.setArticleUserId(user.getUserId());
// 文章发布时间,修改时间
article.setArticleCreateTime(new Date());
article.setArticleUpdateTime(new Date());
// 浏览 回复 喜欢 数
article.setArticleCommentCount(0);
article.setArticleLikeCount(0);
article.setArticleViewCount(0);
article.setArticleIsComment(0);
// 默认的排序
article.setArticleOrder(1);
// 文章的摘要 取前150个字符
String s = HtmlUtil.cleanHtmlTag(article.getArticleContent());// 过滤掉html标签hutu工具包的方法
article.setArticleSummary(s.length() > 150 ? s.substring(0, 150) : s);// 取前150个字符
// 标签id
String[] tagIds = request.getParameterValues("articleTagIds");
// 把所有标签id包装成标签放到文章信息里
List<Tag> tagList = new ArrayList<>();
for (String id : tagIds) {
tagList.add(new Tag(Integer.valueOf(id)));
}
article.setTagList(tagList);
// 分类父级id
Integer parentCateId = Integer.valueOf(request.getParameter("articleParentCategoryId"));
// 子级id
Integer childCateId = Integer.valueOf(request.getParameter("articleChildCategoryId"));
// 把所属分类放到一个集合里面再放到文章信息里
List<Category> categoryList = new ArrayList<>(2);
categoryList.add(new Category(parentCateId));
categoryList.add(new Category(childCateId));
article.setCategoryList(categoryList);
articleService.addArticle(article);
return "redirect:/article/1";
}
@RequestMapping("/delete/{id}") // 删除文章根据id
public void delArticle(@PathVariable("id") Integer id) {
articleService.delArticleById(id);
}
@RequestMapping("/edit/{id}") // 修改文章的转向
public String goUpdate(ModelMap map, @PathVariable("id") Integer id) {
// 根据文章id查文章
Article article = articleService.getArticleById(id);
// 标签数据
List<Tag> tagList = tagService.getTagList();
// 分类数据父级分类,用Ajax获得子类
List<Category> parentCategoryList = categoryService.getParentCategoryList();
// 关联表的标签id和分类id
List<Integer> tagIdList = articleService.getTagIdsList(id);
List<Integer> categoryIdList = articleService.getCategoryIdByArticleId(id);
// 如果是草稿的话可能没有分类和标签,所有get不到
if (categoryIdList.size() != 0) {
map.put("categoryPid", categoryIdList.get(0));// 这个父分类id
map.put("categoryId", categoryIdList.get(1));// 这个子分类id
}
map.put("tagIdList", tagIdList);// 这个文章的标签
map.put("article", article);
map.put("tagList", tagList);
map.put("parentCategoryList", parentCategoryList);
return "Article/article-edit";
}
@RequestMapping("/update") // 真正的修改文章
public String update(HttpServletRequest request, Article article) {
// 文章的标题,内容,文章状态都会自动带过来
// 当前用户的id
User user = (User) request.getSession().getAttribute("session_user");
if (user != null)
article.setArticleTitle(request.getParameter("articleTitle"));
article.setArticleContent(request.getParameter("articleContent"));
article.setArticleStatus(Integer.parseInt(request.getParameter("articleStatus")));
article.setArticleUserId(user.getUserId());
// 文章修改时间
article.setArticleUpdateTime(new Date());
// 标签id
String[] tagIds = request.getParameterValues("articleTagIds");
// 把所有标签id包装成标签放到文章信息里
List<Tag> tagList = new ArrayList<>();
for (String id : tagIds) {
tagList.add(new Tag(Integer.valueOf(id)));
}
article.setTagList(tagList);
// 分类父级id
Integer parentCateId = Integer.valueOf(request.getParameter("articleParentCategoryId"));
// 子级id
Integer childCateId = Integer.valueOf(request.getParameter("articleChildCategoryId"));
// 把所属分类放到一个集合里面再放到文章信息里
List<Category> categoryList = new ArrayList<>(2);
categoryList.add(new Category(parentCateId));
categoryList.add(new Category(childCateId));
article.setCategoryList(categoryList);
articleService.updateArticle(article);
return "redirect:/article/1";
}
@RequestMapping("/uploadImg") // 富文本编译器的上传图片
public String uploadImg(MultipartHttpServletRequest request) throws Exception {
// imgFile 这个名称是固定的
MultipartFile file = request.getFile("imgFile");
// 生成一个随机的文件名
String newName = UUID.randomUUID().toString() + ".jpg";
File destFile = new File("D:/imguploads/" + newName);
String path = "http://localhost:8080/uploads/" + newName;
file.transferTo(destFile);
return "{\"error\":0,\"url\":\"" + path + "\"}";
}
@RequestMapping("/draft") // 显示所有草稿
public String draft(ModelMap map) {
// 分页
PageInfo<Article> articePageInfo = articleService.getPageArticleList(9, 1, " ", "", " ", "0");
// 查询所有分类
List<Category> categoryList = categoryService.
没有合适的资源?快使用搜索试试~ 我知道了~
开发工具:ECLIPSE + TOMCAT + MYSQL + NAVICAT 使用技术:JAVA, SPRING, MYBA
共630个文件
gif:234个
js:78个
css:54个
需积分: 0 2 下载量 196 浏览量
2022-04-14
18:14:14
上传
评论
收藏 5.14MB ZIP 举报
温馨提示
开发工具:ECLIPSE + TOMCAT + MYSQL + NAVICAT 使用技术:JAVA, SPRING, MYBA 项目描述:该项目为一个博客系统,具有文章的发布修改功能,修改文章分类,添加新的用户,发表评论。
资源详情
资源评论
资源推荐
收起资源包目录
开发工具:ECLIPSE + TOMCAT + MYSQL + NAVICAT
使用技术:JAVA, SPRING, MYBA (630个子文件)
layui.css 50KB
layui.css 50KB
layui.css 50KB
dashicons.min.css 45KB
dashicons.min.css 45KB
dashicons.min.css 45KB
font-awesome.css 37KB
font-awesome.css 37KB
font-awesome.css 37KB
font-awesome.min.css 32KB
font-awesome.min.css 32KB
font-awesome.min.css 32KB
login.min.css 25KB
login.min.css 25KB
login.min.css 25KB
forms.min.css 19KB
forms.min.css 19KB
forms.min.css 19KB
layer.css 14KB
layer.css 14KB
layer.css 14KB
layer.css 14KB
layer.css 14KB
layer.css 14KB
layui.mobile.css 10KB
layui.mobile.css 10KB
layui.mobile.css 10KB
back.css 9KB
back.css 9KB
back.css 9KB
laydate.css 7KB
laydate.css 7KB
laydate.css 7KB
buttons.min.css 6KB
buttons.min.css 6KB
buttons.min.css 6KB
highlight.css 5KB
highlight.css 5KB
highlight.css 5KB
index-page.css 5KB
index-page.css 5KB
index-page.css 5KB
layer.css 5KB
layer.css 5KB
layer.css 5KB
l10n.min.css 2KB
l10n.min.css 2KB
l10n.min.css 2KB
code.css 1KB
code.css 1KB
code.css 1KB
login.css 656B
login.css 656B
login.css 656B
fontawesome-webfont.eot 162KB
fontawesome-webfont.eot 162KB
fontawesome-webfont.eot 162KB
iconfont.eot 32KB
iconfont.eot 32KB
iconfont.eot 32KB
59.gif 10KB
59.gif 10KB
59.gif 10KB
22.gif 10KB
22.gif 10KB
22.gif 10KB
24.gif 8KB
24.gif 8KB
24.gif 8KB
13.gif 7KB
13.gif 7KB
13.gif 7KB
16.gif 7KB
16.gif 7KB
16.gif 7KB
39.gif 6KB
39.gif 6KB
39.gif 6KB
64.gif 6KB
64.gif 6KB
64.gif 6KB
63.gif 6KB
63.gif 6KB
63.gif 6KB
50.gif 6KB
50.gif 6KB
50.gif 6KB
loading-0.gif 6KB
loading-0.gif 6KB
loading-0.gif 6KB
loading-0.gif 6KB
loading-0.gif 6KB
loading-0.gif 6KB
4.gif 6KB
4.gif 6KB
4.gif 6KB
1.gif 5KB
1.gif 5KB
1.gif 5KB
42.gif 5KB
共 630 条
- 1
- 2
- 3
- 4
- 5
- 6
- 7
无处安放的小曾
- 粉丝: 78
- 资源: 27
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0