From 834438773878ea0a866bd39fd5c38fed926ea605 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 17 Sep 2012 16:26:25 +0200 Subject: [PATCH] DATAJPA-251 - Updated reference documentation. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Improved code samples for PageableArgumentResolver. Corrected registration info for DomainClassConverter and added JavaConfig example. Replace all obsolete readAll(…) method samples with findAll(…). Added hint to @PageableDefaults now being aware of sort properties and direction. --- src/docbkx/repositories.xml | 59 ++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/src/docbkx/repositories.xml b/src/docbkx/repositories.xml index ca1eb32c6..d20b1ac6f 100644 --- a/src/docbkx/repositories.xml +++ b/src/docbkx/repositories.xml @@ -839,7 +839,9 @@ public class UserController { private final UserRepository userRepository; + @Autowired public UserController(UserRepository userRepository) { + Assert.notNull(repository, "Repository must not be null!"); userRepository = userRepository; } @@ -849,7 +851,8 @@ public class UserController { // Do null check for id User user = userRepository.findOne(id); // Do null check for user - // Populate model + + model.addAttribute("user", user); return "user"; } } @@ -894,8 +897,7 @@ public class UserController { @RequestMapping("/{id}") public String showUserForm(@PathVariable("id") User user, Model model) { - // Do null check for user - // Populate model + model.addAttribute("user", user); return "userForm"; } } @@ -911,23 +913,33 @@ public class UserController { stateless X to Y conversion approach. We now ship with a DomainClassConverter that pretty much mimics the behaviour of - DomainClassPropertyEditorRegistrar. To register - the converter you have to declare - ConversionServiceFactoryBean, register the - converter and tell the Spring MVC namespace to use the configured - conversion service: + DomainClassPropertyEditorRegistrar. To + configure, simply declare a bean instance and pipe the + ConversionService being used into it's + constructor: <mvc:annotation-driven conversion-service="conversionService" /> -<bean id="conversionService" class="….context.support.ConversionServiceFactoryBean"> - <property name="converters"> - <list> - <bean class="org.springframework.data.repository.support.DomainClassConverter"> - <constructor-arg ref="conversionService" /> - </bean> - </list> - </property> +<bean class="org.springframework.data.repository.support.DomainClassConverter"> + <constructor-arg ref="conversionService" /> </bean> + + If you're using JavaConfig you can simply extend + WebMvcConfigurationSupport and hand the + FormatingConversionService the configuration + superclass provides into the + DomainClassConverter instance you + create. + + class WebConfiguration extends WebMvcConfigurationSupport { + + // Other configuration omitted + + @Bean + public DomainClassConverter<?> domainClassConverter() { + return new DomainClassConverter<FormattingConversionService>(mvcConversionService()); + } +} @@ -945,6 +957,9 @@ public class UserController { int page = Integer.parseInt(request.getParameter("page")); int pageSize = Integer.parseInt(request.getParameter("pageSize")); + + Pageable pageable = new PageRequest(page, pageSize); + model.addAttribute("users", userService.getUsers(pageable)); return "users"; } @@ -977,7 +992,7 @@ public class UserController { @RequestMapping public String showUsers(Model model, Pageable pageable) { - model.addAttribute("users", userDao.readAll(pageable)); + model.addAttribute("users", userRepository.findAll(pageable)); return "users"; } } @@ -1050,12 +1065,14 @@ public class UserController { bean declaration directly. In case you might need controller method specific defaults for the Pageable simply annotate the method parameter with - @PageableDefaults and specify page and - page size as annotation attributes: + @PageableDefaults and specify page + (through pageNumber), page size (through + value) as well as sort (the list of + properties to sort by) as wel as sortDir (the direction + to sort by) as annotation attributes: public String showUsers(Model model, - @PageableDefaults(pageNumber = 0, value = 30) Pageable pageable) { … } - + @PageableDefaults(pageNumber = 0, value = 30) Pageable pageable) { … }