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

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

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值