spring boot - 如何正确定义模板位置?

时间:2016-09-01 03:43:26

标签: java spring spring-mvc spring-boot

在使用Spring应用程序(以及春季启动)之后挣扎了一段时间,似乎我终于要让它正常工作了。

我已经通过依赖解析和maven构建已经转移了。应用程序启动(并且很快!)但是当我尝试访问时

localhost:8080

每当我尝试访问应用程序的目标网页时,都会收到以下浏览器消息:

HTTP Status 500 - Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template "home/homeNotSignedIn", template might not exist or might not be accessible by any of the configured Template Resolvers

src/main/resources文件夹

src/main/resources
    static // CSS, IMG and JS
    templates // html
    application.properties
    log4j.properties

现在,我知道我可能会混合概念,但在ApplicationConfiguration.java我有这个:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "b.c.g.c")
public class ApplicationConfiguration extends WebMvcConfigurerAdapter {

    @Bean
    @Description("Thymeleaf template resolver serving HTML 5")
    public ServletContextTemplateResolver templateResolver() {
        ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
        templateResolver.setCacheable(false);
        templateResolver.setTemplateMode("HTML5");
        templateResolver.setCharacterEncoding("UTF-8");
        templateResolver.setPrefix("classpath:/templates/");
        templateResolver.setSuffix(".html");

        return templateResolver;
    }

    @Bean
    @Description("Thymeleaf template engine with Spring integration")
    public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.addDialect(new SpringSecurityDialect());
        templateEngine.addDialect(new LayoutDialect(new GroupingStrategy()));
        templateEngine.setTemplateResolver(templateResolver());

        return templateEngine;
    }

    @Bean
    @Description("Thymeleaf view resolver")
    public ViewResolver viewResolver() {

        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(templateEngine());
        viewResolver.setCharacterEncoding("UTF-8");
        viewResolver.setCache(false);
        viewResolver.setOrder(1);

        return viewResolver;
    }

    // other beans
}

而且,在application.properties上,我有这个:

spring.thymeleaf.check-template-location=true
spring.thymeleaf.prefix=classpath:templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false

虽然我看到这些摘录告诉了同样的事情,但我打赌一个人可以去,对吗?

所以,实际上有两个问题,

1)如何确保Spring + Thymeleaf了解在何处找到模板?

2)如何让应用程序回答localhost:8080/appName而不是localhost:8080/

1 个答案:

答案 0 :(得分:3)

推荐方法
我将首先回答你的第二个问题

  1. 您已在src / main / resources中定义application.properties或application.yml(Yaml更好)文件。 Spring-Boot附带default properties文件,您可以在其中设置上下文路径(查找webproperties),移植所有内容。

     server.context-path=/<appname>
    
  2. 要回答第二个问题spring-boot,您可以参考属性文件中的百万美元配置。