java期末实训项目(1)
由于项目整体框架已经搭建好,所以只需要完成项目里面核心的功能模块。
- (1)
后台文章的添加管理:
后端文章添加使用富文本编辑器editor.md,由于这个编辑器是makerdown语法,前端页面不能直接展示,需要转换为html,这个后续再在代码里实现,
发表第一篇测试文章
点击保存后发送一个/admin/article的post请求
@PostMapping(value = "/publish")
@ResponseBody
public APIResponse publishArticle(
@RequestParam(name = "title", required = true)
String title,
@RequestParam(name = "titlePic", required = false)
String titlePic,
@RequestParam(name = "slug", required = false)
String slug,
@RequestParam(name = "content", required = true)
String content,
@RequestParam(name = "type", required = true)
String type,
@RequestParam(name = "status", required = true)
String status,
@RequestParam(name = "categories", required = false, defaultValue = "默认分类")
String categories,
@RequestParam(name = "tags", required = false)
String tags,
@RequestParam(name = "allowComment", required = true)
Boolean allowComment
) {
ContentDomain contentDomain = new ContentDomain();
// 设置默认浏览量为1次
contentDomain.setHits(1);
contentDomain.setTitle(title);
contentDomain.setTitlePic(titlePic);
contentDomain.setSlug(slug);
contentDomain.setContent(content);
contentDomain.setType(type);
contentDomain.setStatus(status);
// 评论数
contentDomain.setCommentsNum(0);
// 只允许博客文章有分类,防止作品被收入分类
contentDomain.setTags(type.equals(Types.ARTICLE.getType()) ? tags : null);
contentDomain.setCategories(type.equals(Types.ARTICLE.getType()) ? categories : null);
contentDomain.setAllowComment(allowComment ? 1 : 0);
// 添加文章
contentService.addArticle(contentDomain);
return APIResponse.success();
}
后端处理请求代码:
将接收到的请求参数封装为ContentDomain 实体对象,依次传入mapper层,保存到后端数据库
文章保存成功,数据库新增一条记录。
数据库中有数据后可以通过前台页面来展示文章。
先写文章请求接口:
@GetMapping(value = "/")
public String index(
HttpServletRequest request,
@RequestParam(name = "page", required = false, defaultValue = "1")
int page,
@RequestParam(name = "limit", required = false, defaultValue = "5")
int limit
) {
PageInfo<ContentDomain> articles = contentService.getArticlesByCond(new ContentCond(), page, limit);
request.setAttribute("articles",articles);
return "blog/home";
}
后端使用pagehelper插件分页,请求到博客文章的list,转发到首页,
页面使用thymeleaf模板进行填充数据;
我们在此位置使用循环依次填充相关数据;
功能接口写完后,我们启动项目测试接口是否存在异常,如果一切正常,
我们就可以看到前台页面的展示效果
测试正常,到此文章的添加功能及展示功能完成。
此项目为学期期末javaee实训项目,项目前端页面以及搭建完成,所以我们只需要关注后端功能接口以及数据库。
仅为学习使用