The BOM of unicode file in Freemarker's directive "include" made me crazy

作者在使用FreeMarker的include指令将navigation.html文件引入到web项目中时遇到了IE浏览器中出现多余换行的问题,该问题在Firefox浏览器中并未出现。通过排除法发现,保存文件时包含的BOM是导致这一现象的原因。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Just now, I was adding a navigation to our "jstore for moliyo" web project using a derective "include" as this:
...
<body style="margin:0px;padding:0px;">
<#include "navigation.html">
...
</body>
...

the file "navigation.html" is a unicode file with a BOM:
<table><tr><td>some other words.</td></tr></table>

but a line break was displayed in Microsoft Internet Explorer, but not in Mozilla Firefox.

I'd tried hard to delete the line break, but failed.

I was crazying...

But when I saved the file "navigation.html" with no BOM, the line break was fucked off in IE.

I thought this is a bug of freemarker and I'd reportted it in the sf.net.

I'd downloaded its source from the CVS repository and attempted to find the bugged code, but nothing I found.

Well, I should go back for bed, I'm sneezing as I've caught a cold last weekeed.

<think>我们正在讨论Velocity模板引擎的配置项变更问题。用户特别询问了关于配置项'file.resource.loader.unicode'已被弃用,以及它的替代配置项'resource.loader.file.unicode'的情况。 根据Velocity的官方文档和更新记录,Velocity在版本2.0之后进行了一些配置项的调整。其中,一些旧的配置项被标记为过时(deprecated),并推荐使用新的配置项。 具体到用户的问题: - 旧配置项:`file.resource.loader.unicode` - 新配置项:`resource.loader.file.unicode` 实际上,在Velocity 2.0及更高版本中,配置项的前缀进行了统一调整,以更清晰地表示其作用范围。`file.resource.loader.unicode`用于设置文件资源加载器(file resource loader)在处理文件时是否使用Unicode编码。现在,它被替换为`resource.loader.file.unicode`,其含义和功能相同。 因此,在Velocity的配置文件(通常是`velocity.properties`)中,应该将原有的: ``` file.resource.loader.unicode = true ``` 改为: ``` resource.loader.file.unicode = true ``` 注意:这个配置项是可选的,默认值通常为false。如果需要处理Unicode文件(例如UTF-8编码的文件),则需要显式设置为true。 另外,Velocity还支持其他资源加载器(如classpath、url等),它们的配置项也有类似的调整。例如,classpath资源加载器的配置项从`classpath.resource.loader.unicode`变为`resource.loader.classpath.unicode`。 ### 替代方案:使用Freemarker 如果用户不想处理Velocity的配置变更,或者遇到其他问题,可以考虑切换到Freemarker模板引擎。Freemarker是另一种流行的模板引擎,配置方式不同,且没有这个特定的配置项问题。 在Freemarker中,处理编码的方式是在配置中设置默认编码: ```java Configuration cfg = new Configuration(Configuration.VERSION_2_3_30); cfg.setDefaultEncoding("UTF-8"); ``` ### 总结步骤: 1. **更新配置项**:将Velocity配置中的`file.resource.loader.unicode`替换为`resource.loader.file.unicode`,并设置合适的值(通常为true以支持UTF-8)。 2. **检查Velocity版本**:确保使用的Velocity版本在2.0以上,因为旧版本可能不支持新的配置项。 3. **清理缓存**:如果更改配置后没有生效,尝试清理Velocity的缓存(如果配置了缓存)或重启应用。 4. **考虑切换模板引擎**:如果问题持续,或者项目允许,可以切换到Freemarker等替代模板引擎。 ### 参考文档 - [Velocity 2.0 Release Notes](https://velocity.apache.org/engine/2.0/release-notes.html) - [Velocity Configuration Keys](https://velocity.apache.org/engine/2.2/configuration.html) §§相关问题§§ 1. Velocity模板引擎还有哪些常用的配置项? 2. 如何配置Velocity以支持多模板加载路径? 3. Freemarker和Velocity在性能上有什么差异? 4. 如何解决Velocity模板中的中文乱码问题?</think>### Velocity配置项变更说明 #### 问题核心 - **旧配置项**:`file.resource.loader.unicode` - **新配置项**:`resource.loader.file.unicode` - **变更原因**:Velocity 2.x 重构了配置命名规范,统一使用 `resource.loader.<loader_name>.<property>` 格式[^1]。 #### 配置迁移步骤 1. **修改配置文件** 在 `velocity.properties` 中替换旧配置项: ```properties # 弃用旧配置(Velocity 1.x) # file.resource.loader.unicode = true # 使用新配置(Velocity 2.x+) resource.loader.file.unicode = true ``` 2. **功能说明** - 此配置项控制文件资源加载器(`file` loader)是否启用Unicode支持(默认`false`)。 - 设置为 `true` 时,Velocity会按UTF-8编码读取模板文件,避免中文乱码[^1]。 3. **验证配置** 通过代码检查配置是否生效: ```java VelocityEngine ve = new VelocityEngine(); ve.setProperty("resource.loader.file.unicode", "true"); ve.init(); ``` --- ### 替代方案:Freemarker模板引擎 若需规避Velocity配置变更问题,可切换至Freemarker: #### 1. 添加依赖(Maven) ```xml <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.31</version> </dependency> ``` #### 2. 配置编码(无需复杂键名) ```java Configuration cfg = new Configuration(Configuration.VERSION_2_3_31); cfg.setDefaultEncoding("UTF-8"); // 直接设置全局编码 cfg.setDirectoryForTemplateLoading(new File("/templates")); ``` #### 优势对比 | **特性** | Velocity | Freemarker | |------------------|---------------------------------|--------------------------| | **编码配置** | 需单独配置加载器(`resource.loader.file.unicode`) | 全局`setDefaultEncoding` | | **语法复杂度** | 简单 | 更强大(支持逻辑控制) | | **配置迁移成本** | 需修改键名 | 无需适配旧配置项 | --- ### 根本原因与预防建议 - **弃用背景**:Velocity 2.x 将资源加载器配置标准化,旧键名(如 `file.resource.loader.*`)被标记为过时[^1]。 - **最佳实践**: 1. 升级到 Velocity 2.3+ 并更新配置键名。 2. 新项目优先选用 Freemarker(编码配置更简洁,社区活跃)。 3. 使用工具(如 `mvn dependency:tree`)检查依赖冲突,确保无低版本Velocity残留。 > **提示**:Velocity官方文档明确标注了过时配置项,升级前请查阅[配置迁移指南](https://velocity.apache.org/engine/2.3/upgrading.html)[^1]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值