关于Freemarker生成静态html文件及中文乱码的问题
先看生成静态html文件:
FreeMarker允许Java servlet保持图形设计同应用程序逻辑的分离,这是通过在模板中密封HTML完成的。模板用servlet提供的数据动态地生成 HTML。模板语言是强大的直观的,编译器速度快,输出接近静态HTML页面的速度。
FreeMarker是一个模板引擎,一个基于模板生成文本输出的通用工具,使用纯Java编写
FreeMarker被设计用来生成HTML Web页面,特别是基于MVC模式的应用程序
虽然FreeMarker具有一些编程的能力,但通常由Java程序准备要显示的数据,由FreeMarker生成页面,通过模板显示准备的数据
FreeMarker不是一个Web应用框架,而适合作为Web应用框架一个组件
FreeMarker与容器无关,因为它并不知道HTTP或Servlet;FreeMarker同样可以应用于非Web应用程序环境
FreeMarker更适合作为Model2框架(如Struts)的视图组件,你也可以在模板中使用JSP标记库
FreeMarker是免费的
模板: |
<html> <head> <title>查看文章: ${newsitem.title} </title> </head> <body> <table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF"> <tr><td> <table width="95%" border="0" align="center" cellpadding="2" cellspacing="6" > <tr> <td height="10" align="left" colspan=2 ></td> </tr> <tr> <td align="left" width="538" > <strong>${newsitem.title}</strong> ( ${newsitem.addtime} ) </td> <td align="right"> <a href="index.jsp">返回</a> </td> </tr> <tr> <td align="left" valign=top colspan=2> <hr align="left" width="95%" size="1" noshade color="#cc0000" > </td> </tr> <tr> <td colspan=2>${newsitem.showContent} </td> </tr> </table> <br> </td></tr> </table> </body> </html>
|
代码: |
import org.apache.commons.logging.Log; |
编码的问题
这里说的是编码的问题。项目使用的都是UTF-8编码,生成的文件在UTF-8编码下察看是乱码,而GBK正常(后来发现因为我用的中文操作系统所以用GBK查看正常)。
当然我把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的不同构造方法
( out)
创建使用默认字符编码的 OutputStreamWriter。
( out, 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);
}
}