查看静态资源里的主页界面,需要实现主页展示分类、标签、新闻和推荐新闻功能和点击新闻进入相应的新闻查看界面功能
信息展示
主页展示使用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)