First of all, thank JSCUD to provide good articles. "
Use freemarker to generate an HTML static file (instance) "
In our project, FreeMarker generated a static file. But here I want to say the problem. Our projects are all UTF-8 encodings. I directly use the way to use the flying clouds. The files generated in UTF-8 encoding are garbled, while GBK is normal (later found because I used Chinese operating systems. GBK View Normal).
Of course, I changed the FREMARKER configuration to UTF-8, my template file is also UTF-8 encoding. Here is the original code
public
Void
SetTemplatePath (Resource TemplatePath) {
THIS
.TemplatePath
=
TemplatePath;
//
Set the parameters of 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.
"
}}
/ **
* Generate static files *
@Param
TemplateFileName template name EG: (Biz / Order.FTL) *
@Param
PROPMAP for processing templates Object mapping *
@Param
HTMLFilePath To generate the path of the static file, the root path in the relative setting, such as "/ BIZ / 2006/5 /" *
@Param
HTMLFileName To generate file name, such as "123.htm" *
@Return
* /
Private
Boolean
Buildhtml (String TemplateFileName, Map PropMap, String HTMLFILEPATH, STRING HTMLFILENAME) {
Try
{Template Template
=
FreeMarkercfg.getTemplate (TemplateFileName); Template.setencoding
"
UTF-8
"
);
//
Create a generated file directory
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
}}
Below is the code after modification
/ **
* Generate static files *
@Param
TemplateFileName template name EG: (Biz / Order.FTL) *
@Param
PROPMAP for processing templates Object mapping *
@Param
HTMLFilePath To generate the path of the static file, the root path in the relative setting, such as "/ BIZ / 2006/5 /" *
@Param
HTMLFileName To generate file name, such as "123.htm" *
@Return
* /
Private
Boolean
Buildhtml (String TemplateFileName, Map PropMap, String HTMLFILEPATH, STRING HTMLFILENAME) {
Try
{Template Template
=
FreeMarkercfg.getTemplate (TemplateFileName); Template.setencoding
"
UTF-8
"
);
//
Create a generated file directory
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
}}
The reason is the different construction methods of OutputStreamWriter
OutputStreamWriter
(
Outputstream
OUT Creates OutputStreamWriter encoded using the default character.
OutputStreamWriter
(
Outputstream
OUT,
String
CHARSETNAME Creates OutputStreamWriter using the specified character set. This is a Chinese JDK document description, just starting to use the default constructor, so use the system default encoding, GBK, so write UTF-8 content with GBK encoding when generating static files, so in UTF- 8 There is a problem with the browsing. There is also the same attention to this problem with the modification of the template file.
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 ();
//
Throw the Templatesave event
TemplatesaveEvent EVT
=
New
TemplatesaveEvent (); evt.setTemplationename (TemplateName); DispatchTemplateEvent (EVT);
Catch
IOEXCEPTION E) {
Throw
New
SYSTEMEXCEPTION
"
Write Template Error
"
, e);}}
In addition to specifically indicated, this article is copyright
Modo bubbles.
Signature, non-commercial use, consistent. Somebody (Mo Do)