2013-07-29 43 views
0

我試圖讓Freemarker將我的模板文件解析爲UTF-8,但它似乎總是將它們解析爲Cp1252。在freemarker模板中使用<#ftl>指令

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允許我指定編碼。

我把它添加到模板文件(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

請勿使用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>),因此您不需要使用#ftl