You smiled and talked to me of nothing and I felt that for this I had been waiting long.

          The trees come up to my window like the yearning voice of the dumb earth.

          BlogJava 首頁(yè) 新隨筆 聯(lián)系 聚合 管理
            19 Posts :: 10 Stories :: 25 Comments :: 0 Trackbacks
          以往,在webwork中實(shí)現(xiàn)導(dǎo)出數(shù)據(jù)為csv文本時(shí),采用的是之前自己實(shí)現(xiàn)的csv result,但是這個(gè)實(shí)現(xiàn)有個(gè)缺陷就是對(duì)象嵌套不支持,而且需要構(gòu)造dto遍歷獲取列表進(jìn)行列表字段顯示的封裝,且在webwork配置文件要羅列出需要導(dǎo)出的字段,過(guò)于繁瑣。
          于是產(chǎn)生了采用直接用freemarker作為導(dǎo)出csv文件模板的想法,這樣后臺(tái)只做查詢(xún)列表數(shù)據(jù)的獲取,配置文件只需要定位到指定freemarker模板,而且導(dǎo)出排版可以在freemarker中更靈活的得到控制。
          其中,在中文亂碼問(wèn)題上尤其需要注意的是,一定要設(shè)置模板文件的編碼。
          以下是實(shí)現(xiàn)實(shí)例:

          1 
          2 public String sampelExport()throws Exception{
          3     samplelist = SampleService.getAll();
          4     return SUCCESS;
          5 }

          //samplelist的getter不要忘記哦。

          SampleObject具有name屬性,類(lèi)型為String,createDate屬性,類(lèi)型為java.util.date,emails屬性,類(lèi)型為java.util.list,為一個(gè)email封裝對(duì)象列表,status屬性,類(lèi)型為Enum,提供按locale獲取顯示內(nèi)容方法。

          1 <action name="sampleexport" method="sampleExport"
          2      class="org.hook.webwork.SampleExportAction">
          3      <result type="freemarker" name="success">
          4          template/default/sampleexport.ftl
          5      <result>
          6 </action>
          7 

          1 <#ftl encoding="GBK"> 
          2 <#if response?exists>${response.setHeader("Content-Disposition","attachment; filename=sample_export.csv")}${response.setHeader("Cache-  Control", "cache")}${response.setContentType("text/plain; charset=GBK")}${action.getText('org.hook.sampleobject.name')},
          ${action.getText('org.hook.sampleobject.createdate')},${action.getText('org.hook.sampleobject.emails')},
          ${action.getText('org.hook.sampleobject.status')}
          </#if>
          3 <#if samplelist?exists>
          4 <#list samplelist as sampleobject>"${sampleobject.name}","${sampleobject.createDate?date}","<#list sampleobject.emailss as email>${email.fullinfo} </#list>",<#if sampleobject.status?exists&&sampleobject.status.getValue(locale)?exists>
          ${sampleobject.status.getValue(locale)}</#if>
          5 </#list>
          6 </#if>
          注意:沒(méi)有行號(hào)的地方說(shuō)明代碼不要換行,要不導(dǎo)出的文件位置會(huì)有誤差。
          posted on 2008-11-07 10:41 鉤子 閱讀(1577) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): jeeopensources工作筆記
          主站蜘蛛池模板: 沽源县| 房产| 开阳县| 榆社县| 托克逊县| 荥经县| 金坛市| 拉萨市| 龙井市| 万州区| 南投市| 东港市| 沂南县| 双柏县| 巴楚县| 乌鲁木齐县| 阿勒泰市| 南宁市| 沁阳市| 晋江市| 杭州市| 牡丹江市| 黑龙江省| 沅陵县| 偏关县| 隆林| 临湘市| 涡阳县| 佛教| 宁乡县| 宣化县| 黄冈市| 大埔区| 蒙城县| 云龙县| 日喀则市| 淳安县| 金湖县| 铅山县| 延川县| 阳高县|