Thymeleaf:org.thymeleaf.exceptions.TemplateInputException

时间:2017-08-25 16:34:20

标签: java spring thymeleaf

我正在尝试使用基于spring mvc java的配置的thymeleaf 3.0.7.RELEASE

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.sagar")
public class MvcConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware{

    @Autowired
    RoleToUserProfileConverter roleToUserProfileConverter;

    private ApplicationContext applicationContext;

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

    @Bean
    public ViewResolver viewResolver() {
        ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setTemplateEngine(templateEngine());
        resolver.setCharacterEncoding("UTF-8");
        return resolver;
    }

    @Bean
    public TemplateEngine templateEngine() {
        SpringTemplateEngine engine = new SpringTemplateEngine();
        engine.setEnableSpringELCompiler(true);
        engine.setTemplateResolver(templateResolver());
        return engine;
    }

    private ITemplateResolver templateResolver() {
        SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
        resolver.setApplicationContext(applicationContext);
        resolver.setPrefix("/WEB-INF/templates/");
        resolver.setTemplateMode(TemplateMode.HTML);
        return resolver;
    }

    public void addResourceHandlers(ResourceHandlerRegistry registry){
        registry.addResourceHandler("/static/**").addResourceLocations("/static/");

    }
}
springsecurity配置类中的

方法。

 @Autowired
    public void configureGlobalSecurity(AuthenticationManagerBuilder auth)
     throws Exception{
        auth.userDetailsService(userDetailsService);
        auth.authenticationProvider(authenticationProvider());
    }

    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers( "/admin/list")
                .access("hasRole('USER') or hasRole('ADMIN') or hasRole('DBA')")
                .antMatchers("/newuser/**", "/delete-user-*").access("hasRole
                ('ADMIN')").antMatchers("/edit-user-*")
                .access("hasRole('ADMIN') or hasRole('DBA')").and().formLogin()
                .loginPage("/login")
                .loginProcessingUrl("/login").usernameParameter("ssoId").
                passwordParameter("password").and()
                .rememberMe().rememberMeParameter("remember-me").
                tokenRepository(tokenRepository)
                .tokenValiditySeconds(86400).and().csrf().and().exceptionHandling()
                .accessDeniedPage("/Access_Denied");
    }

和我的 home.html 文件

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

this is home page using thymeleaf

</body>
</html>

我的pom.xml依赖项包括

<dependency>
  <groupId>org.thymeleaf</groupId>
  <artifactId>thymeleaf-spring4</artifactId>
  <version>3.0.7.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->
<dependency>
  <groupId>org.thymeleaf</groupId>
  <artifactId>thymeleaf</artifactId>
  <version>3.0.7.RELEASE</version>
</dependency>

春季安全版本为4.2.3.RELEASE,春季版本为4.3.9.RELEASE

我得到的错误是: -

Message Request processing failed; nested exception is 
org.thymeleaf.exceptions.TemplateInputException: 
An error happened during template parsing 
(template: "ServletContext resource [/WEB-INF/templates/home]")


Root Cause

java.io.FileNotFoundException: Could not open ServletContext 
resource [/WEB-INF/templates/home]

Root Cause

org.thymeleaf.exceptions.TemplateInputException: 
An error happened during template parsing
 (template: "ServletContext resource [/WEB-INF/templates/home]")

1 个答案:

答案 0 :(得分:5)

需要修复templateResolver方法。

  1. 尝试设置后缀

    resolver.setSuffix(".html");
    
  2. 尝试设置正确的TemplateMode

    resolver.setTemplateMode("HTML5");
    
  3. 我认为这应该可以解决问题