简体   繁体   English

使用Freemarker生成Java .properties文件

[英]Using Freemarker to generate Java .properties files

I am currently using Freemarker to generate a number of configuration files. 我目前正在使用Freemarker生成许多配置文件。 So far these have been either xml files or properietary format text files. 到目前为止,这些文件要么是xml文件,要么是专有格式的文本文件。 I now would like to generate some Java .properties files but have hit a couple of issues. 我现在想生成一些Java .properties文件,但是遇到了两个问题。

The first is character encoding. 首先是字符编码。 As far as I can see simply adding 据我所见,只需添加

<#ftl encoding="8859_1">

to the start of the file should sort this out. 到文件开头应对此进行整理。

The second issue is the escaping of the keys and values. 第二个问题是键和值的转义。 The keys are probably ok as I would be hardcoding these in the template anyway so I can escape them in the template. 密钥可能还可以,因为无论如何我都会在模板中对它们进行硬编码,以便可以在模板中对其进行转义。 The values will be coming from my data model and so will need escaping. 这些值将来自我的数据模型,因此需要转义。

I can see how I can create my own user defined directive and by installing it as a shared variable use it in my template. 我可以看到如何创建自己的用户定义指令,并通过将其安装为共享变量来在模板中使用它。

Is this the best or only way to do this? 这是最好的还是唯一的方法? I would have thought generating .properties files is something that has been tackled many times before and was hoping something may already exist before I start writing my own code. 我以为生成.properties文件是以前已经解决过很多次的事情,并且希望在开始编写自己的代码之前可能已经存在了一些东西。

The class java.util.Properties got various store methods to save properties to OutputStreams or files. 类java.util.Properties获得了各种存储方法,可将属性保存到OutputStreams或文件中。 This seems more preferable than trying to adapt freemarker. 这似乎比尝试适应freemarker更可取。

I don't get what are the charset issues that are specific to generating properties files. 我不了解特定于生成properties文件的字符集问题。 But note that the charset of the template and the charset of the output are independent, so you might as well use the same charset for these templates as for the others (like maybe UTF-8). 但是请注意,模板的字符集和输出的字符集是独立的,因此您最好对这些模板使用与其他模板相同的字符集(例如UTF-8)。

As of escaping, always use auto-escaping if you can. 从转义开始,请尽可能使用自动转义。 In 2.3.24 that will be especially sleek, but unless you are allowed to use unreleased versions, you had to wait for that until the end of February or so. 在2.3.24中,这会特别流畅,但是除非允许您使用未发布的版本,否则您必须等到2月底左右。 (If you can use unreleased/unofficial versions, you can find out about the internal testing releases in the developer list archive.) Before 2.3.24, there's <#escape x as propEsc(x)>all the template content here</#escape> , where propEsc is a TemplateMethodModelEx (not a TemplateDirectiveModel ) that you have added as shared variable or such. (如果可以使用未发布/非官方的版本,则可以在开发人员列表档案中找到有关内部测试版本的信息。)在2.3.24之前, <#escape x as propEsc(x)>all the template content here</#escape> ,其中propEsc是您已作为共享变量等添加的TemplateMethodModelEx (不是TemplateDirectiveModel )。 And so all ${...} -s will be magically escaped. 因此,所有${...} -s都将被神奇地转义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM