【问题标题】:Thymleaf Template not being used properly within spring boot在 Spring Boot 中未正确使用 Thymeleaf 模板
【发布时间】:2016-12-28 15:41:36
【问题描述】:

我无法正确设置我的 Spring Boot 应用程序。

基本上我有这个 pom.xml :

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.3.RELEASE</version>
    <relativePath />
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-hateoas</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-ldap</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

还有这个网络应用配置:

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/login").setViewName("security/login");
}

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

@Bean
public SpringTemplateEngine templateEngine() {
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();

    Set<IDialect> dialects = new HashSet<IDialect>();
    dialects.add(springSecurityDialect());

    templateEngine.setAdditionalDialects(dialects);
    templateEngine.setTemplateResolver(templateResolver());
    return templateEngine;
}

@Bean
public SpringSecurityDialect springSecurityDialect() {
    SpringSecurityDialect dialect = new SpringSecurityDialect();
    return dialect;
}

@Bean
public TemplateResolver templateResolver() {
    SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
    resolver.setPrefix("templates/");
    //resolver.setSuffix(".html");
    return resolver;
}
}

在资源方面,我有以下内容:

/src
--/main
----/resources
------/template
--------/home
----------/home.html
--------/security
----------/login.html

我也有这门课:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
JdbcTemplate jdbcTemplate;

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .authorizeRequests()
                .antMatchers("/", "/home").permitAll()
            .and()
                .formLogin().loginPage("/login").failureUrl("/login?error")
                .usernameParameter("username").passwordParameter("password")
            .and()
                .logout().logoutSuccessUrl("/login?logout")
            .and()
                .exceptionHandling().accessDeniedPage("/403")
            .and()
                .csrf();
}

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth
        .jdbcAuthentication().dataSource(jdbcTemplate.getDataSource())
            .usersByUsernameQuery("select username,password, enabled from users where username=?")
            .authoritiesByUsernameQuery("select username, role from user_roles where username=?");

    ;
}

}

无论如何,我无法修复或理解以下错误:

2016-08-19 11:40:58.707 ERROR 6874 --- [nio-8080-exec-1]         org.thymeleaf.TemplateEngine             : [THYMELEAF][http-nio-8080-exec-1]       Exception processing template "home/home": Error resolving template "home/home",  template might not exist or might not be accessible by any of the configured   Template Resolvers
2016-08-19 11:40:58.723 ERROR 6874 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[.[dispatcherServlet]      : Servlet.service() for servlet [dispatcherServlet] in context with path [/fidzit] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template "home/home", template might not exist or might not be accessible by any of the configured Template Resolvers] with root cause
org.thymeleaf.exceptions.TemplateInputException: Error resolving template "home/home", template might not exist or might not be accessible by any of the configured Template Resolvers
at org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:246) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1104) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1060) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1011) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at 
[...]

【问题讨论】:

    标签: spring-security spring-boot thymeleaf


    【解决方案1】:

    您需要为模板目录选择正确的名称:templatetemplates

    看这里:

    resolver.setPrefix("templates/");
    

    这里:

    /src
    --/main
    ----/resources
    ------/template
    

    【讨论】:

      猜你喜欢
      • 2016-01-26
      • 2019-10-31
      • 2019-04-11
      • 2020-03-14
      • 2017-03-08
      • 1970-01-01
      • 2017-10-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多