【问题标题】:Spring boot: TilesDialect() throw exceptionSpring boot:TilesDialect() 抛出异常
【发布时间】:2016-09-10 18:44:38
【问题描述】:

我用的是Spring boot,我想用tile,但是我有一个问题:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.thymeleaf.spring4.SpringTemplateEngine]: Factory method 'getTemplateEngine' threw exception; nested exception is java.lang.NoSuchMethodError: org.thymeleaf.dialect.AbstractDialect: method <init>()V not found
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
... 18 common frames omitted
Caused by: java.lang.NoSuchMethodError: org.thymeleaf.dialect.AbstractDialect: method <init>()V not found
at org.thymeleaf.extras.tiles2.dialect.TilesDialect.<init>(TilesDialect.java:46) ~[thymeleaf-extras-tiles2-2.1.1.RELEASE.jar:na]
at com.scheduler.config.mvc.MvcConfiguration.getTemplateEngine(MvcConfiguration.java:39) ~[classes/:na]
at com.scheduler.config.mvc.MvcConfiguration$$EnhancerBySpringCGLIB$$7a5dfe74.CGLIB$getTemplateEngine$1(<generated>) ~[classes/:na]
at com.scheduler.config.mvc.MvcConfiguration$$EnhancerBySpringCGLIB$$7a5dfe74$$FastClassBySpringCGLIB$$41c485b2.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:356) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at com.scheduler.config.mvc.MvcConfiguration$$EnhancerBySpringCGLIB$$7a5dfe74.getTemplateEngine(<generated>) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_80]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_80]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_80]
at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_80]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]

WebMvcConfigurerAdapter 我的部分:

@Bean(name = "templateResolver")
public SpringResourceTemplateResolver getTemplateResolver() {
    SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
    templateResolver.setPrefix("/WEB-INF/views/");
    templateResolver.setSuffix(".html");
    templateResolver.setTemplateMode("HTML");
    templateResolver.setCacheable(false);
    return templateResolver;
}

@Bean(name = "templateEngine")
public SpringTemplateEngine getTemplateEngine(SpringResourceTemplateResolver resolver) {
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.setTemplateResolver(resolver);
    templateEngine.addDialect(new SpringSecurityDialect());
    templateEngine.addDialect(new TilesDialect());
    return templateEngine;
}

@Bean(name = "viewResolver")
public ThymeleafViewResolver getViewResolver(SpringTemplateEngine templateEngine) {
    ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
    viewResolver.setTemplateEngine(templateEngine);
    viewResolver.setCharacterEncoding("UTF-8");
    viewResolver.setOrder(Ordered.HIGHEST_PRECEDENCE);
    viewResolver.setExcludedViewNames(new String[]{"*"});
    return viewResolver;
}

当下面这行被执行时,上面的异常被抛出:

templateEngine.addDialect(new TilesDialect());

pom.xml 依赖:

<dependencies>

    <!-- Spring Boot -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>


    <!-- Spring security -->
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-core</artifactId>
        <version>4.1.0.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>4.1.0.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>4.1.0.RELEASE</version>
    </dependency>


    <!-- Thymeleaf -->
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf-spring4</artifactId>
        <version>3.0.0.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf</artifactId>
        <version>3.0.0.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-java8time</artifactId>
        <version>2.1.0.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-tiles2-spring4</artifactId>
        <version>2.1.1.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity3</artifactId>
        <version>3.0.0.RELEASE</version>
    </dependency>

</dependencies>

你能帮帮我吗? 谢谢!

【问题讨论】:

    标签: maven spring-mvc thymeleaf tiles2


    【解决方案1】:

    我在 spring-social 和 thymeleaf 3.0.3 之间的集成遇到了一些问题。

    我的解决方案:

    1)从模块的自动配置中排除模块(在我的情况下是spring-social):

    @SpringBootApplication
    @EnableAutoConfiguration(exclude = {SocialWebAutoConfiguration.class})
    public class MyApplication { ...
    

    2) 创建配置类:

    @EnableSocial
    @Configuration
    public class SocialConfig implements SocialConfigurer {
    ...
    }
    

    3) 覆盖 ModuleDialect 类(对我来说:SpringSocialDialect.class):

    public class CustomSpringSocialDialect extends AbstractDialect  {
    
      public CustomSpringSocialDialect() {
        super("social");
      }
    
      public String getPrefix() {
        return "soc";
      }
    
      public Set<IProcessor> getProcessors() {
        return new SpringSocialDialect().getProcessors();
      }
    }
    

    4) 在 MvcConfig.class 中为 templateEngine 添加方言:

    @Bean
    public SpringTemplateEngine templateEngine(ITemplateResolver   templateResolver) {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver);
        templateEngine.addDialect(new SpringSecurityDialect());
        templateEngine.addDialect(new CustomSpringSocialDialect());
        templateEngine.addDialect(new Java8TimeDialect());
        templateEngine.setMessageSource(messageSource());
    
        return templateEngine;
    }
    

    我认为你的问题在这个方向。所以你的 TilesDialect 有没有 arg 的构造函数,这是你的问题。之后你可以使用最新版本的百里香

    【讨论】:

      【解决方案2】:

      这是因为全新的 Thymeleaf 3.0.0.RELEASE 还不支持 Tiles。您必须等待 thymeleaf-extras-tiles2-spring4 的更新,回到 Thymeleaf 2 或像我一样从 Tiles 辞职:P

      【讨论】:

      • 感谢您的帮助 :) 我已将 thymeleaf 设置为“2.1.2.RELEASE”并且它正在工作! :)
      • 有什么消息什么时候发布吗?这篇文章已经十个月了
      猜你喜欢
      • 2016-09-26
      • 2019-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-10
      • 2012-05-22
      • 2019-07-24
      • 2020-02-05
      相关资源
      最近更新 更多