Repository navigation
Expand file tree
/
Copy pathHomeController.java
More file actions
48 lines (36 loc) · 1.45 KB
/
Copy pathHomeController.java
File metadata and controls
48 lines (36 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.raysmond.blog.controllers;
import com.raysmond.blog.Constants;
import com.raysmond.blog.models.Post;
import com.raysmond.blog.services.AppSetting;
import com.raysmond.blog.services.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import static org.springframework.web.bind.annotation.RequestMethod.*;
@Controller
public class HomeController {
@Autowired
private PostService postService;
@Autowired
private AppSetting appSetting;
@RequestMapping(value = "", method = GET)
public String index(@RequestParam(defaultValue = "1") int page, Model model) {
page = page < 1 ? 0 : page - 1;
Page<Post> posts = postService.getAllPublishedPostsByPage(page, appSetting.getPageSize());
model.addAttribute("totalPages", posts.getTotalPages());
model.addAttribute("posts", posts);
model.addAttribute("page", page + 1);
return "home/index";
}
@RequestMapping(value = "about", method = GET)
public String about(Model model) {
Post post = postService.getPublishedPostByPermalink(Constants.ABOUT_PAGE_PERMALINK);
if (post == null) {
post = postService.createAboutPage();
}
model.addAttribute("about", post);
return "home/about";
}
}