java spring boot - 如何正确定义模板位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39262370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
spring boot - how to correctly define template locations?
提问by gtludwig
After struggling for some time with a Spring app (and spring boot, for that matter), it seems I'm finally about to get it working.
在使用 Spring 应用程序(和 spring boot,就此而言)挣扎了一段时间之后,我似乎终于要让它工作了。
I have shifted through dependency resolutions and maven build already. Application starts (and very quickly!) but when I try to access
我已经通过依赖解决方案和 Maven 构建转移了。应用程序启动(而且非常快!)但是当我尝试访问时
localhost:8080
localhost:8080
I get the following browser message whenever I try to reach the application's landing page:
每当我尝试访问应用程序的登录页面时,都会收到以下浏览器消息:
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
The src/main/resources
folder is
该src/main/resources
文件夹
src/main/resources
static // CSS, IMG and JS
templates // html
application.properties
log4j.properties
Now, I understand I may be mixing concepts, but on my ApplicationConfiguration.java
I have this:
现在,我明白我可能会混淆概念,但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
}
And, on application.properties
, I have this:
而且,在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
Though I see these excerpts tell the same thing, I bet one can go, right?
虽然我看到这些摘录讲述了同样的事情,但我打赌有人可以去,对吧?
So, two questions, actually,
所以,实际上有两个问题,
1) how to make sure Spring + Thymeleaf understand where to find the templates?
1) 如何确保 Spring + Thymeleaf 知道在哪里可以找到模板?
2) how to get the application to answer to localhost:8080/appName
instead of localhost:8080/
?
2)如何让应用程序回答localhost:8080/appName
而不是localhost:8080/
?
采纳答案by Praveen Kumar K S
Recommended Approach
I will first answer your second question
推荐方法
我先回答你的第二个问题
You have define the application.properties or application.yml (Yaml is better) file in src/main/resources. Spring-Boot comes with default propertiesfile where you can set your context path (look for webproperties), port everything.
server.context-path=/<appname>
To answer your second question spring-boot you can refer the thymeleaf configurations in the properties file.
您已经在 src/main/resources 中定义了 application.properties 或 application.yml(Yaml 更好)文件。Spring-Boot 带有默认属性文件,您可以在其中设置上下文路径(查找 webproperties),移植所有内容。
server.context-path=/<appname>
要回答您的第二个问题 spring-boot,您可以参考属性文件中的 thymeleaf 配置。