You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
DATACMNS-689 - Improved reference documentation on web pagination.
Re-added index and defaulting information to the section that documents the currents state of web pagination. Removed legacy pagination sections as they're not really documenting the very legacy support (that would not even work anymore).
Added explicit anchors where missing.
Copy file name to clipboardExpand all lines: src/main/asciidoc/repositories.adoc
+5-143
Original file line number
Diff line number
Diff line change
@@ -675,6 +675,7 @@ The configuration setup shown above will register a few basic components:
675
675
- A `DomainClassConverter` to enable Spring MVC to resolve instances of repository managed domain classes from request parameters or path variables.
676
676
- `HandlerMethodArgumentResolver` implementations to let Spring MVC resolve Pageable and Sort instances from request parameters.
677
677
678
+
[[core.web.basic.domain-class-converter]]
678
679
===== DomainClassConverter
679
680
The `DomainClassConverter` allows you to use domain types in your Spring MVC controller method signatures directly, so that you don't have to manually lookup the instances via the repository:
680
681
@@ -700,6 +701,7 @@ As you can see the method receives a User instance directly and no further looku
700
701
701
702
NOTE: Currently the repository has to implement `CrudRepository` to be eligible to be discovered for conversion.
702
703
704
+
[[core.web.basic.paging-and-sorting]]
703
705
===== HandlerMethodArgumentResolvers for Pageable and Sort
704
706
The configuration snippet above also registers a `PageableHandlerMethodArgumentResolver` as well as an instance of `SortHandlerMethodArgumentResolver`. The registration enables `Pageable` and `Sort` being valid controller method arguments
705
707
@@ -728,8 +730,8 @@ This method signature will cause Spring MVC try to derive a Pageable instance fr
728
730
.Request parameters evaluated for Pageable instances
729
731
[options = "autowidth"]
730
732
|===============
731
-
|`page`|Page you want to retrieve.
732
-
|`size`|Size of the page you want to retrieve.
733
+
|`page`|Page you want to retrieve, 0 indexed and defaults to 0.
734
+
|`size`|Size of the page you want to retrieve, defaults to 20.
733
735
|`sort`|Properties that should be sorted by in the format `property,property(,ASC\|DESC)`. Default sort direction is ascending. Use multiple `sort` parameters if you want to switch directions, e.g. `?sort=firstname&sort=lastname,asc`.
734
736
|===============
735
737
@@ -908,6 +910,7 @@ public class UserController {
908
910
909
911
First you declare a repository dependency for each controller to look up the entity managed by the controller or repository respectively. Looking up the entity is boilerplate as well, as it's always a `findOne(…)` call. Fortunately Spring provides means to register custom components that allow conversion between a `String` value to an arbitrary type.
910
912
913
+
[[web.legacy.property-editors]]
911
914
===== PropertyEditors
912
915
913
916
For Spring versions before 3.0 simple Java `PropertyEditors` had to be used. To integrate with that, Spring Data offers a `DomainClassPropertyEditorRegistrar`, which looks up all Spring Data repositories registered in the `ApplicationContext` and registers a custom `PropertyEditor` for the managed domain class.
@@ -942,144 +945,3 @@ public class UserController {
942
945
}
943
946
----
944
947
945
-
ConversionServiceIn Spring 3.0 and later the `PropertyEditor` support is superseded by a new conversion infrastructure that eliminates the drawbacks of `PropertyEditors` and uses a stateless X to Y conversion approach. Spring Data now ships with a `DomainClassConverter` that mimics the behavior of `DomainClassPropertyEditorRegistrar`. To configure, simply declare a bean instance and pipe the `ConversionService` being used into its constructor:
If you are using JavaConfig, you can simply extend Spring MVC's `WebMvcConfigurationSupport` and hand the `FormatingConversionService` that the configuration superclass provides into the `DomainClassConverter` instance you create.
957
-
958
-
[source, java]
959
-
----
960
-
class WebConfiguration extends WebMvcConfigurationSupport {
961
-
962
-
// Other configuration omitted
963
-
964
-
@Bean
965
-
public DomainClassConverter<?> domainClassConverter() {
966
-
return new DomainClassConverter<FormattingConversionService>(mvcConversionService());
967
-
}
968
-
}
969
-
----
970
-
971
-
[[web-pagination]]
972
-
==== Web pagination
973
-
974
-
When working with pagination in the web layer you usually have to write a lot of boilerplate code yourself to extract the necessary metadata from the request. The less desirable approach shown in the example below requires the method to contain an `HttpServletRequest` parameter that has to be parsed manually. This example also omits appropriate failure handling, which would make the code even more verbose.
975
-
976
-
[source, java]
977
-
----
978
-
@Controller
979
-
@RequestMapping("/users")
980
-
public class UserController {
981
-
982
-
// DI code omitted
983
-
984
-
@RequestMapping
985
-
public String showUsers(Model model, HttpServletRequest request) {
986
-
987
-
int page = Integer.parseInt(request.getParameter("page"));
988
-
int pageSize = Integer.parseInt(request.getParameter("pageSize"));
989
-
990
-
Pageable pageable = new PageRequest(page, pageSize);
The bottom line is that the controller should not have to handle the functionality of extracting pagination information from the request. So Spring Data ships with a `PageableHandlerMethodArgumentResolver` that will do the work for you. The Spring MVC JavaConfig support exposes a `WebMvcConfigurationSupport` helper class to customize the configuration as follows:
999
-
1000
-
[source, java]
1001
-
----
1002
-
@Configuration
1003
-
public class WebConfig extends WebMvcConfigurationSupport {
The `PageableArgumentResolver` automatically resolves request parameters to build a `PageRequest` instance. By default it expects the following structure for the request parameters.
1043
-
1044
-
.Request parameters evaluated by PageableHandlerMethodArgumentResolver
1045
-
[options = "autowidth"]
1046
-
|===============
1047
-
|`page`|Page you want to retrieve, 0 indexed and defaults to 0.
1048
-
|`size`|Size of the page you want to retrieve, defaults to 20.
1049
-
|`sort`|A collection of sort directives in the format `($propertyname,)[asc\|desc]?`.
1050
-
|===============
1051
-
1052
-
.Pagination URL parameter examples
1053
-
1054
-
To retrieve the third page with a maximum page size of 100 with the data sorted by the email property in ascending order use the following url parameter:
1055
-
====
1056
-
----
1057
-
?page=2&size=100&sort=email,asc
1058
-
----
1059
-
====
1060
-
To sort the data by multiple properties in different sort order use the following URL parameter:
1061
-
====
1062
-
----
1063
-
?sort=foo,asc&sort=bar,desc
1064
-
----
1065
-
====
1066
-
1067
-
In case you need multiple `Pageable` instances to be resolved from the request (for multiple tables, for example) you can use Spring's `@Qualifier` annotation to distinguish one from another. The request parameters then have to be prefixed with `${qualifier}_`. So for a method signature like this:
1068
-
1069
-
[source, java]
1070
-
----
1071
-
public String showUsers(Model model,
1072
-
@Qualifier("foo") Pageable first,
1073
-
@Qualifier("bar") Pageable second) { … }
1074
-
----
1075
-
1076
-
you have to populate `foo_page` and `bar_page` and the related subproperties.
1077
-
1078
-
Configuring a global default on bean declaration the `PageableArgumentResolver` will use a `PageRequest` with the first page and a page size of 10 by default. It will use that value if it cannot resolve a `PageRequest` from the request (because of missing parameters, for example). You can configure a global default on the bean declaration directly. If you might need controller method specific defaults for the `Pageable`, annotate the method parameter with `@PageableDefaults` and specify page (through `pageNumber`), page size (through `value`), `sort` (list of properties to sort by), and `sortDir` (the direction to sort by) as annotation attributes:
0 commit comments