亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

springboot搭配thymeleaf訪問html頁面的時候,什么時候需要自定義前綴和后綴呢

為什么我全部使用默認配置會報錯,必須自己定義前綴和后綴

解析器配置如下:

@Configuration
public?class?WebMvcConfig?extends?WebMvcConfigurerAdapter?implements?ApplicationContextAware?{
????//實現的這個接口能幫助我們設置spring的上下文
????private?ApplicationContext?applicationContext;

????@Override
????public?void?setApplicationContext(ApplicationContext?applicationContext)?throws?BeansException?{
????????????this.applicationContext=applicationContext;
????}
????//對模板資源進行解析(模板資源解析器
????@Bean
????@ConfigurationProperties(prefix?=?"spring.thymeleaf")
????public?SpringResourceTemplateResolver?templateResolver()?{
????????SpringResourceTemplateResolver?templateResolver?=?new?SpringResourceTemplateResolver();
????????templateResolver.setApplicationContext(this.applicationContext);
????????templateResolver.setCharacterEncoding("UTF-8");

????????return?templateResolver;
????}


????@Bean
????public?SpringTemplateEngine?templateEngine(){
????????SpringTemplateEngine?templateEngine=new?SpringTemplateEngine();
????????templateEngine.setTemplateResolver(templateResolver());
????????//設置支持spring?el表達式
????????templateEngine.setEnableSpringELCompiler(true);
????????return?templateEngine;
????}
????//配置視圖解析器
????@Bean
????public?ThymeleafViewResolver?viewResolver(){
????ThymeleafViewResolver?viewResolver=new?ThymeleafViewResolver();
????viewResolver.setTemplateEngine(templateEngine());
????return?viewResolver;
????}
}

thymeleaf屬性只配置https://img1.sycdn.imooc.com//5b2afeb4000189a905830072.jpg

控制器代碼為:

https://img1.sycdn.imooc.com//5b2aff3b0001b4ee07180217.jpg

啟動項目后,訪問http://localhost:8080/helloword就會報解析thymeleaf錯誤如下

https://img1.sycdn.imooc.com//5b2affda0001375511740176.jpg

這個報錯真的很懵。求問為什么?

正在回答

1 回答

你如果訪問的是 WEB-INF下面的hello.html頁面,那么你就要添加前綴 ‘/WEB-INF/’和后綴 ‘.html’。因為你在方法中就返回了一個 ‘hello’,不加前綴拼接起來就是:‘ http://localhost:8080/hello’了。

0 回復 有任何疑惑可以回復我~
#1

_Henry_Lin_

我也是剛學,如果回答錯誤請指出,共勉。
2018-06-22 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
2小時學會Spring Boot
  • 參與學習       151526    人
  • 解答問題       1132    個

Spring Boot入門視頻教程,你將學會使用Spring Boot快速構建應用程序

進入課程

springboot搭配thymeleaf訪問html頁面的時候,什么時候需要自定義前綴和后綴呢

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號