在 spring.ftl springMacroRequestContext 函数中评估 spring 消息期间,Freemarker 获取 null

问题描述 投票:0回答:1

当我尝试在 Spring MVC 应用程序中显示资源包中的标签时,当我尝试显示值时出现空错误。我使用 spring.ftl 定义了一些 utils 方法。我将其加载到类路径中并且配置没问题。您可以在下面找到我的 html 模板和 Fremmarker 配置。

配置:

private static Configuration getConfig() throws IOException, TemplateException {
        // Create your Configuration instance, and specify if up to what FreeMarker
        // version (here 2.3.32) do you want to apply the fixes that are not 100%
        // backward-compatible. See the Configuration JavaDoc for details.
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_32);

        // Specify the source where the template files come from. Here I set a
        // plain directory for it, but non-file-system sources are possible too:
        //cfg.setDirectoryForTemplateLoading(new File("/where/you/store/templates"));

        // From here we will set the settings recommended for new projects. These
        // aren't the defaults for backward compatibilty.

        cfg.setDefaultEncoding("UTF-8");

        // Sets how errors will appear.
        // During web page *development* TemplateExceptionHandler.HTML_DEBUG_HANDLER is better.
        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);

        // Don't log exceptions inside FreeMarker that it will thrown at you anyway:
        cfg.setLogTemplateExceptions(false);

        // Wrap unchecked exceptions thrown during template processing into TemplateException-s:
        cfg.setWrapUncheckedExceptions(true);

        // Do not fall back to higher scopes when reading a null loop variable:
        cfg.setFallbackOnNullLoopVariable(false);

        cfg.setTemplateLoader(new ClassTemplateLoader(DocGeneratorUtils.class, "/templates"));
        Properties settings = new Properties();
        settings.setProperty("auto_import", "/spring.ftl as spring");
        cfg.setSettings(settings);

        return cfg;
    }

HTML 模板:

<#import "/spring.ftl" as spring/>

<!DOCTYPE html>
<html>

<body>
<@spring.message "resource.bundle.key"/>
</body>
</html>

错误:

The following has evaluated to null or missing:
==> springMacroRequestContext  [in template "spring.ftl" at line 28, column 24]

----
Tip: If the failing expression is known to legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing</#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??
----

----
FTL stack trace ("~" means nesting-related):
    - Failed at: ${springMacroRequestContext.getMessag...  [in template "spring.ftl" in macro "message" at line 28, column 22]
    - Reached through: @spring.message "sale.enum.TravelPass...  [in template "MY_TEMPLATE" at line 7, column 9]
----
FreeMarker template error:
The following has evaluated to null or missing:
==> springMacroRequestContext  [in template "spring.ftl" at line 28, column 24]

----
Tip: If the failing expression is known to legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing</#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??
----

----
FTL stack trace ("~" means nesting-related):
    - Failed at: ${springMacroRequestContext.getMessag...  [in template "spring.ftl" in macro "message" at line 28, column 22]
    - Reached through: @spring.message "resource.bundle.key...  [in template "MY_TEMPLATE" at line 53, column 9]
----

Java stack trace (for programmers):
----
freemarker.core.InvalidReferenceException: [... Exception message was already printed; see it above ...]
    at freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:134)
    at freemarker.core.UnexpectedTypeException.newDescriptionBuilder(UnexpectedTypeException.java:85)
    at freemarker.core.UnexpectedTypeException.<init>(UnexpectedTypeException.java:48)
    at freemarker.core.NonHashException.<init>(NonHashException.java:49)
    at freemarker.core.Dot._eval(Dot.java:48)
java spring freemarker
1个回答
0
投票

非常有趣的信息。感谢分享。

© www.soinside.com 2019 - 2024. All rights reserved.