1.在springboot中需要引入jpa的starter依赖,数据渲染采用themeleaf。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2.TypeController.java
package com.wang.springboot.controller;
import com.wang.springboot.pojo.Type;
import com.wang.springboot.service.BlogService;
import com.wang.springboot.service.TagService;
import com.wang.springboot.service.TypeService;
import com.wang.springboot.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
/**
* Created by 王一宁 on 2017/10/23.
*/
@Controller
public class TypeShowController {
@Autowired
private TypeService typeService;
@Autowired
private BlogService blogService;
@Autowired
private TagService tagService;
/**/
@GetMapping("/types")
public String types(@PageableDefault(size = 10, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
HttpServletRequest request,
Model model) {
// 1.点击分类,拿到的分类id
String id = request.getParameter("id");
long id2 = Long.parseLong(id);
// 2.把分类id放入到session
request.getSession().setAttribute("id", id);
// 3.拿到所有的分类
List<Type> types = typeService.listTypeTop(10000);
/*if (id == -1) {
id = types.get(0).getId();
}*/
// 4.封装查询对象
// 或者自己实现根据id查询所有就好。
BlogQuery blogQuery = new BlogQuery();
blogQuery.setTypeId(id2);
//5.放入前台页面的实体
model.addAttribute("types", types);
model.addAttribute("page", blogService.listBlog(pageable, blogQuery));
return "list";
}
/*分页发送请求的地址*/
@GetMapping("/pages")
public String pages(@PageableDefault(size = 10, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
HttpServletRequest request,
Model model) {
// 1.拿到所有的分类
List<Type> types = typeService.listTypeTop(10000);
// 2.取出session存放的分类id
String id2 = (String) request.getSession().getAttribute("id");
Long id = Long.parseLong(id2);
// 3.查询对象,BlogQuery封装分类的ID,根据id查询
// 或者自己实现根据id查询所有就好。
BlogQuery blogQuery = new BlogQuery();
blogQuery.setTypeId(id);
model.addAttribute("types", types);
model.addAttribute("page", blogService.listBlog(pageable, blogQuery));
return "list";
}
}
3.前台页面
<li class="menu"><a href="">分类</a>
<ul class="sub">
<li th:each="type : ${types}"><a th:href="@{/types/{id}(id=${type.id})}" th:text="${type.name}">分类名称</a>
</li>
</ul>
<span></span>
</li>
/**
* 省略遍历内容的列表
*/
<div class="pagelist" th:if="${page.totalPages}>1">
<a th:href="@{/pages?page=0}" th:classappend="${page.number==0} ? 'curPage'" >首页</a>
<a th:href="@{/pages?page=1}" th:classappend="${page.number==1} ? 'curPage'">2</a>
<a th:href="@{/pages?page=1}" th:classappend="${page.number==2} ? 'curPage'">3</a>
<a th:href="@{/pages?page=2}" th:classappend="${page.number==3} ? 'curPage'">4</a>
<a th:href="@{/pages?page=3}" th:classappend="${page.number==4} ? 'curPage'">5</a>
<a>...</a>
<a th:href="@{/pages(page=${page.totalPages}-1)}" th:classappend="${page.number==page.totalPages-1} ? 'curPage'" >末页</a>
<a th:href="@{/pages(page=${page.number}-1)}" th:unless="${page.first}">上一页</a>
<a th:href="@{/pages(page=${page.number}+1)}" th:unless="${page.last}">下一页</a>
</div>
评论