解决: org.thymeleaf.exceptions.TemplateInputException: Error resolving template

本文详细解析了SpringBoot使用Thymeleaf时出现的TemplateInputException错误,阐述了视图解析原理,包括如何计算资源名称,以及前缀和后缀的配置对资源查找的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

spring boot 使用thymleaf时,出现: org.thymeleaf.exceptions.TemplateInputException: Error resolving template [xxxxx], template might not exist or might not be accessible by any of the configured Template Resolvers 说明配置或返回视图不正确。源码中视图解析之后找资源文件的目录: org.thymeleaf.templateresolver.AbstractConfigurableTemplateResolver#computeResourceName(org.thymeleaf.IEngineConfiguration, java.lang.String, java.lang.String, java.lang.String, java.lang.String, boolean, java.util.Map<java.lang.String,java.lang.String>, java.util.Map<java.lang.String,java.lang.Object>) 查看此方法,即可知道根本原因了。

/**
     * <p>
     *   Computes the resource name that will be used for resolving, from the template name and other
     *   parameters configured at this <em>configurable</em> resolver.
     * </p>
     * <p>
     *   This method can be overridden by subclasses that need to modify the standard way in which the
     *   name of the template resource is computed by default before passing it to the real resource
     *   resolution mechanism (in method {@link #computeTemplateResource(IEngineConfiguration, String, String, String, String, Map)}
     * </p>
     * <p>
     *   By default, the resource name will be created by first applying the <em>template aliases</em>, and then
     *   adding <em>prefix</em> and <em>suffix</em> to the specified <em>template</em> (template name).
     * </p>
     *
     * @param configuration the engine configuration in use.
     * @param ownerTemplate the owner template, if the resource being computed is a fragment. Might be null.
     * @param template the template (normally the template name, except for String templates).
     * @param prefix the prefix to be applied.
     * @param suffix the suffix to be applied.
     * @param forceSuffix whether the suffix should be forced or not.
     * @param templateAliases the template aliases map.
     * @param templateResolutionAttributes the template resolution attributes, if any. Might be null.
     * @return the resource name that should be used for resolving
     * @since 3.0.6
     */
    protected String computeResourceName(
            final IEngineConfiguration configuration, final String ownerTemplate, final String template,
            final String prefix, final String suffix, final boolean forceSuffix,
            final Map<String, String> templateAliases, final Map<String, Object> templateResolutionAttributes) {

        Validate.notNull(template, "Template name cannot be null");

        String unaliasedName = templateAliases.get(template);
        if (unaliasedName == null) {
            unaliasedName = template;
        }

        final boolean hasPrefix = !StringUtils.isEmptyOrWhitespace(prefix);
        final boolean hasSuffix = !StringUtils.isEmptyOrWhitespace(suffix);

        final boolean shouldApplySuffix =
                hasSuffix && (forceSuffix || !ContentTypeUtils.hasRecognizedFileExtension(unaliasedName));

        if (!hasPrefix && !shouldApplySuffix){
            return unaliasedName;
        }

        if (!hasPrefix) { // shouldApplySuffix
            return unaliasedName + suffix;
        }

        if (!shouldApplySuffix) { // hasPrefix
            return prefix + unaliasedName;
        }

        // hasPrefix && shouldApplySuffix
        return prefix + unaliasedName + suffix;

    }

资源文件最终为位置: prefix + unaliasedName + suffix

默认 spring boot thymleaf 默认前缀(prefix)为: spring.thymeleaf.prefix=classpath:/templates/ 后缀(suffix)为: spring.thymeleaf.suffix=.html 比如登录: 返回视图为 login, 则最终会查找资源位置: classpath:/templates/login.html 没问题,如果返回视图为: /login, 则最终会查找资源位置: classpath:/templates//login.html 就会报找不到资源会无法解析了。

转载于:https://my.oschina.net/who7708/blog/3035658

项目启动起来了,但是访问失败打印日志:[heal-dialysis-server:18760::] 2025-03-25 10:17:30.320[ INFO] 27184 [] [XNIO-2 task-2:44332] [io.undertow.servlet.?:?] Initializing Spring DispatcherServlet ‘dispatcherServlet’ [heal-dialysis-server:18760::] 2025-03-25 10:17:30.320[ INFO] 27184 [] [XNIO-2 task-2:44332] [org.springframework.web.servlet.DispatcherServlet.?:?] Initializing Servlet ‘dispatcherServlet’ [heal-dialysis-server:18760::] 2025-03-25 10:17:30.323[ INFO] 27184 [] [XNIO-2 task-2:44335] [org.springframework.web.servlet.DispatcherServlet.?:?] Completed initialization in 3 ms [heal-dialysis-server:18760::] 2025-03-25 10:17:30.808[ERROR] 27184 [] [XNIO-2 task-2:44820] [org.thymeleaf.TemplateEngine.?:?] [THYMELEAF][XNIO-2 task-2] Exception processing template “index”: Error resolving template [index], template might not exist or might not be accessible by any of the configured Template Resolvers org.thymeleaf.exceptions.TemplateInputException: Error resolving template [index], template might not exist or might not be accessible by any of the configured Template Resolvers at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:869) at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:607) at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1103) at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1077) at org.thymeleaf.spring6.view.ThymeleafView.renderFragment(ThymeleafView.java:372) at org.thymeleaf.spring6.view.ThymeleafView.render(ThymeleafView.java:192) at ... [heal-dialysis-server:18760::] 2025-03-25 10:17:30.817[ERROR] 27184 [] [XNIO-2 task-2:44829] [io.undertow.request.?:?] UT005023: Exception handling request to / jakarta.servlet.ServletException: Request processing failed: org.thymeleaf.exceptions.TemplateInputException: Error resolving template [index], template might not exist or might not be accessible by any of the configured Template Resolvers
最新发布
03-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值