Spring Java Config and Thymeleaf - Dandelion Datatables config

5.8k Views Asked by At

Attempting to get pagination on datatables using Thymeleaf and Dandelion. According to the docs I need to update a few things:

web.xml (javaconfig attempt further down)

<!-- 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 (skipped as I already have the Thymeleaf template Engine)

<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>

My knowledge of Spring is still extremely shaky but I have to replace the web.xml components (least I think I can do it this way):

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") };
    }

My ThymeleafConfig:

@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();
}

My security settings:

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

I get the following in my logs after both dialects load:

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

When the page loads:

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...

Only warning I get:

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

And finally:

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

When the page loads, the data loads but it isn't paginated; where'd I go wrong?

1

There are 1 best solutions below

2
On BEST ANSWER

Just tried to set this up. Here follows my working configuration.

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();
    }
}

Here is the imported ThymeleafConfig.java class:

@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;
    }
}

Then the root configuration. Quite hairy as you can see.

RootConfig.java

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

And finally, the app initializer. Make sure to have well configured the Dandelion components (DandelionFilter and DandelionServlet).

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/*");
    }
}

You can see the full sample application here.

Hope this helps!

(Disclaimer required by StackOverflow: I'm the author of Dandelion)