如何解決freemarker靜態(tài)化生成html頁面亂碼的問題
freemarker靜態(tài)化生成html頁面亂碼的問題
今天在整理之前所學(xué)的知識,在復(fù)習(xí)freemarker生成html頁面的時(shí)候出現(xiàn)了中文亂碼的問題,
在費(fèi)了一番時(shí)間后終于找到問題的原因,覺得挺有意思,就把這段記錄下來。
下面是springmvc的核心代碼
?<!-- freemarker的配置 --> ? ? <bean id="freeMarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> ? ? ?? ??? ?<!-- templateLoaderPath ?:前綴 --> ? ? ?? ??? ?<property name="templateLoaderPath" value="/WEB-INF/ftl/"></property> ? ? ?? ??? ?<!-- 編碼 --> ? ? ?? ??? ?<property name="defaultEncoding" value="utf-8"></property> ? ? ?? ??? ?<!-- 可選的配置 --> ? ? ?? ??? ?<property name="freemarkerSettings"> ? ? ? ? ? <props> ?? ??? ??? ??? ?<prop key="template_update_delay">10</prop> ?? ??? ??? ??? ?<prop key="locale">zh_CN</prop> ?? ??? ??? ??? ?<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop> ?? ??? ??? ??? ?<prop key="date_format">yyyy-MM-dd</prop> ?? ??? ??? ??? ?<prop key="time_format">HH:mm:ss</prop> ?? ??? ??? ??? ?<!-- 頁面數(shù)值的顯示格式 --> ?? ??? ??? ??? ?<prop key="number_format">#.##</prop><!-- 88,282,882,888,888 --><!-- 88282882888888.00 ?--> ?? ??? ??? ?</props> ? ? ? ? ? ?</property> ? ? ?</bean> ? ? ? ? ? ?<!-- freemarker的解析器 --> ? ? ?<bean id="freeMarkerViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> ? ? ??? ?<!-- 后綴 ? ? ??? ?.ftl:是freemarker模板文件的后綴 ? ? ??? ? --> ? ? ??? ? ?<property name="suffix" value=".ftl"></property> ? ? ??? ? ?<property name="contentType" value="text/html;charset=utf-8"></property> ? ? ??? ? ?<!-- 方便頁面獲得項(xiàng)目的絕對路徑 --> ? ? ??? ? ?<property name="requestContextAttribute" value="request"></property> ? ? ?</bean>
然后是controller的核心代碼
@RequestMapping("/getHtml") ?? ?public String getHtml(HttpServletRequest request,HttpServletResponse response) throws Exception{ ?? ??? ?//第一步 freemarkerConfigurer得到一個(gè)Configure對象 ?? ??? ?Configuration configuration = freeMarkerConfigurer.getConfiguration(); ?? ??? ?//第二步 得到一個(gè)模版文件 ?? ??? ?Template template = configuration.getTemplate("index.ftl"); ?? ? ? ?//第三步 構(gòu)建數(shù)據(jù)模型 ?? ??? ?Map<String, Object> map = new HashMap<String, Object>(); ?? ??? ?map.put("uname", "zhangsan"); ?? ??? ?map.put("bookList", BookDaoImpl.getBookList()); ?? ??? ?System.out.println(BookDaoImpl.getBookList().get(0).getAuthor()); ?? ??? ?//第四步 指定一個(gè)文件夾 構(gòu)建一個(gè)輸出流 ?? ??? ?String dir = request.getSession().getServletContext().getRealPath("/WEB-INF/"); ?? ??? ?//PrintWriter printWriter = new PrintWriter(new FileWriter(new File(dir,"index.html"))); ?? ??? ?System.out.println(dir); ?? ??? ?//第五步 數(shù)據(jù)模型+模版文件 = 輸出(控制臺輸出,html文件) ?? ??? ?template.process(map, printWriter); ?? ??? ?printWriter.flush(); ?? ??? ?return "success"; ?? ?}
最后頁面提示成功生成html頁面
但在進(jìn)入生成的html頁面時(shí)發(fā)生了亂碼
在網(wǎng)上也查了下大致給了以下幾種解決方案
首先是說ftl文件的head上加上
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
因?yàn)槲以趕pringmvc的視圖解析器配置了
<property name="contentType" value="text/html;charset=utf-8"></property>
所以這個(gè)選擇首先pass掉,然后說是在controller里加上
configuration.setDefaultEncoding("UTF-8");
不過因?yàn)槲以趂reemarker的環(huán)境配置我也配置了默認(rèn)的編碼
<!-- 編碼 --> <property name="defaultEncoding" value="utf-8"></property>
所以應(yīng)該也不是這個(gè)原因,后來我找到生成的html文件,發(fā)現(xiàn)用瀏覽器查看源代碼雖然會亂碼,但用記事本打開的時(shí)候所顯示并沒有亂碼,然后判斷是輸出流的問題,通過網(wǎng)上查找發(fā)現(xiàn)FileWriter和FileReader使用的是系統(tǒng)默認(rèn)的編碼方式,因?yàn)閒ileWriter本身不具有用戶指定編碼的方式,這里選擇使用filewriter 的父類OutputStreamWriter來讀寫操作,把代碼
String dir = request.getSession().getServletContext().getRealPath("/WEB-INF/"); //PrintWriter printWriter = new PrintWriter(new FileWriter(new File(dir,"index.html")));
替換成
String dir = request.getSession().getServletContext().getRealPath("/WEB-INF/index.html"); ?? ??? ?OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(dir), "UTF-8"); ?? ??? ?PrintWriter printWriter = new PrintWriter(writer);
后啟動(dòng)程序
亂碼解決了,很開心!
freemarker頁面靜態(tài)化步驟以及相關(guān)注意事項(xiàng)
Freemarker
導(dǎo)入坐標(biāo)
<dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.23</version> </dependency>
創(chuàng)建模板文件
<html> <head> <meta charset="utf-8"> <title>Freemarker入門</title> </head> <body> <#--我只是一個(gè)注釋,我不會有任何輸出 --> ${name}你好,${message} </body> </html>
生成文件
public static void main(String[] args) throws Exception{ //1.創(chuàng)建配置類 Configuration configuration=new Configuration(Configuration.getVersion()); //2.設(shè)置模板所在的目錄 configuration.setDirectoryForTemplateLoading(new File("D:\\ftl")); //3.設(shè)置字符集,讀取文件的編碼 configuration.setDefaultEncoding("utf-8"); //4.加載模板 Template template = configuration.getTemplate("test.ftl"); //5.創(chuàng)建數(shù)據(jù)模型 Map map=new HashMap(); map.put("name", "張三"); map.put("message", "歡迎來到中國!"); //6.創(chuàng)建Writer對象 // ? // 指定輸出編碼格式 utf-8 ? ? ? ? Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream ("d:\\ftl\\test.html"),"UTF-8")); //Writer out =new FileWriter(new File("d:\\test.flt")); //7.輸出 template.process(map, out); //8.關(guān)閉Writer對象 out.close(); }
例子
分析
前面我們已經(jīng)學(xué)習(xí)了Freemarker的基本使用方法,下面我們就可以將Freemarker應(yīng)用到項(xiàng)目中,幫我們生成移動(dòng)端套餐列表靜態(tài)頁面和套餐詳情靜態(tài)頁面。
接下來我們需要思考幾個(gè)問題:
(0)那些頁面應(yīng)該靜態(tài)化? 數(shù)據(jù)不經(jīng)常發(fā)生變化,訪問量大的
(1)什么時(shí)候生成靜態(tài)頁面比較合適呢?
(2)將靜態(tài)頁面生成到什么位置呢?
(3)應(yīng)該生成幾個(gè)靜態(tài)頁面呢?
- 對于第一個(gè)問題,應(yīng)該是當(dāng)套餐數(shù)據(jù)發(fā)生改變時(shí),需要生成靜態(tài)頁面,即我們通過后臺系統(tǒng)修改套餐數(shù)據(jù)(包括新增、刪除、編輯)時(shí)。
- 對于第二個(gè)問題,如果是在開發(fā)階段可以將文件生成到項(xiàng)目工程中,如果上線后可以將文件生成到移動(dòng)端系統(tǒng)運(yùn)行的tomcat中。
- 對于第三個(gè)問題,套餐列表只需要一個(gè)頁面就可以了,在這個(gè)頁面中展示所有的套餐列表數(shù)據(jù)即可。套餐詳情頁面需要有多個(gè),即一個(gè)套餐應(yīng)該對應(yīng)一個(gè)靜態(tài)頁面。
模板
mobile_setmeal.ftl
<!DOCTYPE html> <html lang="zh-CN"> <head> ? ? <meta charset="utf-8"> ? ? <meta http-equiv="X-UA-Compatible" content="IE=edge"> ? ? <!-- 上述3個(gè)meta標(biāo)簽*必須*放在最前面,任何其他內(nèi)容都*必須*跟隨其后! --> ? ? <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0,user-scalable=no,minimal-ui"> ? ? <meta name="description" content=""> ? ? <meta name="author" content=""> ? ? <link rel="icon" href="../img/asset-favico.ico" rel="external nofollow" rel="external nofollow" > ? ? <title>預(yù)約</title> ? ? <link rel="stylesheet" href="../css/page-health-order.css" rel="external nofollow" /> </head> <body data-spy="scroll" data-target="#myNavbar" data-offset="150"> <div class="app" id="app"> ? ? <!-- 頁面頭部 --> ? ? <div class="top-header"> ? ? ? ? <span class="f-left"><i class="icon-back" onclick="history.go(-1)"></i></span> ? ? ? ? <span class="center">大鵝健康</span> ? ? ? ? <span class="f-right"><i class="icon-more"></i></span> ? ? </div> ? ? <!-- 頁面內(nèi)容 --> ? ? <div class="contentBox"> ? ? ? ? <div class="list-column1"> ? ? ? ? ? ? <ul class="list"> ? ? ? ? ? ? ? ? <#list setmealList as setmeal> ? ? ? ? ? ? ? ? ? ? <li class="list-item"> ? ? ? ? ? ? ? ? ? ? ? ? <a class="link-page" href="setmeal_detail_${setmeal.id}.html" rel="external nofollow" > ? ? ? ? ? ? ? ? ? ? ? ? ? ? <img class="img-object f-left" ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?src="http://py25jppgz.bkt.clouddn.com/${setmeal.img}" ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?alt=""> ? ? ? ? ? ? ? ? ? ? ? ? ? ? <div class="item-body"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <h4 class="ellipsis item-title">${setmeal.name}</h4> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <p class="ellipsis-more item-desc">${setmeal.remark}</p> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <p class="item-keywords"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <span> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <#if setmeal.sex == '0'> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 性別不限 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <#else> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <#if setmeal.sex == '1'> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 男 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <#else> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 女 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? </#if> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? </#if> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? </span> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <span>${setmeal.age}</span> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? </p> ? ? ? ? ? ? ? ? ? ? ? ? ? ? </div> ? ? ? ? ? ? ? ? ? ? ? ? </a> ? ? ? ? ? ? ? ? ? ? </li> ? ? ? ? ? ? ? ? </#list> ? ? ? ? ? ? </ul> ? ? ? ? </div> ? ? </div> </div> <!-- 頁面 css js --> <script src="../plugins/vue/vue.js"></script> <script src="../plugins/vue/axios-0.18.0.js"></script> </body>
模板
<!DOCTYPE html> <html lang="zh-CN"> <head> ? ? <meta charset="utf-8"> ? ? <meta http-equiv="X-UA-Compatible" content="IE=edge"> ? ? <!-- 上述3個(gè)meta標(biāo)簽*必須*放在最前面,任何其他內(nèi)容都*必須*跟隨其后! --> ? ? <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0,user-scalable=no,minimal-ui"> ? ? <meta name="description" content=""> ? ? <meta name="author" content=""> ? ? <link rel="icon" href="../img/asset-favico.ico" rel="external nofollow" rel="external nofollow" > ? ? <title>預(yù)約詳情</title> ? ? <link rel="stylesheet" href="../css/page-health-orderDetail.css" rel="external nofollow" /> ? ? <script src="../plugins/vue/vue.js"></script> ? ? <script src="../plugins/vue/axios-0.18.0.js"></script> ? ? <script src="../plugins/healthmobile.js"></script> </head> <body data-spy="scroll" data-target="#myNavbar" data-offset="150"> <div id="app" class="app"> ? ? <!-- 頁面頭部 --> ? ? <div class="top-header"> ? ? ? ? <span class="f-left"><i class="icon-back" onclick="history.go(-1)"></i></span> ? ? ? ? <span class="center">大鵝健康</span> ? ? ? ? <span class="f-right"><i class="icon-more"></i></span> ? ? </div> ? ? <!-- 頁面內(nèi)容 --> ? ? <div class="contentBox"> ? ? ? ? <div class="card"> ? ? ? ? ? ? <div class="project-img"> ? ? ? ? ? ? ? ? <img src="http://py25jppgz.bkt.clouddn.com/${setmeal.img}" ? ? ? ? ? ? ? ? ? ? ?width="100%" height="100%" /> ? ? ? ? ? ? </div> ? ? ? ? ? ? <div class="project-text"> ? ? ? ? ? ? ? ? <h4 class="tit">${setmeal.name}</h4> ? ? ? ? ? ? ? ? <p class="subtit">${setmeal.remark}</p> ? ? ? ? ? ? ? ? <p class="keywords"> ? ? ? ? ? ? ? ? ? ? <span> ?? ??? ??? ??? ??? ??? ?<#if setmeal.sex == '0'> ?? ??? ??? ??? ??? ??? ??? ?性別不限 ? ? ? ? ? ? ? ? ? ? ? ? <#else> ? ? ? ? ? ? ? ? ? ? ? ? ? ? <#if setmeal.sex == '1'> ?? ??? ??? ??? ??? ??? ??? ??? ?男 ? ? ? ? ? ? ? ? ? ? ? ? ? ? <#else> ?? ??? ??? ??? ??? ??? ??? ??? ?女 ? ? ? ? ? ? ? ? ? ? ? ? ? ? </#if> ? ? ? ? ? ? ? ? ? ? ? ? </#if> ? ? ? ? ? ? ? ? ? ? </span> ? ? ? ? ? ? ? ? ? ? <span>${setmeal.age}</span> ? ? ? ? ? ? ? ? </p> ? ? ? ? ? ? </div> ? ? ? ? </div> ? ? ? ? <div class="table-listbox"> ? ? ? ? ? ? <div class="box-title"> ? ? ? ? ? ? ? ? <i class="icon-zhen"><span class="path1"></span><span class="path2"></span></i> ? ? ? ? ? ? ? ? <span>套餐詳情</span> ? ? ? ? ? ? </div> ? ? ? ? ? ? <div class="box-table"> ? ? ? ? ? ? ? ? <div class="table-title"> ? ? ? ? ? ? ? ? ? ? <div class="tit-item flex2">項(xiàng)目名稱</div> ? ? ? ? ? ? ? ? ? ? <div class="tit-item ?flex3">項(xiàng)目內(nèi)容</div> ? ? ? ? ? ? ? ? ? ? <div class="tit-item ?flex3">項(xiàng)目解讀</div> ? ? ? ? ? ? ? ? </div> ? ? ? ? ? ? ? ? <div class="table-content"> ? ? ? ? ? ? ? ? ? ? <ul class="table-list"> ?? ??? ??? ??? ??? ??? ?<#list setmeal.checkGroups as checkgroup> ? ? ? ? ? ? ? ? ? ? ? ? ? ? <li class="table-item"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <div class="item flex2">${checkgroup.name}</div> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <div class="item flex3"> ?? ??? ??? ??? ??? ??? ??? ??? ??? ?<#list checkgroup.checkItems as checkitem> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <label> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ${checkitem.name} ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? </label> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? </#list> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? </div> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <div class="item flex3">${checkgroup.remark}</div> ? ? ? ? ? ? ? ? ? ? ? ? ? ? </li> ? ? ? ? ? ? ? ? ? ? ? ? </#list> ? ? ? ? ? ? ? ? ? ? </ul> ? ? ? ? ? ? ? ? </div> ? ? ? ? ? ? ? ? <div class="box-button"> ? ? ? ? ? ? ? ? ? ? <a @click="toOrderInfo()" class="order-btn">立即預(yù)約</a> ? ? ? ? ? ? ? ? </div> ? ? ? ? ? ? </div> ? ? ? ? </div> ? ? </div> </div> <script> ? ? var vue = new Vue({ ? ? ? ? el:'#app', ? ? ? ? methods:{ ? ? ? ? ? ? toOrderInfo(){ ? ? ? ? ? ? ? ? window.location.href = "orderInfo.html?id=${setmeal.id}"; ? ? ? ? ? ? } ? ? ? ? } ? ? }); </script> </body>
配置文件
(1)在health_service_provider工程中創(chuàng)建屬性文件freemarker.properties 通過上面的配置可以指定將靜態(tài)HTML頁面生成的目錄位置
out_put_path=靜態(tài)頁面生成的位置
在spring的中進(jìn)行配置
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <!--指定模板文件所在目錄--> <property name="templateLoaderPath" value="/WEB-INF/ftl/" /> <!--指定字符集--> <property name="defaultEncoding" value="UTF-8" /> </bean> <context:property-placeholder location="classpath:freemarker.properties"/>
13 java 代碼
? ?@Autowired ? ? private SetmealDao setmealDao; ? ? @Autowired ? ? private JedisPool jedisPool; ? ? @Autowired ? ? private CheckGroupDao checkGroupDao; ? ? @Autowired ? ? private CheckItemDao checkItemDao; ? ? @Autowired ? ? private FreeMarkerConfigurer freeMarkerConfigurer; ? ? @Value("${out_put_path}") ? ? private String outPutPath;//從屬性文件中讀取要生成的html對應(yīng)的目錄 //新增套餐,同時(shí)關(guān)聯(lián)檢查組 ? ? public void add(Setmeal setmeal, Integer[] checkgroupIds) { ? ? ? ? setmealDao.add(setmeal); ? ? ? ? Integer setmealId = setmeal.getId();//獲取套餐id ? ? ? ? this.setSetmealAndCheckGroup(setmealId,checkgroupIds); ? ? ? ? //完成數(shù)據(jù)庫操作后需要將圖片名稱保存到redis ? ? ? ? jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg()); ? ? ? ? //當(dāng)添加套餐后需要重新生成靜態(tài)頁面(套餐列表頁面、套餐詳情頁面) ? ? ? ? generateMobileStaticHtml(); ? ? } ? ? //生成當(dāng)前方法所需的靜態(tài)頁面 ? ? public void generateMobileStaticHtml(){ ? ? ? ? //在生成靜態(tài)頁面之前需要查詢數(shù)據(jù) ? ? ? ? List<Setmeal> list = setmealDao.findAll(); ? ? ? ? //需要生成套餐列表靜態(tài)頁面 ? ? ? ? generateMobileSetmealListHtml(list); ? ? ? ? //需要生成套餐詳情靜態(tài)頁面 ? ? ? ? generateMobileSetmealDetailHtml(list); ? ? } ? ? //生成套餐列表靜態(tài)頁面 ? ? public void generateMobileSetmealListHtml(List<Setmeal> list){ ? ? ? ? Map map = new HashMap(); ? ? ? ? //為模板提供數(shù)據(jù),用于生成靜態(tài)頁面 ? ? ? ? map.put("setmealList",list); ? ? ? ? generteHtml("mobile_setmeal.ftl","m_setmeal.html",map); ? ? } ? ? //生成套餐詳情靜態(tài)頁面(可能有多個(gè)) ? ? public void generateMobileSetmealDetailHtml(List<Setmeal> list){ ? ? ? ? for (Setmeal setmeal : list) { ? ? ? ? ? ? Map map = new HashMap(); ? ? ? ? ? ? map.put("setmeal",setmealDao.findById4Detail(setmeal.getId())); ? ? ? ? ? ? generteHtml("mobile_setmeal_detail.ftl","setmeal_detail_" + setmeal.getId() + ".html",map); ? ? ? ? } ? ? } ? ? //通用的方法,用于生成靜態(tài)頁面 ? ? public void generteHtml(String templateName,String htmlPageName,Map map){ ? ? ? ? Configuration configuration = freeMarkerConfigurer.getConfiguration();//獲得配置對象 ? ? ? ? Writer out = null; ? ? ? ? try { ? ? ? ? ? ? Template template = configuration.getTemplate(templateName); ? ? ? ? ? ? //構(gòu)造輸出流 ? ? ? ? ? ? // 中文亂碼 ? ? ? ? ? ? ? //out = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (outPutPath + "/" + htmlPageName),"UTF-8")); ? ? ? ? ? ?//構(gòu)造輸出流 ? ? ? ? ? ? out = new FileWriter(new File(outPutPath + "/" + htmlPageName)); ? ? ? ? ? ? //輸出文件 ? ? ? ? ? ? template.process(map,out); ? ? ? ? ? ? out.close(); ? ? ? ? } catch (Exception e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? }
生成靜態(tài)頁面的通用方法
? ? //通用的方法,用于生成靜態(tài)頁面(參數(shù):templateName:模板,htmlPageName:生成的文件名稱,Map:數(shù)據(jù)) ? ? public void generteHtml(String templateName,String htmlPageName,Map map){ ? ? ? ? Configuration configuration = freeMarkerConfigurer.getConfiguration();//獲得配置對象 ? ? ? ? Writer out = null; ? ? ? ? try { ? ? ? ? ? ? Template template = configuration.getTemplate(templateName); ? ? ? ? ? ? //構(gòu)造輸出流 ? ? ? ? ? ? // 中文亂碼 ? ? ? ? ? ? ? //out = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (outPutPath + "/" + htmlPageName),"UTF-8")); ? ? ? ? ? ?//構(gòu)造輸出流 ? ? ? ? ? ? out = new FileWriter(new File(outPutPath + "/" + htmlPageName)); ? ? ? ? ? ? //輸出文件 ? ? ? ? ? ? template.process(map,out); ? ? ? ? ? ? out.close(); ? ? ? ? } catch (Exception e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? }
14 -測試
? public void genById(Integer setmealId){ ? ? ? ? Map map = new HashMap(); ? ? ? ? map.put("setmeal",setmealDao.findById4Detail(setmealId)); ? ? ? ? generteHtml("mobile_setmeal_detail.ftl","setmeal_detail_" + setmealId + ".html",map); ? ? }
總結(jié)
1導(dǎo)入寫好的模板文件(放置到服務(wù)的提供方的WEB-INF下創(chuàng)建的ftl文件夾中)
2配置freemarker的靜態(tài)頁面生成的地方,在spring的配置文件中配置freemarker的相關(guān)bean的配置.
3判斷在什么情況下生成靜態(tài)頁面比較合適,一般為數(shù)據(jù)改變的是時(shí)候生成(例如:信息的增加,修改,刪除)并修改相關(guān)的實(shí)現(xiàn)方法.
4.運(yùn)行程序,增加,修改,刪除信息,調(diào)用靜態(tài)化方法進(jìn)行頁面生成.(基本流程:增加,修改,刪除信息–>查詢數(shù)據(jù)庫獲取到相應(yīng)的數(shù)據(jù)–>調(diào)用生成靜態(tài)頁面的通用方法進(jìn)行頁面的生成).
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
MyBatis-Plus 之selectMaps、selectObjs、selectCount、selectO
本文主要介紹了MyBatis-Plus 之selectMaps、selectObjs、selectCount、selectOne的使用,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03Java錯(cuò)誤:進(jìn)行語法分析時(shí)已到達(dá)文件結(jié)尾的解決
這篇文章主要介紹了Java錯(cuò)誤:進(jìn)行語法分析時(shí)已到達(dá)文件結(jié)尾的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08springboot如何獲取request請求的原始url與post參數(shù)
這篇文章主要介紹了springboot如何獲取request請求的原始url與post參數(shù)問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12詳解Java并發(fā)編程基礎(chǔ)之volatile
volatile作為Java多線程中輕量級的同步措施,保證了多線程環(huán)境中“共享變量”的可見性。這里的可見性簡單而言可以理解為當(dāng)一個(gè)線程修改了一個(gè)共享變量的時(shí)候,另外的線程能夠讀到這個(gè)修改的值。本文將詳解介紹Java并發(fā)編程基礎(chǔ)之volatile2021-06-06Java實(shí)現(xiàn)定時(shí)任務(wù)最簡單的3種方法
幾乎在所有的項(xiàng)目中,定時(shí)任務(wù)的使用都是不可或缺的,如果使用不當(dāng)甚至?xí)斐少Y損,下面這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)定時(shí)任務(wù)最簡單的3種方法,本文通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06