Thymeleaf布局方言 - Java Config

时间:2015-12-12 14:37:36

标签: java spring thymeleaf

我将Thymeleaf从2.1版升级到3.0 BETA-1。新的Thymeleaf需要java配置,所以我将我的xml配置重写为java。一切正常,期待布局方言。我不明白为什么。

@Configuration
@EnableWebMvc
@ComponentScan
public class ThymeleafConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }


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

    @Bean(name = "templateViewResolver")
    public ViewResolver viewResolver() {
        ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setTemplateEngine(templateEngine());
        resolver.setCharacterEncoding("UTF-8");
        resolver.setContentType("text/html; charset=UTF-8");
        return resolver;
    }

    @Bean(name = "templateEngine")
    public TemplateEngine templateEngine() {
        TemplateEngine engine = new TemplateEngine();
        engine.setTemplateResolver(templateResolver());
        engine.addDialect(new SpringSecurityDialect());
        engine.addDialect(new LayoutDialect()); //On this line I get Exception
        return engine;
    }

    @Bean(name = "templateResolver")
    public ITemplateResolver templateResolver() {
        SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
        resolver.setApplicationContext(applicationContext);
        resolver.setPrefix("/views/");
        resolver.setSuffix(".html");
        resolver.setTemplateMode("HTML5");
        resolver.setCharacterEncoding("UTF-8");
        resolver.setCacheable(false);
        return resolver;
    }

    @Bean(name = "thymeleafViewResolver")
    public AjaxThymeleafViewResolver thymeleafViewResolver() {
        AjaxThymeleafViewResolver thymeleafViewResolver = new AjaxThymeleafViewResolver(); // web flow ajax
        thymeleafViewResolver.setViewClass(FlowAjaxThymeleafView.class); // web flow + thymeleaf ajax
        thymeleafViewResolver.setTemplateEngine(templateEngine());
        thymeleafViewResolver.setCharacterEncoding("UTF-8");
        return thymeleafViewResolver;
    }
}

(我知道在这种情况下这个bean的名字是没用的) 我也使用Spring Web Flow

<bean class="org.springframework.webflow.mvc.builder.MvcViewFactoryCreator"
      id="mvcViewFactoryCreator">
    <property name="viewResolvers" ref="thymeleafViewResolver"/>
</bean>

所以我将百里叶视图解析器设置为此。

我的堆栈跟踪

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'templateViewResolver' defined in pl.onedayproject.oneday.webapp.config.ThymeleafConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.ViewResolver]: Factory method 'viewResolver' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'templateEngine' defined in pl.onedayproject.oneday.webapp.config.ThymeleafConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.thymeleaf.TemplateEngine]: Factory method 'templateEngine' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'layoutDialect' defined in pl.onedayproject.oneday.webapp.config.ThymeleafConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [nz.net.ultraq.thymeleaf.LayoutDialect]: Factory method 'layoutDialect' threw exception; nested exception is java.lang.NoClassDefFoundError: org/thymeleaf/dom/Attribute
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:835)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.ViewResolver]: Factory method 'viewResolver' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'templateEngine' defined in pl.onedayproject.oneday.webapp.config.ThymeleafConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.thymeleaf.TemplateEngine]: Factory method 'templateEngine' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'layoutDialect' defined in pl.onedayproject.oneday.webapp.config.ThymeleafConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [nz.net.ultraq.thymeleaf.LayoutDialect]: Factory method 'layoutDialect' threw exception; nested exception is java.lang.NoClassDefFoundError: org/thymeleaf/dom/Attribute
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
    ... 23 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'templateEngine' defined in pl.onedayproject.oneday.webapp.config.ThymeleafConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.thymeleaf.TemplateEngine]: Factory method 'templateEngine' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'layoutDialect' defined in pl.onedayproject.oneday.webapp.config.ThymeleafConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [nz.net.ultraq.thymeleaf.LayoutDialect]: Factory method 'layoutDialect' threw exception; nested exception is java.lang.NoClassDefFoundError: org/thymeleaf/dom/Attribute
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331)
    at pl.onedayproject.oneday.webapp.config.ThymeleafConfig$$EnhancerBySpringCGLIB$$8dbcdad3.templateEngine(<generated>)
    at pl.onedayproject.oneday.webapp.config.ThymeleafConfig.viewResolver(ThymeleafConfig.java:38)
    at pl.onedayproject.oneday.webapp.config.ThymeleafConfig$$EnhancerBySpringCGLIB$$8dbcdad3.CGLIB$viewResolver$0(<generated>)
    at pl.onedayproject.oneday.webapp.config.ThymeleafConfig$$EnhancerBySpringCGLIB$$8dbcdad3$$FastClassBySpringCGLIB$$5bcedadb.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:318)
    at pl.onedayproject.oneday.webapp.config.ThymeleafConfig$$EnhancerBySpringCGLIB$$8dbcdad3.viewResolver(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
    ... 24 more
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.thymeleaf.TemplateEngine]: Factory method 'templateEngine' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'layoutDialect' defined in pl.onedayproject.oneday.webapp.config.ThymeleafConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [nz.net.ultraq.thymeleaf.LayoutDialect]: Factory method 'layoutDialect' threw exception; nested exception is java.lang.NoClassDefFoundError: org/thymeleaf/dom/Attribute
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
    ... 45 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'layoutDialect' defined in pl.onedayproject.oneday.webapp.config.ThymeleafConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [nz.net.ultraq.thymeleaf.LayoutDialect]: Factory method 'layoutDialect' threw exception; nested exception is java.lang.NoClassDefFoundError: org/thymeleaf/dom/Attribute
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331)
    at pl.onedayproject.oneday.webapp.config.ThymeleafConfig$$EnhancerBySpringCGLIB$$8dbcdad3.layoutDialect(<generated>)
    at pl.onedayproject.oneday.webapp.config.ThymeleafConfig.templateEngine(ThymeleafConfig.java:48)
    at pl.onedayproject.oneday.webapp.config.ThymeleafConfig$$EnhancerBySpringCGLIB$$8dbcdad3.CGLIB$templateEngine$2(<generated>)
    at pl.onedayproject.oneday.webapp.config.ThymeleafConfig$$EnhancerBySpringCGLIB$$8dbcdad3$$FastClassBySpringCGLIB$$5bcedadb.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:318)
    at pl.onedayproject.oneday.webapp.config.ThymeleafConfig$$EnhancerBySpringCGLIB$$8dbcdad3.templateEngine(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
    ... 46 more
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [nz.net.ultraq.thymeleaf.LayoutDialect]: Factory method 'layoutDialect' threw exception; nested exception is java.lang.NoClassDefFoundError: org/thymeleaf/dom/Attribute
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
    ... 67 more
Caused by: java.lang.NoClassDefFoundError: org/thymeleaf/dom/Attribute
    at nz.net.ultraq.thymeleaf.LayoutDialect.<clinit>(LayoutDialect.groovy:49)
    at pl.onedayproject.oneday.webapp.config.ThymeleafConfig.layoutDialect(ThymeleafConfig.java:55)
    at pl.onedayproject.oneday.webapp.config.ThymeleafConfig$$EnhancerBySpringCGLIB$$8dbcdad3.CGLIB$layoutDialect$3(<generated>)
    at pl.onedayproject.oneday.webapp.config.ThymeleafConfig$$EnhancerBySpringCGLIB$$8dbcdad3$$FastClassBySpringCGLIB$$5bcedadb.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:318)
    at pl.onedayproject.oneday.webapp.config.ThymeleafConfig$$EnhancerBySpringCGLIB$$8dbcdad3.layoutDialect(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
    ... 68 more
Caused by: java.lang.ClassNotFoundException: org.thymeleaf.dom.Attribute
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547)
    ... 80 more

3 个答案:

答案 0 :(得分:1)

Thymeleaf 3尚未提供Thymeleaf布局方言

答案 1 :(得分:1)

将其添加到pom.xml:

<dependency>
    <groupId>nz.net.ultraq.thymeleaf</groupId>
    <artifactId>thymeleaf-layout-dialect</artifactId>
    <version>2.1.2</version>
</dependency>

答案 2 :(得分:0)

这个完全相同的错误是对我造成的,因为我忘记删除 thymeleaf-extras-springsecurity5 依赖项。从 pom.xml 中删除 spring security 依赖项后。