Spring Boot项目实战4(主页信息展示+详情页)
2020-08-04 21:11
363 查看
接上:
https://blog.csdn.net/qq_43923042/article/details/107718118
上面的已经基本完成后台管理功能了,接下来是前台的制作
上面是完成图,可以看到主要需要分类,推荐,新闻,推荐四个列表,即要传这四个参数到前端。
以新闻为例子
1、DAO层:
public interface NewRepository extends JpaRepository<News, Long>, JpaSpecificationExecutor<News> { @Query("select m from News m where m.Title like ?1 or m.content like ?1") Page<News> findByQuery(String query, Pageable pageable); @Query("select n from News n where n.recommend=true") List<News> findTop(Pageable pageable); }
由于其他都是只需要一部分数据,且大概步骤相同,在此不多加描述
2、Service层:主要用到以下函数
public Page<News> listNews(Pageable pageable) { return newRepository.findAll(pageable); } //主页推荐 @Override public List<News> listRecommentNewTop(Integer size) { Sort sort = Sort.by(Sort.Direction.DESC, "updateTime"); Pageable pageable = PageRequest.of(0, size, sort); return newRepository.findTop(pageable); }
3、Controller层:
@Controller public class IndexCotroller { @Autowired private TypeService typeService; @Autowired private TagService tagService; @Autowired private NewService newService; @GetMapping("/") public String index(@PageableDefault(size = 5, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable, Model model) { model.addAttribute("page", newService.listNews(pageable)); model.addAttribute("types", typeService.listTypeTop(5)); model.addAttribute("tags", tagService.listTagTop(5)); model.addAttribute("recommendNews", newService.listRecommentNewTop(5)); return "index"; } @PostMapping("/search") public String search(@PageableDefault(size = 5, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable, @RequestParam String query, Model model) { model.addAttribute("page", newService.listNews("%" + query + "%", pageable)); model.addAttribute("query", query); return "search"; } @RequestMapping("/news/{id}") public String news(@PathVariable Long id, Model model) { model.addAttribute("news", newService.getAndConvert(id)); return "new"; } }
新闻详情的话只要是点击新闻列表就能点进去,就是一个跳转的问题,传过来的时候把id传过来再将数据返回新页面就好了
相关文章推荐
- Springboot项目-主页信息展示+详情页
- 中软国际实训十:SpringBoot项目——主页信息展示+详情页
- 【SpringBoot商城秒杀系统项目实战19】秒杀静态化+订单详情静态化
- 【SpringBoot商城秒杀系统项目实战15】订单详情页
- 【SpringBoot商城秒杀系统项目实战18】页面优化技术-商品详情页面静态化(前后端分离)
- [置顶] spring boot项目实战:跨域问题解决
- 【SpringBoot商城秒杀系统项目实战05】数据库设计与对应的domain对象
- 【SpringBoot商城秒杀系统项目实战12】商品列表页
- 【SpringBoot商城秒杀系统项目实战21】高并发秒杀系统接口优化 RabbitMQ异步下单
- [置顶] spring boot项目实战:swagger2在线文档
- [置顶] spring boot项目实战:swagger2在线文档
- 2018SpringBoot 仿抖音短视频小程序开发 全栈式实战项目最全
- 免费分享:5本微服务开发书籍,Spring Boot 2微服务项目实战,Spring Cloud+Docker,架构师12项修炼
- 基于SpringBoot从零构建博客网站 - 开发设置主页标识和修改个人信息功能
- [置顶] spring boot项目实战:redis
- [置顶] spring boot项目实战:JPA
- [置顶] spring boot项目实战:分布式锁
- SpringBoot实战_1 Spring项目搭建
- [置顶] spring boot项目实战-集合操作
- [置顶] spring boot项目实战之工具篇(json)