使用< #ftl> freemarker模板中的指令

时间:2013-07-29 17:03:03

标签: spring encoding character-encoding freemarker

我正在尝试让Freemarker将我的模板文件解析为UTF-8,但似乎总是将它们解析为Cp1252。

2013-07-30 00:00:00.984 DEBUG freemarker.cache:? Could not find template in cache, creating new one; id=["myFile.ftl"["es_MX",Cp1252,parsed] ]

我搜索过,found that adding the FTL directive允许我指定编码。

我将它添加到模板文件(在Notepad ++中用BOM保存在UTF-8中),如下所示:

<#ftl encoding="UTF-8">
Estimado Usuario,
Testing this content!
<#/ftl>

但是请记录下来。

MessageTemplateException [message=Unable to process template due to Encountered "<#ftl " at line 1, column 4 in myFile.ftl.
Was expecting one of:
    <EOF> 
    <ATTEMPT> ...
    <IF> ...
    <LIST> ...
    <FOREACH> ...
    <SWITCH> ...
    <ASSIGN> ...
    <GLOBALASSIGN> ...
    <LOCALASSIGN> ...
    <_INCLUDE> ...
    <IMPORT> ...
    <FUNCTION> ...
    <MACRO> ...
    <TRANSFORM> ...
    <VISIT> ...
    <STOP> ...
    <RETURN> ...
    <CALL> ...
    <SETTING> ...
    <COMPRESS> ...
    <COMMENT> ...
    <TERSE_COMMENT> ...
    <NOPARSE> ...
    <BREAK> ...
    <SIMPLE_RETURN> ...
    <HALT> ...
    <FLUSH> ...
    <TRIM> ...
    <LTRIM> ...
    <RTRIM> ...
    <NOTRIM> ...
    <SIMPLE_NESTED> ...
    <NESTED> ...
    <SIMPLE_RECURSE> ...
    <RECURSE> ...
    <FALLBACK> ...
    <ESCAPE> ...
    <NOESCAPE> ...
    <UNIFIED_CALL> ...
    <WHITESPACE> ...
    <PRINTABLE_CHARS> ...
    <FALSE_ALERT> ...
    "${" ...
    "#{" ...
 with inputs of {loginUrl=testURL} in myFile.ftl with locale es_MX, code=SUCCESS, arguments=[]]

也许我不是这样做的?

1 个答案:

答案 0 :(得分:1)

不要使用BOM。也不要使用</#ftl>,只有<#ftl>。但最重要的是,如果你的大部分模板都是UTF-8,只需将FreeMarker的默认编码设置为UTF-8(在Spring中,类似<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">...<property name="freemarkerSettings">...<props>...<prop key="default_encoding">UTF-8</prop>),所以你不需要使用{{1因为这个。