溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

WebWork實(shí)現(xiàn)數(shù)據(jù)怎么導(dǎo)出CSV文本

發(fā)布時間:2021-12-18 15:49:54 來源:億速云 閱讀:130 作者:柒染 欄目:編程語言

今天就跟大家聊聊有關(guān)WebWork實(shí)現(xiàn)數(shù)據(jù)怎么導(dǎo)出CSV文本,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

以往,在WebWork中實(shí)現(xiàn)導(dǎo)出CSV文本時,采用的是之前自己實(shí)現(xiàn)的csv result,但是這個實(shí)現(xiàn)有個缺陷就是對象嵌套不支持,而且需要構(gòu)造dto遍歷獲取列表進(jìn)行列表字段顯示的封裝,且在WebWork配置文件要羅列出需要導(dǎo)出的字段,過于繁瑣。

于是產(chǎn)生了采用直接用freemarker作為導(dǎo)出CSV文本文件模板的想法,這樣后臺只做查詢列表數(shù)據(jù)的獲取,WebWork配置文件只需要定位到指定freemarker模板,而且導(dǎo)出排版可以在freemarker中更靈活的得到控制。

其中,WebWork在導(dǎo)出CSV文本過程中,中文亂碼問題上尤其需要注意的是,一定要設(shè)置模板文件的編碼。

以下是實(shí)現(xiàn)實(shí)例:

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

//samplelist的getter不要忘記哦。

SampleObject具有name屬性,類型為String,createDate屬性,類型為java.util.date,emails屬性,類型為java.util.list,為一個email封裝對象列表,status屬性,類型為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〉
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〉

看完上述內(nèi)容,你們對WebWork實(shí)現(xiàn)數(shù)據(jù)怎么導(dǎo)出CSV文本有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI