Spring MVC 4 + Apache Tiles 2 + Thymeleaf 3的集成错误
问题描述:
我的团队一直在使用JSP开发几个月的项目。最近,我们意识到Thymeleaf的实施可能会方便我们的网页设计师,因此我们开始着手整合它。以下是我们的Web应用程序的配置。Spring MVC 4 + Apache Tiles 2 + Thymeleaf 3的集成错误
import java.util.LinkedHashSet;
import java.util.Set;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import org.springframework.web.servlet.view.tiles2.TilesConfigurer;
import org.thymeleaf.dialect.IDialect;
import org.thymeleaf.extras.tiles2.dialect.TilesDialect;
import org.thymeleaf.extras.tiles2.spring4.web.configurer.ThymeleafTilesConfigurer;
import org.thymeleaf.extras.tiles2.spring4.web.view.ThymeleafTilesView;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ITemplateResolver;
import com.mycompany.web.MultilanguageMessageSource;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.mycompany")
public class ViewConfig extends WebMvcConfigurerAdapter {
/**
* auto redirection for trailing slash
*/
@Bean
public RequestMappingHandlerMapping requestMappingHandlerMapping(){
RequestMappingHandlerMapping mapping = new RequestMappingHandlerMapping();
mapping.setUseTrailingSlashMatch(true);
return mapping;
}
// view resolvers
@Bean
public ITemplateResolver templateResolver(){
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setTemplateMode("LEGACYHTML5");
resolver.setCharacterEncoding("UTF-8");
resolver.setSuffix(".html");
resolver.setCacheable(false);
resolver.setOrder(0);
return resolver;
}
@Bean
public ViewResolver viewResolver(){
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setViewClass(ThymeleafTilesView.class);
resolver.setTemplateEngine(templateEngine());
resolver.setOrder(1);
return resolver;
}
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
Set<IDialect> dialects = new LinkedHashSet<IDialect>();
dialects.add(new TilesDialect());
templateEngine.setTemplateResolver(templateResolver());
templateEngine.setAdditionalDialects(dialects);
return templateEngine;
}
@Bean
public TilesConfigurer tilesConfigurer() {
ThymeleafTilesConfigurer configurer = new ThymeleafTilesConfigurer();
configurer.setDefinitions("/WEB-INF/tiles/tiles-definitions.xml");
// configurer.setCheckRefresh(true);
return configurer;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/");
}
@Bean
// for Spring general localization
public MessageSource messageSource() {
return new MultilanguageMessageSource();
}
}
这样的配置不能在所有的工作,让在Eclipse控制台下面的错误输出:
03:28:29 DEBUG filter jar: slf4j-api-1.7.12.jar -> false
03:28:29 DEBUG filter jar: logback-core-1.1.3.jar -> false
Oct 18, 2017 3:28:29 AM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Oct 18, 2017 3:28:29 AM org.apache.catalina.core.ApplicationContext log
INFO: Spring WebApplicationInitializers detected on classpath: [[email protected]]
Oct 18, 2017 3:28:30 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
Oct 18, 2017 3:28:30 AM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
java.lang.NoSuchMethodError: org.slf4j.spi.LocationAwareLogger.log(Lorg/slf4j/Marker;Ljava/lang/String;ILjava/lang/String;Ljava/lang/Throwable;)V
at org.apache.commons.logging.impl.SLF4JLocationAwareLog.info(SLF4JLocationAwareLog.java:159)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:285)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4851)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5314)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
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)
Oct 18, 2017 3:28:30 AM org.apache.catalina.core.StandardContext startInternal
SEVERE: One or more listeners failed to start. Full details will be found in the appropriate container log file
Oct 18, 2017 3:28:30 AM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [/WEB] startup failed due to previous errors
Oct 18, 2017 3:28:30 AM org.apache.catalina.core.ApplicationContext log
INFO: Closing Spring root WebApplicationContext
Oct 18, 2017 3:28:30 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8081"]
03:28:30 INFO Tomcat 8.0.44 started and listening on port 8081
03:28:30 INFO WEB runs at:
03:28:30 INFO http://localhost:8081/WEB
03:28:30 DEBUG Tomcat 8.0.44 started.
Run 'gradle appStop' to stop the server.
以下是我的build.gradle的摘录:
compile group: 'org.thymeleaf', name: 'thymeleaf', version: '3.0.8.RELEASE'
compile group: 'org.thymeleaf', name: 'thymeleaf-spring4', version: '3.0.8.RELEASE'
compile group: 'org.thymeleaf.extras', name: 'thymeleaf-extras-tiles2', version: '2.1.1.RELEASE'
compile group: 'org.thymeleaf.extras', name: 'thymeleaf-extras-tiles2-spring4', version: '2.1.1.RELEASE'
compile group: 'org.apache.tiles', name: 'tiles-core', version: '2.2.2'
compile group: 'org.apache.tiles', name: 'tiles-jsp', version: '2.2.2'
compile group: 'org.apache.tiles', name: 'tiles-api', version: '2.2.2'
compile group: 'org.apache.tiles', name: 'tiles-servlet', version: '2.2.2'
答
它(java的.lang.NoSuchMethodError:org.slf4j.spi.LocationAwareLogger.log)看起来像slf4j记录器错误。由于classpath中有两个不同版本的slf4j-api jar,我曾经遇到类似的错误。一次检查你的slf4j依赖关系。