在项目中也用到了Freemarker生成静态文件。不过这里我要说的是编码的问题。我们的项目使用的都是UTF-8编码
当然我把Freemarker的配置都改成了UTF-8,我的模版文件也是UTF-8编码的。下面是原来的代码
public void setTemplatePath(Resource templatePath) {
this.templatePath = templatePath;
//设置freemarker的参数
freemarkerCfg = new Configuration();
try {
freemarkerCfg.setDirectoryForTemplateLoading(this.templatePath.getFile());
freemarkerCfg.setObjectWrapper(new DefaultObjectWrapper());
freemarkerCfg.setDefaultEncoding("UTF-8");
} catch (IOException ex) {
throw new SystemException("No Directory found,please check you config.");
}
}
/**
* 生成静态文件
* @param templateFileName 模版名称eg:(biz/order.ftl)
* @param propMap 用于处理模板的属性Object映射
* @param htmlFilePath 要生成的静态文件的路径,相对设置中的根路径,例如 "/biz/2006/5/"
* @param htmlFileName 要生成的文件名,例如 "123.htm"
* @return
*/
private boolean buildHtml(String templateFileName,Map propMap, String htmlFilePath,String htmlFileName){
try {
Template template = freemarkerCfg.getTemplate(templateFileName);
template.setEncoding("UTF-8");
//创建生成文件目录
creatDirs(buildPath.getFilename(),htmlFilePath);
File htmlFile = new File(buildPath + htmlFilePath + htmlFileName);
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFile)));
template.process(propMap,out);
out.flush();
return true;
} catch (TemplateException ex){
log.error("Build Error"+templateFileName,ex);
return false;
} catch (IOException e) {
log.error("Build Error"+templateFileName,e);
return false;
}
}
下面是修改之后的代码
/**
* 生成静态文件
* @param templateFileName 模版名称eg:(biz/order.ftl)
* @param propMap 用于处理模板的属性Object映射
* @param htmlFilePath 要生成的静态文件的路径,相对设置中的根路径,例如 "/biz/2006/5/"
* @param htmlFileName 要生成的文件名,例如 "123.htm"
* @return
*/
private boolean buildHtml(String templateFileName,Map propMap, String htmlFilePath,String htmlFileName){
try {
Template template = freemarkerCfg.getTemplate(templateFileName);
template.setEncoding("UTF-8");
//创建生成文件目录
creatDirs(buildPath.getFilename(),htmlFilePath);
File htmlFile = new File(buildPath + htmlFilePath + htmlFileName);
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFile),"UTF-8"));
template.process(propMap,out);
out.flush();
return true;
} catch (TemplateException ex){
log.error("Build Error"+templateFileName,ex);
return false;
} catch (IOException e) {
log.error("Build Error"+templateFileName,e);
return false;
}
}
原因就在于OutputStreamWriter的不同构造方法
OutputStreamWriter(OutputStream out)
创建使用默认字符编码的 OutputStreamWriter。
OutputStreamWriter(OutputStream out, String charsetName)
创建使用指定字符集的 OutputStreamWriter。
这个是中文JDK的文档说明,刚开始我使用默认的构造函数,所以使用了系统默认的编码,GBK,所以在生成静态文件的时候把UTF-8内容用GBK编码写入了,所以在UTF-8下浏览就有问题。
还有关于修改模版文件同样也要注意这个问题。
public String loadTemplate(String templateName) {
StringBuffer sb = new StringBuffer();
try {
File file = new File(templatePath+"/"+templateName);
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));
String line = reader.readLine();
while(line != null) {
sb.append(line);
sb.append("\r\n");
line = reader.readLine();
}
reader.close();
} catch (IOException e) {
throw new SystemException("Loading template Error:",e);
}
return sb.toString();
}
public void saveTemplate(String templateName, String templateContent) {
try {
File file = new File(templatePath + "/" + templateName);
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),"UTF-8"));
out.write(templateContent);
out.flush();
//扔出templatesave事件
TemplateSaveEvent evt = new TemplateSaveEvent();
evt.setTemplateName(templateName);
dispatchTemplateEvent(evt);
} catch (IOException e) {
throw new SystemException("Write template Error",e);
}
}
分享到:
相关推荐
在FreeMarker中,如果尝试访问的对象或属性不存在,通常会返回一个空字符串。但可以通过`?exists`、`?defined`、`?not_null`等操作符来检查值是否存在。另外,可以设置全局或局部的`default`指令,为未定义的变量...
1. 修改`freemarker.properties`文件,确保其编码设置为中文字符集(如GBK或UTF-8): ``` locale=zh_CN default_encoding=gbk number_format=# date_format=yyyy-MM-dd time_format=HH:mm:Ss datetime_...
在国际化和本地化方面,FreeMarker考虑了字符集、数字和日期时间的本地化需求,允许使用非US字符集作为变量名,并支持多语言模板。而在XML处理上,FreeMarker提供递归遍历XML树的指令,简化了对XML数据的操作。 ...
7. 智能的国际化和本地化:字符集智能化(内部使用 UNICODE),数字格式本地化敏感,日期和时间格式本地化敏感等。 8. 强大的 XML 处理能力:<#recurse> 和指令(2.3 版本)用于递归遍历 XML 树。 -FreeMarker 的...
- **字符集问题**:讨论了如何处理多字符集环境下的文本编码问题。 - **多线程**:介绍了FreeMarker在多线程环境下的使用技巧。 - **Bean的包装**:讲解了如何将Java Bean转换为FreeMarker可以访问的格式。 - **日志...
- **字符集智能处理**: 内部使用Unicode字符集,支持多种字符集。 - **本地化敏感**: 数字、日期和时间格式都支持本地化处理。 - **多语言支持**: 同一模板可以适应多种语言。 - **强大的XML处理能力**: - **...
5. 智能的国际化和本地化:支持字符集智能化、数字格式本地化敏感、日期和时间格式本地化敏感等。 6. 强大的XML处理能力:支持递归遍历XML树,能够在模板中清楚和直觉的访问XML对象模型。 FreeMarker的设计指南是...
FreeMarker还提供了一些高级功能,比如字符集问题的处理,多线程环境下的使用建议,以及如何在Servlet中使用FreeMarker来生成动态网页。此外,FreeMarker也提供了一定的安全策略配置方法,以便在多用户环境中更安全...
在使用FreeMarker进行开发时,遇到的常见问题如变量的使用、字符集问题、多线程环境下的使用、Bean的包装、日志记录、在Servlet中使用FreeMarker、配置安全策略、与Ant的集成、以及Jython包装器等也在手册中有相应的...
- **字符集智能化**:内部使用 Unicode 字符集,支持多种字符集。 - **本地化敏感**:数字格式、日期和时间格式均可以根据本地化需求进行调整。 - **多语言支持**:可以在不同的语言环境中使用相同的模板。 ##### ...
在Java应用中,需要配置FreeMarker环境,包括设置模板目录、输出字符集等。通过`Configuration`对象加载`.ftl`模板,并关联数据模型,该模型包含了要插入到模板中的数据。 6. **数据绑定**: 创建一个Java对象...
在JavaMail中,可以使用 `MimeMessage` 的 `setEncoding` 方法设置邮件的字符集,确保内容正确显示。同时,对于非ASCII字符,如中文,也需要确保邮件头部(如主题)和邮件内容都使用相同的字符集。 8. **Spring配置...
- **字符集支持**:内部采用Unicode字符集,支持多种字符编码。 - **本地化功能**:支持数字、日期和时间的本地化格式,满足不同地区的显示需求。 - **多语言支持**:可在不同语言环境下使用相同的模板,提高开发...
在使用Freemarker时,首先需要配置和初始化`freemarker.template.Configuration`对象,设置模板目录和输出字符集等参数。然后,创建一个`Template`对象,加载模板文件。接着,准备一个数据模型,通常是Java对象或Map...
- **字符集支持**: 内部使用 Unicode,支持多种字符集。 - **本地化格式**: 数字、日期和时间格式可以根据本地化设置自动调整。 - **标识符支持**: 支持非英语字符作为标识符(如变量名)。 - **多语言模板**: 支持...
- **字符集智能化**:内部使用Unicode字符集。 - **本地化敏感**:数字、日期和时间格式本地化敏感。 - **多语言支持**:可以在不同的语言环境中使用相同的模板。 ##### 6. 强大的XML处理能力 - **递归遍历**:`...
5. 智能的国际化和本地化:字符集智能化,数字格式本地化敏感,日期和时间格式本地化敏感,非 US 字符集可以用作标识,多种不同语言的相同模板。 6. 强大的 XML 处理能力:<#recurse> 和指令用于递归遍历 XML 树,...
6. **性能优化**:FreeMarker在设计时考虑到了性能问题,通过缓存机制等手段来提高渲染速度,尤其是在处理大量数据时能够保持高效的响应能力。 #### 四、FreeMarker的基本语法与使用方法 - **变量引用**:在模板中...
- **字符集处理**:Freemarker 内部使用 Unicode,支持多种字符集,能够智能化地处理不同语言的文字。 - **本地化敏感**:数字、日期和时间格式都支持本地化设置,确保输出内容符合用户的本地习惯。 - **标识符多样...