锋哥原创的Springboot+Layui python222网站实战:
/**
* 首页
*
* @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;
}
}