【学习日记】(SpringBoot-part 5)新闻管理系统—主页功能

新闻管理系统的主页功能

查看静态资源里的主页界面,需要实现主页展示分类、标签、新闻和推荐新闻功能和点击新闻进入相应的新闻查看界面功能
在这里插入图片描述

信息展示

主页展示使用web目录下的indexController,查看静态界面我们一共需要四个部分的展示功能,分别是

1. 新闻展示
在这里我们需要提取到数据库中的所有新闻信息,然后进行分页展示,首先先在NewService接口中声明一个分页查询的方法

    //主页分页展示
    Page<News> listNew(Pageable pageable);

再在Impl中实现该方法,直接使用jpa中根据分页查询的方法

    @Override
    public Page<News> listNew(Pageable pageable) {
   
        return newRepository.findAll(pageable);
    }

最后再在indexController中调用该方法显示

@Controller
public class indexController {
   

    @Autowired
    private NewService newService;

	@GetMapping("/")
    public String index(@PageableDefault(size = 3,sort = {
   "updateTime"},direction = Sort.Direction.DESC)
                        Pageable pageable, Model model){
   
        model.addAttribute("page",newService.listNew(pageable));
        return "index";
    }
}

2. 分类展示
这里我们自定义一个查询数据库方法,在typeRepository中声明一个查询type方法,其他部分和新闻展示一样,需要先在TypeService中声明一个接口方法,分类不需要分页,只需要展示出固定几条分类m所以设计为以下即可,size表示展示的数据量

@Query("select t from Type t")
List<Type> findTop(Pageable pageable);
//不分页的List  在查找新闻界面显示出
List<Type> listTypeTop(Integer size);

Impl中实现该方法,根据分类下的新闻数量进行排序

//显示在主页
@Override
public List<Type> listTypeTop(Integer size) {
   
    Sort sort = Sort.by(Sort.Direction.DESC,"news.size");
    Pageable pageable = PageRequest.of(0,size,sort);
    return typeRepository.findTop(pageable);
}

最后再在indexController中添加该方法显示

@Controller
public class indexController {
   

    @Autowired
    private NewService newService;
    @Autowired
    private TypeService typeService;

	@GetMapping("/")
    public String index(@PageableDefault(size = 3,sort = {
   "updateTime"},direction = Sort.Direction.DESC)
                        Pageable pageable, Model model){
   
        model.addAttribute("page",newService.listNew(pageable));
        model.addAttribute("types",typeService.listTypeTop(3)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值