Freemarker generate static document on the issue
Freemarker generate static document on the issue of the first thanked JScud good. "The use of static documents FreeMarker Html generation (example)"
????? In our projects also uses Freemarker generate static document. But here I want to say is that coding problems. Our project is the use of the UTF-8 encoding, I Feiyun Xiaoxia direct use of methods provided in the documents generated by UTF-8 encoding is inspected hash, and GBK normal (because I later found by the Chinese-language operating system used so GBK Show normal).
????? Freemarker course, I have changed the configuration UTF-8, I document the template is UTF-8 encoded. Below is the original code
?? Public? Void? SetTemplatePath (Resource? TemplatePath)? (
???????? This.templatePath? =? TemplatePath;
Parameter settings 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 document
?????*?Param? TemplateFileName? Template name eg: (biz / order.ftl)
?????*?Param? PropMap? Template for dealing with the properties of Object Mapping?
?????*?Param? HtmlFilePath? Generated static document to the path of the root of the relative path settings, such as "/ biz/2006/5 /"?
?????*?Param? HtmlFileName? To generate a file name, such as "123. Htm?"
?????*?Return
?????*/
?? Private? Boolean? BuildHtml (String? TemplateFileName, Map? PropMap, the String? HtmlFilePath, String? HtmlFileName) (
???????? Try? (
???????????? Template? Template? =? FreemarkerCfg.getTemplate (templateFileName);
???????????? Template.setEncoding ( "UTF-8");
????????????// Create documents generated 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 modifications
????/**
?????*? Generate static document
?????*?Param? TemplateFileName? Template name eg: (biz / order.ftl)
?????*?Param? PropMap? Template for dealing with the properties of Object Mapping?
?????*?Param? HtmlFilePath? Generated static document to the path of the root of the relative path settings, such as "/ biz/2006/5 /"?
?????*?Param? HtmlFileName? To generate a file name, such as "123. Htm?"
?????*?Return
?????*/
?? Private? Boolean? BuildHtml (String? TemplateFileName, Map? PropMap, the String? HtmlFilePath, String? HtmlFileName) (
???????? Try? (
???????????? Template? Template? =? FreemarkerCfg.getTemplate (templateFileName);
???????????? Template.setEncoding ( "UTF-8");
????????????// Create documents generated 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;
????????}
????????
????} Reason for this is that the different construction method OutputStreamWriter
OutputStreamWriter (OutputStream? Out)
?????????? Created using the default character encoding OutputStreamWriter.
OutputStreamWriter (OutputStream? Out, String? CharsetName)
?????????? Create designated Character Set OutputStreamWriter.
This is the Chinese JDK documented, I just started using the default constructor, so use the system default coding, GBK, static document generated when the UTF-8 content into the code by GBK, so UTF-8 browse there will be problems.
Also on the amendments to the same document templates to pay attention to the issue.
?? 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, the 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 ();
????????????// Thrown templatesave incident
???????????? TemplateSaveEvent? Evt? =? New? TemplateSaveEvent ();
???????????? Evt.setTemplateName (templateName);
???????????? DispatchTemplateEvent (evt);
????????}? Catch (IOException? E) (
???????????? Throw? New? SystemException ( "Write? Template? Error", e);
????????}
????}
Posted on 2006-06-21 10:46 MO reading (1341) Comments (0) edit their collections quoted Category: Other, Spring






