栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

解决:template might not exist or might not be accessible by any of the configured Template Resolvers报错

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

解决:template might not exist or might not be accessible by any of the configured Template Resolvers报错

问题描述:

  Spring Boot 项目访问resources/templates下静态资源文件报500错误,如果页面显示404错误一定是你地址写错了。

报错内容:
Servlet.service() for servlet [dispatcherServlet] in context with path [/test] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template [login], template might not exist or might not be accessible by any of the configured Template Resolvers] with root cause

org.thymeleaf.exceptions.TemplateInputException: Error resolving template [login], template might not exist or might not be accessible by any of the configured Template Resolvers
问题分析:

  SpringBoot项目中resources/templates下的静态资源是不能直接访问的,原因是没有开放访问权限的,因为templates下可能存放有后台管理的页面资源,当templates对外开放就会产生安全隐患,所以templates下的资源需要通过ViewResolver(视图解析器)去解析访问

注意
    这里需要使用@Controller,不能使用@RestController,不然会以Json格式响应到页面,不会进行视图解析。如果application.properties文件配置了二级目录请求地址不需要带上此路径return也不需要返回出去。如果你的静态资源(列如:login.html)放在templates下的一个目录下(列如:pages)则请求地址需要带上也需要return出去。
案例

我这个项目设置了二级目录:server.servlet.context-path=/test
浏览器访问地址:http://localhost:9999/test/pages/login

@Controller
public class PagesController {
	// 方法1:
    @RequestMapping("{pages}/{login}")
    public String pages(@PathVariable String pages,@PathVariable String login){
        return pages+"/"+login;
    }
    //方法二
	
}


了解Spring MVC 执行流程请点击

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/770571.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号