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]

如果你在使用 Thymeleaf 遇到了页面报错,可以查看控制台输出的错误信息,找到具体的问题所在。常见的错误包括语法错误、模板引用错误、标签使用错误等等。 如果你想利用 Thymeleaf 自定义错误页面,可以按照以下步骤进行: 1. 首先在 `src/main/resources/templates` 目录下创建一个 `error` 目录。在该目录下创建一个名为 `error.html` 的 Thymeleaf 模板文件。 2. 在 `application.properties` 文件中配置错误处理的地址: ``` server.error.path=/error ``` 3. 创建一个名为 `ErrorController` 的控制器类,用于处理错误请求: ```java @Controller public class ErrorController implements org.springframework.boot.web.servlet.error.ErrorController { @RequestMapping("/error") public String handleError(HttpServletRequest request, HttpServletResponse response) { // 获取错误状态码 Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code"); // 根据状态码返回不同的错误页面 if (statusCode == 404) { return "error/404"; } else { return "error/500"; } } @Override public String getErrorPath() { return "/error"; } } ``` 4. 在 `error` 目录下创建 `404.html` 和 `500.html` 两个 Thymeleaf 模板文件,用于显示不同类型的错误页面。 例如,`404.html` 可以这样编写: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>404 Not Found</title> </head> <body> <h1>404 Not Found</h1> <p>您请求的页面不存在,请检查您输入的地址是否正确。</p> </body> </html> ``` 5. 访问不存在的页面,尝试触发 404 错误,可以看到自定义的错误页面。 如果你想测试 500 错误,可以在控制器中抛出一个异常,例如: ```java @RequestMapping("/test") public String test() { throw new RuntimeException("测试错误"); } ``` 然后访问 `/test` 地址即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值