1. 程式人生 > >解決: org.thymeleaf.exceptions.TemplateInputException: Error resolving template

解決: 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 + unaliase