java期末实训项目(1)

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实训项目,项目前端页面以及搭建完成,所以我们只需要关注后端功能接口以及数据库。

仅为学习使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值