Spring 启动:TilesDialect() 抛出异常

Spring boot: TilesDialect() throw exception

我使用 Spring 引导,我想使用磁贴,但我有一个问题:

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$a5dfe74.CGLIB$getTemplateEngine(<generated>) ~[classes/:na]
at com.scheduler.config.mvc.MvcConfiguration$$EnhancerBySpringCGLIB$a5dfe74$$FastClassBySpringCGLIB$c485b2.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$a5dfe74.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>

你能帮帮我吗? 谢谢!

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

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

我的解决方案:

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

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

2) 创建配置 class:

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

3) 覆盖 ModuleDialect class(对我来说: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 的构造函数,这是你的问题。之后你可以使用最新版本的 thymeleaf