2

I want to add Paging functionality in Spring Boot + Spring Data JPA App, I have Repository :

@Repository
public interface PostRepository extends JpaRepository<Post, Long> {
    List<Post> findAllByOrderByPostIdDesc(Pageable pageable);
}

and in Service class I have :

@Service
public class PostService {

    @Autowired
    private PostRepository postRepository;

    public List<Post> findAll(Pageable pageable) {
        return postRepository.findAllByOrderByPostIdDesc(pageable);
    }
}

in the end Controller is

@Controller
public class HomeController {

    @Autowired
    private PostService postService;

    @GetMapping(value = "/")
    public String getHomePage(Model model, Pageable pageable){
        model.addAttribute("postList", postService.findAll(pageable));
        return "home";
    }
}

the code above on http://localhost:8080 mapping I get all the records as response, but if I pass page and size params as http://localhost:8080/?page=0&size=1 I get paging working.

Now I want to make http://localhost:8080/?page=0&size=1 by default landing page address, and in HTML code how can I dynamically update values of params page and size which is :

 <ul class="pagination justify-content-center mb-4">
            <li class="page-item">
              <a class="page-link" th:href="@{/(page=0,size=1)}">&larr; Older</a>
            </li>
            <li class="page-item disabled">
              <a class="page-link" th:href="@{/(page=0,size=1)}">Newer &rarr;</a>
            </li>
          </ul>

I have tried a many tricks but none worked for me!!

Update

I tried and added this class as suggested but @Nyle Hassan,

@Configuration
public class DefaultView extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers( ViewControllerRegistry registry ) {
        registry.addViewController( "/" ).setViewName( "forward:/?page=0&size=10" );
        registry.setOrder( Ordered.HIGHEST_PRECEDENCE );
        super.addViewControllers( registry );
    }
}

it resulted in :

2019-01-01 17:26:43.484 ERROR 3824 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] threw exception

java.lang.StackOverflowError: null
    at javax.servlet.http.HttpServletRequestWrapper.getSession(HttpServletRequestWrapper.java:231) ~[tomcat-embed-core-8.5.29.jar:8.5.29]
    at org.apache.catalina.core.ApplicationHttpRequest.getSession(ApplicationHttpRequest.java:597) ~[tomcat-embed-core-8.5.29.jar:8.5.29]
    at javax.servlet.http.HttpServletRequestWrapper.getSession(HttpServletRequestWrapper.java:231) ~[tomcat-embed-core-8.5.29.jar:8.5.29]
    at org.apache.catalina.core.ApplicationHttpRequest.getSession(ApplicationHttpRequest.java:597) ~[tomcat-embed-core-8.5.29.jar:8.5.29]
    at javax.servlet.http.HttpServletRequestWrapper.getSession(HttpServletRequestWrapper.java:231) ~[tomcat-embed-core-8.5.29.jar:8.5.29]
    at org.apache.catalina.core.ApplicationHttpRequest.getSession(ApplicationHttpRequest.java:597) ~[tomcat-embed-core-8.5.29.jar:8.5.29]
    at javax.servlet.http.HttpServletRequestWrapper.getSession(HttpServletRequestWrapper.java:231) ~[tomcat-embed-core-8.5.29.jar:8.5.29]
    at org.apache.catalina.core.ApplicationHttpRequest.getSession(ApplicationHttpRequest.java:597) ~[tomcat-embed-core-8.5.29.jar:8.5.29]
    at javax.servlet.http.HttpServletRequestWrapper.getSession(HttpServletRequestWrapper.java:231) ~[tomcat-embed-core-8.5.29.jar:8.5.29]
    at org.apache.catalina.core.ApplicationHttpRequest.getSession(ApplicationHttpRequest.java:597) ~[tomcat-embed-core-8.5.29.jar:8.5.29]
Arshad Ali
  • 3,082
  • 12
  • 56
  • 99

1 Answers1

3

the code above on http://localhost:8080 mapping I get all the records as response

Are you sure about this? Which version of Spring Boot are you using? I checked latest version (2.1.1) and calling http://localhost:8080 without any query parameters will instantiate a Pageable object with page = 0, size = 20, no sorting. Just try to set a debug point to org.springframework.data.web.PageableHandlerMethodArgumentResolver#resolveArgument to learn, how Spring performs this binding.

This argument resolver also comes with an annotation, which allows you to change defaults, which are used, when query parameters are not present. You can use it like this:

@GetMapping(value = "/")
public String getHomePage(Model model, @PageableDefault(page=0, size = 1) Pageable pageable) {
    ...
}

Page 0 is annotation default, so this has the same effect:

@GetMapping(value = "/")
public String getHomePage(Model model, @PageableDefault(size = 1) Pageable pageable) {
    ...
}

in HTML code how can I dynamically update values of params page and size

Next time, please post two separate questions, but here it goes:

First, change List<Post> to Page<Post> in your repository and service. In your controller, bind the result to the model:

Page<Post> page = postService.findAll(pageable);
model.addAttribute("page", page); // page.getContent() contains the List<Page> now

And in the view, use the page to render paging links, e.g. like this:

<li class="page-item">
    <a class="page-link" th:if="${page.hasPrevious()}" th:href="@{/(page=${page.previousPageable().getPageNumber()},size=${page.getSize()})}">&larr; Older</a>
</li>
<li class="page-item disabled">
    <a class="page-link" th:if="${page.hasNext()}" th:href="@{/(page=${page.nextPageable().getPageNumber()},size=${page.getSize()})}">Newer &rarr;</a>
</li>
ygor
  • 1,726
  • 1
  • 11
  • 23