springBoot+thymeleaf访问时报错!!!!

进行写了一个springboot+thymeleaf访问时报错,卧槽,这真是无奈了,看了路由发现没问题:

这是为什么呢?理论上前缀和后缀是可以自动帮我们补全的,为什么不行,也不知道。

WebMvcConfig:
package liangliang.bigdata.config;

import org.springframework.beans.BeansException;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;

@Configuration
//帮助我们获取spring的上下文
public class WebMvcConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware {
    //私有化一个类
    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();
        //设置spring的上下文
        templateResolver.setApplicationContext(this.applicationContext);
        return templateResolver;
    }
    /**
     * Thymeleaf标准方言解析器
     */
    @Bean
    public SpringTemplateEngine templateEngine(){
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        //配置资源解析器的引擎
        templateEngine.setTemplateResolver(templateResolver());
        //设置支持EL表达式
        templateEngine.setEnableSpringELCompiler(true);
        return templateEngine;
    }
    /**
     * 视图解析器
     */
    @Bean
    public ThymeleafViewResolver viewResolver(){
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        //配置Thymeleaf标准方言解析器
        viewResolver.setTemplateEngine(templateEngine());
        return viewResolver;
    }



}

我发现上面补全themplates是假的,卧槽:

application.properties:

spring.profiles.active=dev

#帮助我们在开发过程中执行的时候jpa我们执行的sql的语句
spring.jpa.show-sql=true
#hibernate 执行只进行sql的验证,不会对sql做一些增删改的操作的
spring.jpa.hibernate.ddl-auto=validate

#sql打印级别设置为debug
logging.level.org.hibernate.SQL = debug

#设置session会话存储的类型
spring.session.store-type = hash_map
#关闭http基本验证
security.basic.enabled=false

#thymeleaf
spring.thymeleaf.mode=HTML
#spring.thymeleaf.suffix=.html
#spring.thymeleaf.prefix=classpath:/templates/

解决办法,加上前缀和后缀就可以解决了!!!

 Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/index]

发布了195 篇原创文章 · 获赞 87 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/qq_41479464/article/details/103054079