use of org.springframework.data.domain.Pageable in project halo by ruibaby.
the class IndexController method index.
/**
* 首页分页
*
* @param model model
* @param page page
* @param size size
* @return freemarker
*/
@GetMapping(value = "page/{page}")
public String index(Model model, @PathVariable(value = "page") Integer page) {
Sort sort = new Sort(Sort.Direction.DESC, "postDate");
// 默认显示10条
Integer size = 10;
// 尝试加载设置选项,用于设置显示条数
if (!StringUtils.isBlank(HaloConst.OPTIONS.get("index_posts"))) {
size = Integer.parseInt(HaloConst.OPTIONS.get("index_posts"));
}
// 所有文章数据,分页
Pageable pageable = new PageRequest(page - 1, size, sort);
Page<Post> posts = postService.findPostByStatus(0, pageable);
model.addAttribute("posts", posts);
// 文章总数
model.addAttribute("postsCount", postService.findAllPosts().size());
model.addAttribute("is_home", true);
// 用户信息
User user = userService.findUser();
model.addAttribute("user", user);
// 所有分类目录
List<Category> categories = categoryService.findAllCategories();
model.addAttribute("categories", categories);
// 菜单列表
List<Menu> menus = menuService.findAllMenus();
model.addAttribute("menus", menus);
// 归档数据,包含[year,month,count,List<Post>]
List<Archive> archives = postService.findPostGroupByPostDate();
model.addAttribute("archives", archives);
// 设置选项
model.addAttribute("options", HaloConst.OPTIONS);
return this.render("index");
}
use of org.springframework.data.domain.Pageable in project halo by ruibaby.
the class IndexController method feed.
/**
* 获取文章rss
*
* @return rss
*/
@GetMapping(value = { "feed", "feed.xml", "atom.xml" }, produces = { MediaType.APPLICATION_ATOM_XML_VALUE, MediaType.APPLICATION_RSS_XML_VALUE })
@ResponseBody
public String feed() {
String rssPosts = HaloConst.OPTIONS.get("rss_posts");
if (StringUtils.isBlank(rssPosts)) {
rssPosts = "20";
}
// 获取文章列表并根据时间排序
Sort sort = new Sort(Sort.Direction.DESC, "postDate");
Pageable pageable = new PageRequest(0, Integer.parseInt(rssPosts), sort);
Page<Post> postsPage = postService.findPostByStatus(0, pageable);
List<Post> posts = postsPage.getContent();
return postService.buildRss(posts);
}
use of org.springframework.data.domain.Pageable in project halo by ruibaby.
the class IndexController method archives.
/**
* 文章归档,根据年月
*
* @param model model
* @param year year
* @param month month
* @return string
*/
@GetMapping(value = "/archives/{year}/{month}")
public String archives(Model model, @PathVariable(value = "year") String year, @PathVariable(value = "month") String month) {
log.info(year);
log.info(month);
// 根据年月查出的文章数据,分页
Sort sort = new Sort(Sort.Direction.DESC, "post_date");
Pageable pageable = new PageRequest(0, 5, sort);
Page<Post> posts = postService.findPostByYearAndMonth(year, month, pageable);
model.addAttribute("posts", posts);
// 文章总数
model.addAttribute("postsCount", postService.findAllPosts().size());
// 用户信息
User user = userService.findUser();
model.addAttribute("user", user);
// 分类目录
List<Category> categories = categoryService.findAllCategories();
model.addAttribute("categories", categories);
// 菜单列表
List<Menu> menus = menuService.findAllMenus();
model.addAttribute("menus", menus);
// 归档数据,包含[year,month,count,List<Post>]
List<Archive> archives = postService.findPostGroupByPostDate();
model.addAttribute("archives", archives);
// 是否是归档页,用于判断输出链接
model.addAttribute("isArchives", "true");
// 设置选项
model.addAttribute("options", HaloConst.OPTIONS);
return this.render("archives");
}
use of org.springframework.data.domain.Pageable in project halo by ruibaby.
the class AttachmentController method attachments.
/**
* 获取upload的所有图片资源并渲染页面
*
* @param model model
* @return String
*/
@GetMapping
public String attachments(Model model, @RequestParam(value = "page", defaultValue = "0") Integer page, @RequestParam(value = "size", defaultValue = "18") Integer size) {
Sort sort = new Sort(Sort.Direction.DESC, "attachId");
Pageable pageable = new PageRequest(page, size, sort);
Page<Attachment> attachments = attachmentService.findAllAttachments(pageable);
model.addAttribute("attachments", attachments);
// 设置选项
model.addAttribute("options", HaloConst.OPTIONS);
return "admin/admin_attachment";
}
use of org.springframework.data.domain.Pageable in project halo by ruibaby.
the class PostController method searchPost.
/**
* 模糊查询文章
*
* @param model Model
* @param keyword keyword
* @param page page
* @param size size
* @return freemarker
*/
@PostMapping(value = "/search")
public String searchPost(Model model, @RequestParam(value = "keyword") String keyword, @RequestParam(value = "page", defaultValue = "0") Integer page, @RequestParam(value = "size", defaultValue = "10") Integer size) {
try {
// 排序规则
Sort sort = new Sort(Sort.Direction.DESC, "postId");
Pageable pageable = new PageRequest(page, size, sort);
model.addAttribute("posts", postService.searchPosts(keyword, pageable));
} catch (Exception e) {
log.error("未知错误:" + e.getMessage());
}
return "admin/admin_post";
}
Aggregations