python222网站实战(SpringBoot+SpringSecurity+MybatisPlus+thymeleaf+layui)-贴子列表分页显示实现

发布时间:2024年01月19日

锋哥原创的Springboot+Layui python222网站实战:

python222网站实战课程视频教程(SpringBoot+Python爬虫实战) ( 火爆连载更新中... )_哔哩哔哩_bilibilipython222网站实战课程视频教程(SpringBoot+Python爬虫实战) ( 火爆连载更新中... )共计23条视频,包括:python222网站实战课程视频教程(SpringBoot+Python爬虫实战) ( 火爆连载更新中... )、第2讲 架构搭建实现、第3讲 页面系统属性动态化设计实现等,UP主更多精彩视频,请关注UP账号。icon-default.png?t=N7T8https://www.bilibili.com/video/BV1yX4y1a7qM/请求获取第一页数据;

/**
 * 首页
 *
 * @return
 */
@RequestMapping("/")
public ModelAndView index() {
    PageBean pageBean = new PageBean(1, 20);
    System.out.println("首页");
    ModelAndView mav = new ModelAndView();
    Page<Article> articlePage = articleService.page(new Page<>(pageBean.getPage(), pageBean.getPageSize()), new QueryWrapper<Article>().orderByDesc("publish_date"));
    mav.addObject("articleList", articlePage.getRecords());
    mav.setViewName("index");
    return mav;
}

网页上 显示数据:

<div class="post_list">
    <div class="post_item" th:each="article,status:${articleList}">
        <div class="post_item_text">
            <a class="post_item_title" th:href="'/article/'+${article.id}" target="_blank" th:text="${article.title}"></a>
            <div class="post_item_summary">
                <a th:href="'/article/'+${article.id}" target="_blank"><img class="avatar" th:src="'/randomImages/'+${(status.index)%10+1}+'.png'"/></a>
                <p th:text="${article.summary}"></p>
            </div>
        </div>
        <div class="publishDate" th:text="${#dates.format(article.publishDate, 'yyyy-MM-dd HH:mm:ss')}"></div>
    </div>
</div>

帖子随机小图标,映射路径:

randomImagesFilePath: D:\\randomImages\\
@Value("${randomImagesFilePath}")
private String randomImagesFilePath;
registry.addResourceHandler("/randomImages/**").addResourceLocations("file:"+randomImagesFilePath);  // 随机头像图片映射

分页实现:

mav.addObject("pageCode", PageUtil.genPagination("/article/list", articlePage.getTotal(), 1, pageBean.getPageSize(), ""));
<div class="pageCode" th:utext="${pageCode}"></div>
package com.python222.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.python222.entity.Article;
import com.python222.entity.PageBean;
import com.python222.service.ArticleService;
import com.python222.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 帖子Controller控制器
 * @author python222小锋老师
 * @site www.python222.com
 */
@Controller
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 分页查询帖子
     * @return
     */
    @RequestMapping("/list/{page}")
    public ModelAndView list(@PathVariable(value = "page",required = false)Integer page){
        PageBean pageBean=new PageBean(page,20);
        ModelAndView mav=new ModelAndView();
        Page<Article> articlePage = articleService.page(new Page<>(pageBean.getPage(), pageBean.getPageSize()), new QueryWrapper<Article>().eq("status",2).orderByDesc("publish_date"));
        mav.addObject("articleList",articlePage.getRecords());
        mav.addObject("pageCode", PageUtil.genPagination("/article/list",articlePage.getTotal(),page,pageBean.getPageSize(),""));
        mav.setViewName("index");
        return mav;
    }
}

文章来源:https://blog.csdn.net/caoli201314/article/details/135688521
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。