<legend id='IJKJW'><style id='IJKJW'><dir id='IJKJW'><q id='IJKJW'></q></dir></style></legend>
    <tfoot id='IJKJW'></tfoot>
    • <bdo id='IJKJW'></bdo><ul id='IJKJW'></ul>

    <small id='IJKJW'></small><noframes id='IJKJW'>

      <i id='IJKJW'><tr id='IJKJW'><dt id='IJKJW'><q id='IJKJW'><span id='IJKJW'><b id='IJKJW'><form id='IJKJW'><ins id='IJKJW'></ins><ul id='IJKJW'></ul><sub id='IJKJW'></sub></form><legend id='IJKJW'></legend><bdo id='IJKJW'><pre id='IJKJW'><center id='IJKJW'></center></pre></bdo></b><th id='IJKJW'></th></span></q></dt></tr></i><div id='IJKJW'><tfoot id='IJKJW'></tfoot><dl id='IJKJW'><fieldset id='IJKJW'></fieldset></dl></div>

      Spring Data 不处理 Pageable 动作参数创建

      时间:2024-08-24
      <tfoot id='LwcSH'></tfoot>

      <small id='LwcSH'></small><noframes id='LwcSH'>

    1. <legend id='LwcSH'><style id='LwcSH'><dir id='LwcSH'><q id='LwcSH'></q></dir></style></legend>
      <i id='LwcSH'><tr id='LwcSH'><dt id='LwcSH'><q id='LwcSH'><span id='LwcSH'><b id='LwcSH'><form id='LwcSH'><ins id='LwcSH'></ins><ul id='LwcSH'></ul><sub id='LwcSH'></sub></form><legend id='LwcSH'></legend><bdo id='LwcSH'><pre id='LwcSH'><center id='LwcSH'></center></pre></bdo></b><th id='LwcSH'></th></span></q></dt></tr></i><div id='LwcSH'><tfoot id='LwcSH'></tfoot><dl id='LwcSH'><fieldset id='LwcSH'></fieldset></dl></div>

              <tbody id='LwcSH'></tbody>
              <bdo id='LwcSH'></bdo><ul id='LwcSH'></ul>
              1. 本文介绍了Spring Data 不处理 Pageable 动作参数创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个简单的控制器动作:

                I have a simple controller action:

                public class CategoriesController
                {
                    @RequestMapping(value = { "/", "" })
                    public String list(
                        Model model,
                        @PageableDefault(size = CategoriesController.PAGE_LIMIT) Pageable pager
                    )
                    {
                        // load page data
                        Page<Category> page = this.categoryService.findAll(pager);
                
                        /* action logic here */
                    }
                }
                

                这是我的 pom.xml 片段:

                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-beans</artifactId>
                        <version>3.2.4.RELEASE</version>
                    </dependency>
                
                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-web</artifactId>
                        <version>3.2.4.RELEASE</version>
                    </dependency>
                
                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-webmvc</artifactId>
                        <version>3.2.4.RELEASE</version>
                    </dependency>
                
                    <dependency>
                        <groupId>org.springframework.data</groupId>
                        <artifactId>spring-data-commons</artifactId>
                        <version>1.6.4.RELEASE</version>
                    </dependency>
                
                    <dependency>
                        <groupId>org.springframework.data</groupId>
                        <artifactId>spring-data-jpa</artifactId>
                        <version>1.5.0.RELEASE</version>
                    </dependency>
                

                将此添加到我的 applicationContext.xml 后:

                After addtion this to my applicationContext.xml:

                <bean class="org.springframework.data.web.config.SpringDataWebConfiguration"/>
                

                我有以下错误:

                org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.data.domain.Pageable]: Specified class is an interface
                

                Spring Data 本身运行良好,JPA 存储库运行良好.但到目前为止,我在控制器中使用手写分页(我自己计算页面,手动创建 PageRequest 对象).我想使用 Spring Data 网络附加功能,但由于某种原因它们对我不起作用......手动注册过时的 org.springframework.data.web.PageableArgumentResolver 使其部分工作,但不完全,但我认为这甚至不应该是一个解决方案.

                Spring Data itself works fine, JPA repositories are working. But until now I had hand-written pagination in controllers (calculating pages on my own, creating PageRequest objects by hand). I wanted to make use of Spring Data web extras, but they don't work for me for some reason... registering obsolete org.springframework.data.web.PageableArgumentResolver by hand partially made it working, but not completely, but still, I don't think this should even be a solution.

                org.springframework 上启用调试记录器后,我看到:

                After enabling debug logger on org.springframework I see that:

                01:37:33.850 [localhost-startStop-1] DEBUG org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader - Registering bean definition for @Bean method org.springframework.data.web.config.SpringDataWebConfiguration.pageableResolver()
                

                所以它已注册 - 知道为什么它不起作用吗?

                So it's registered - any idea why it's not working?

                推荐答案

                您的问题是您试图混合使用 XML 配置和基于 Java Config 的配置.在这种特殊情况下,这是行不通的.配置类中的 bean 将被实例化,但就是这样,它们没有注册到您的 <mvc:annotation-driven/> 配置中.

                Your problem is that you are trying to mix XML configuration and Java Config based configuration. In this particular case that isn't going to work. The bean in the configuration class will be instantiated but that is it, they aren't registered to your <mvc:annotation-driven /> configuration.

                您必须手动将 bean 添加到 ConversionService 和您的 RequestMappingHandlerMapping.无论是我们的开关,至少你的 DispatcherServlet 配置到 Java Config.

                You will have to add the beans manually to the ConversionService and your RequestMappingHandlerMapping. Either that our switch, at least your DispatcherServlet configuration to Java Config.

                在 XML 中,您可以使用 <mvc:argument-resolvers/> 标记配置其他参数解析器.(这模仿了 SpringDataWebConfiguration 中的配置).

                In XML you can configure additional argument-resolvers by using the <mvc:argument-resolvers /> tag. (This mimics the configuration from the SpringDataWebConfiguration).

                <mvc:annotation-driven>
                    <mvc:argument-resolvers>
                        <ref bean="sortResolver"/>
                        <ref bean="pageableResolver" />
                    </mvc:argument-resolvers>
                </mvc:annotation-driven>
                
                <bean id="sortResolver" class="org.springframework.data.web.SortHandlerMethodArgumentResolver" />
                <bean id="pageableResolver" class="org.springframework.data.web.PageableHandlerMethodArgumentResolver">
                    <constructor-arg ref="sortResolver" />
                </bean>
                

                但是 SpringDataWebConfiguration 做的不仅仅是这两个解析器,它还注册了一个 DomainClassConverter.如果你也想使用它,你需要一些额外的配置.

                However the SpringDataWebConfiguration does more then only these 2 resolvers it also registers a DomainClassConverter. If you also want to use that you need some additional configuration.

                <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" />
                
                <bean class="org.springframework.data.repository.support.DomainClassConverter">
                   <constructor-arg ref="conversionService" />
                </bean>
                
                <mvc:annotation-driven conversion-service="conversionService">
                    <mvc:argument-resolvers>
                        <ref bean="sortResolver"/>
                        <ref bean="pageableResolver" />
                    </mvc:argument-resolvers>
                </mvc:annotation-driven>
                
                <bean id="sortResolver" class="org.springframework.data.web.SortHandlerMethodArgumentResolver" />
                <bean id="pageableResolver" class="org.springframework.data.web.PageableHandlerMethodArgumentResolver">
                    <constructor-arg ref="sortResolver" />
                </bean>
                

                这篇关于Spring Data 不处理 Pageable 动作参数创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:通过 Spring Config 扫描 Spring Data 存储库? 下一篇:如何在 Spring Data REST 中公开 @EmbeddedId 转换器

                相关文章

                  <bdo id='qWKCH'></bdo><ul id='qWKCH'></ul>
              2. <small id='qWKCH'></small><noframes id='qWKCH'>

                  <tfoot id='qWKCH'></tfoot>

                  <i id='qWKCH'><tr id='qWKCH'><dt id='qWKCH'><q id='qWKCH'><span id='qWKCH'><b id='qWKCH'><form id='qWKCH'><ins id='qWKCH'></ins><ul id='qWKCH'></ul><sub id='qWKCH'></sub></form><legend id='qWKCH'></legend><bdo id='qWKCH'><pre id='qWKCH'><center id='qWKCH'></center></pre></bdo></b><th id='qWKCH'></th></span></q></dt></tr></i><div id='qWKCH'><tfoot id='qWKCH'></tfoot><dl id='qWKCH'><fieldset id='qWKCH'></fieldset></dl></div>
                  <legend id='qWKCH'><style id='qWKCH'><dir id='qWKCH'><q id='qWKCH'></q></dir></style></legend>