Spring Boot Data JPA Paging implementing in correct way
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)}">← Older</a>
</li>
<li class="page-item disabled">
<a class="page-link" th:href="@{/(page=0,size=1)}">Newer →</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]
java spring spring-boot pagination spring-data-jpa
add a comment |
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)}">← Older</a>
</li>
<li class="page-item disabled">
<a class="page-link" th:href="@{/(page=0,size=1)}">Newer →</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]
java spring spring-boot pagination spring-data-jpa
This might help stackoverflow.com/questions/26057995/…
– Nyle Hassan
Jan 1 at 12:21
@NyleHassan please see the update
– Arshad Ali
Jan 1 at 12:32
add a comment |
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)}">← Older</a>
</li>
<li class="page-item disabled">
<a class="page-link" th:href="@{/(page=0,size=1)}">Newer →</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]
java spring spring-boot pagination spring-data-jpa
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)}">← Older</a>
</li>
<li class="page-item disabled">
<a class="page-link" th:href="@{/(page=0,size=1)}">Newer →</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]
java spring spring-boot pagination spring-data-jpa
java spring spring-boot pagination spring-data-jpa
edited Jan 1 at 12:31
Arshad Ali
asked Jan 1 at 12:14
Arshad AliArshad Ali
1,41264071
1,41264071
This might help stackoverflow.com/questions/26057995/…
– Nyle Hassan
Jan 1 at 12:21
@NyleHassan please see the update
– Arshad Ali
Jan 1 at 12:32
add a comment |
This might help stackoverflow.com/questions/26057995/…
– Nyle Hassan
Jan 1 at 12:21
@NyleHassan please see the update
– Arshad Ali
Jan 1 at 12:32
This might help stackoverflow.com/questions/26057995/…
– Nyle Hassan
Jan 1 at 12:21
This might help stackoverflow.com/questions/26057995/…
– Nyle Hassan
Jan 1 at 12:21
@NyleHassan please see the update
– Arshad Ali
Jan 1 at 12:32
@NyleHassan please see the update
– Arshad Ali
Jan 1 at 12:32
add a comment |
1 Answer
1
active
oldest
votes
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()})}">← 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 →</a>
</li>
That's what which I needed, Thanks a lot Man!
– Arshad Ali
Jan 2 at 4:45
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995348%2fspring-boot-data-jpa-paging-implementing-in-correct-way%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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()})}">← 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 →</a>
</li>
That's what which I needed, Thanks a lot Man!
– Arshad Ali
Jan 2 at 4:45
add a comment |
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()})}">← 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 →</a>
</li>
That's what which I needed, Thanks a lot Man!
– Arshad Ali
Jan 2 at 4:45
add a comment |
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()})}">← 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 →</a>
</li>
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()})}">← 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 →</a>
</li>
edited Jan 1 at 21:34
answered Jan 1 at 21:03
ygorygor
1,1161615
1,1161615
That's what which I needed, Thanks a lot Man!
– Arshad Ali
Jan 2 at 4:45
add a comment |
That's what which I needed, Thanks a lot Man!
– Arshad Ali
Jan 2 at 4:45
That's what which I needed, Thanks a lot Man!
– Arshad Ali
Jan 2 at 4:45
That's what which I needed, Thanks a lot Man!
– Arshad Ali
Jan 2 at 4:45
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995348%2fspring-boot-data-jpa-paging-implementing-in-correct-way%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
This might help stackoverflow.com/questions/26057995/…
– Nyle Hassan
Jan 1 at 12:21
@NyleHassan please see the update
– Arshad Ali
Jan 1 at 12:32