1

尝试使用 Thymeleaf 和 Dandelion 对数据表进行分页。根据文档,我需要更新一些内容:

web.xml(javaconfig 尝试进一步向下)

<!-- Dandelion filter definition and mapping -->
<filter>
   <filter-name>dandelionFilter</filter-name>
   <filter-class>com.github.dandelion.core.web.DandelionFilter</filter-class>
</filter>
<filter-mapping>
   <filter-name>dandelionFilter</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Dandelion servlet definition and mapping -->
<servlet>
   <servlet-name>dandelionServlet</servlet-name>
   <servlet-class>com.github.dandelion.core.web.DandelionServlet</servlet-class>
</servlet>
<servlet-mapping>
   <servlet-name>dandelionServlet</servlet-name>
   <url-pattern>/dandelion-assets/*</url-pattern>
</servlet-mapping>

SpringTemplateEngine @Bean(因为我已经有了 Thymeleaf 模板引擎,所以跳过了)

<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
   <property name="templateResolver" ref="templateResolver" />
   <property name="additionalDialects">
      <set>
         <bean class="com.github.dandelion.datatables.thymeleaf.dialect.DataTablesDialect" />
      </set>
   </property>
</bean>

我对 Spring 的了解仍然非常不稳定,但我必须替换 web.xml 组件(至少我认为我可以这样做):

public class Initializer extends
        AbstractAnnotationConfigDispatcherServletInitializer...

    @Override
    protected Class<?>[] getServletConfigClasses() {
        logger.debug("Entering getServletConfigClasses()");
        return new Class<?>[] { ThymeleafConfig.class, WebAppConfig.class, DandelionServlet.class };
    } 

    @Override
    protected Filter[] getServletFilters() {
        return new Filter[] { new DandelionFilter(),
                new DelegatingFilterProxy("springSecurityFilterChain") };
    }

我的百里香配置:

@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
templateEngine.addDialect(dataTablesDialect());
return templateEngine;
}

@Bean
public ThymeleafViewResolver thymeleafViewResolver() {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine());
return resolver;
}

@Bean
public DataTablesDialect dataTablesDialect() {
return new DataTablesDialect();
}

我的安全设置:

.antMatchers("/dandelion-assets/**").permitAll()
.antMatchers("/datatablesController/**").permitAll()

加载两种方言后,我在日志中得到以下信息:

[THYMELEAF] TEMPLATE ENGINE CONFIGURED OK
INFO org.thymeleaf.TemplateEngine - [THYMELEAF] TEMPLATE ENGINE INITIALIZED

页面加载时:

DEBUG com.github.dandelion.datatables.core.configuration.StandardConfigurationLoader - No custom configuration. Using default one.
DEBUG com.github.dandelion.datatables.core.configuration.StandardConfigurationLoader - Resolving groups for the locale en_US...
DEBUG com.github.dandelion.datatables.core.configuration.StandardConfigurationLoader - 1 groups declared [global].
DEBUG com.github.dandelion.datatables.core.configuration.StandardConfigurationLoader - Resolving configurations for the locale en_US...
DEBUG com.github.dandelion.datatables.core.configuration.StandardConfigurationLoader - Group 'global' initialized with 0 properties
DEBUG com.github.dandelion.datatables.core.configuration.StandardConfigurationLoader - 1 group(s) resolved [global] for the locale en_US
DEBUG com.github.dandelion.datatables.thymeleaf.processor.el.TableFinalizerElProcessor - No configuration to apply, i.e. no 'dt:conf' has been found in the current template.
DEBUG com.github.dandelion.datatables.core.generator.configuration.DatatablesGenerator - Generating DataTables configuration ..
DEBUG com.github.dandelion.datatables.core.generator.configuration.DatatablesGenerator - DataTables configuration generated
DEBUG com.github.dandelion.datatables.core.generator.WebResourceGenerator - Loading extensions...
DEBUG com.github.dandelion.datatables.core.extension.ExtensionLoader - Scanning built-in extensions...
DEBUG com.github.dandelion.datatables.core.generator.WebResourceGenerator - Transforming configuration to JSON...
DEBUG com.github.dandelion.datatables.thymeleaf.processor.el.TableFinalizerElProcessor - Web content generated successfully
DEBUG com.github.dandelion.datatables.core.configuration.DatatablesConfigurator - Initializing the Javascript generator...

我得到的唯一警告:

WARN com.github.dandelion.core.asset.AssetMapper - No location found for delegate on AssetStorageUnit [name=dandelion-datatables, version=0.10.0, type=js, dom=null, locations={delegate=dandelion-datatables.js}, attributes=null, attributesOnlyName=[]]
DEBUG com.github.dandelion.core.asset.cache.AssetCacheManager - Retrieving asset with the key 6c075191955bbb1ecbd703380e648817806cf15b/dandelion-datatables-0.10.0.js
DEBUG com.github.dandelion.core.asset.cache.AssetCacheManager - Storing asset under the key 6c075191955bbb1ecbd703380e648817806cf15b/dandelion-datatables-0.10.0.js
WARN com.github.dandelion.core.asset.AssetMapper - No location found for delegate on AssetStorageUnit [name=dandelion-datatables, version=0.10.0, type=js, dom=null, locations={delegate=dandelion-datatables.js}, attributes=null, attributesOnlyName=[]]
DEBUG com.github.dandelion.core.asset.cache.AssetCacheManager - Retrieving asset with the key 6c075191955bbb1ecbd703380e648817806cf15b/dandelion-datatables-0.10.0.js
DEBUG com.github.dandelion.core.asset.cache.AssetCacheManager - Storing asset under the key 6c075191955bbb1ecbd703380e648817806cf15b/dandelion-datatables-0.10.0.js

最后:

<table class="table table-striped table-bordered table-hover"
                            dt:table="true" id="myTable" dt:pagination="true">

页面加载时,数据加载但不分页;我哪里错了?

4

1 回答 1

7

只是试图设置这个。以下是我的工作配置。

WebConfig.java

@Configuration
@ComponentScan(basePackages = { "com.github.dandelion.datatables.web" })
@EnableWebMvc
@Import({ ThymeleafConfig.class })
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}

这是导入的ThymeleafConfig.java类:

@Configuration
public class ThymeleafConfig {

    @Bean
    public ServletContextTemplateResolver templateResolver() {
        ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".html");
        resolver.setTemplateMode("HTML5");
        resolver.setCacheable(false);
        return resolver;
    }

    @Bean
    public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine engine = new SpringTemplateEngine();
        engine.setTemplateResolver(templateResolver());
        engine.addDialect(new DandelionDialect());
        engine.addDialect(new DataTablesDialect());
        return engine;
    }

    @Bean
    public ThymeleafViewResolver thymeleafViewResolver() {
        ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setTemplateEngine(templateEngine());
        return resolver;
    }
}

然后是根配置。如您所见,非常多毛。

RootConfig.java

@Configuration
@ComponentScan(basePackages = { "com.github.dandelion.datatables.service", "com.github.dandelion.datatables.repository" })
public class RootConfig {
}

最后是应用初始化程序。确保已正确配置蒲公英组件(DandelionFilterDandelionServlet)。

ApplicationInitializer.java

public class ApplicationInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {

        // Register the Root application context
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(RootConfig.class);

        // Register the Web application context
        AnnotationConfigWebApplicationContext mvcContext = new AnnotationConfigWebApplicationContext();
        mvcContext.register(WebConfig.class);

        // Context loader listener
        servletContext.addListener(new ContextLoaderListener(rootContext));

        // Register the Dandelion filter
        FilterRegistration.Dynamic dandelionFilter = servletContext.addFilter("dandelionFilter", new DandelionFilter());
        dandelionFilter.addMappingForUrlPatterns(null, false, "/*");

        // Register the Spring dispatcher servlet
        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("springServlet", new DispatcherServlet(mvcContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");

        // Register the Dandelion servlet
        ServletRegistration.Dynamic dandelionServlet = servletContext.addServlet("dandelionServlet", new DandelionServlet());
        dandelionServlet.setLoadOnStartup(2);
        dandelionServlet.addMapping("/dandelion-assets/*");
    }
}

您可以在此处查看完整的示例应用程序。

希望这可以帮助!

(StackOverflow 要求的免责声明:我是蒲公英的作者)

于 2014-06-02T13:12:58.323 回答