使用Spring boot

时间:2017-08-06 12:14:26

标签: spring twitter-bootstrap

我想使用Spring-boot将Pageable添加到我的项目中。我刚学春天,我遇到了一些教程,但我不知道如何在我的代码中实现它。我想在每个页面中显示5个用户。点击按钮,我将通过5个显示的用户批次。

这是我的代码: 的 UserRepository.java

@Repository
public interface UserRepository extends CrudRepository<User, Long>{

    Page<User> findUsersByPage(Pageable pageable);

}

UserController.java

@RequestMapping(value = "/usersList", method = RequestMethod.GET)
    public String userList(Model model) {
        model.addAttribute("users", userRepository.findAll());
        return "usersList";
    }

我也有来自bootstrap的分页代码

的index.html

<ul class="pagination">
  <li class="page-item"><a class="page-link" href="#" aria-
     label="Previous"> <span aria-hidden="true">&laquo;</span>
     <span class="sr-only">Previous</span>
   </a></li>
   <li class="page-item"><a class="page-link" href="#">1</a></li>
   <li class="page-item"><a class="page-link" href="#">2</a></li>
   <li class="page-item"><a class="page-link" href="#">3</a></li>
   <li class="page-item"><a class="page-link" href="#" aria-label="Next"> 
   <span aria-hidden="true">&raquo;</span>
   <span class="sr-only">Next</span>
   </a></li>
   </ul>

2 个答案:

答案 0 :(得分:0)

看一下Spring HATEOS,你需要某种(AJAX)查询来检索数据。

答案 1 :(得分:0)

前几天我做了类似的事情。当我滚动到页面底部时,我会加载下6篇博文。

<强> PostRepository.java

@Repository
public interface PostRepository extends JpaRepository<Post, Long> {

    @Query("SELECT p FROM Post p ORDER BY p.date DESC")
    List<Post> findLatestFromPage(Pageable pageable);
}

制作服务是一种很好的做法,因为它使控制器独立于存储库:

<强> PostService.java

public interface PostService {

    List<Post> findLatest6FromPage(int page);
}

<强> PostServiceImpl.java

@Service
public class PostServiceImpl implements PostService {

    private final PostRepository postRepository;

    @Autowired
    public PostServiceImpl(PostRepository postRepository) {
        this.postRepository = postRepository;
    }

    @Override
    public List<Post> findLatest6FromPage(int page) {
        return postRepository.findLatestFromPage(new PageRequest(page, 6));
    }
}

<强> PostController.java

@RestController()
@RequestMapping("/api/posts")
public class PostController {

    private final PostService postService;

    @Autowired
    public PostController(PostService postService) {
        this.postService = postService;
    }

    @GetMapping("/latest6FromPage")
    public List<Post> getLatest6PostsFromPage(@RequestParam("page") int page) {
        return postService.findLatest6FromPage(page);
    }
}

我打电话给网址从第2页获得6个帖子:

http://localhost:8080/api/posts/latest6FromPage?page=1

我的控制器是RestController所以我使用URL来获取带有帖子的json,但是如果你想将用户列表直接传递给HTML文件,你的控制器可能如下所示:

@Controller
public class PostsController {

    @Autowired
    private PostService postService;

    @RequestMapping("/postList")
    public String postList(@RequestParam("page") int page, Model model) {
        List<Post> posts = postService.findLatest6FromPage(page);

        model.addAttribute("posts", posts);
        return "postList";
    }
}