今天就跟大家聊聊有關WebWork實現數據怎么導出CSV文本,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
以往,在WebWork中實現導出CSV文本時,采用的是之前自己實現的csv result,但是這個實現有個缺陷就是對象嵌套不支持,而且需要構造dto遍歷獲取列表進行列表字段顯示的封裝,且在WebWork配置文件要羅列出需要導出的字段,過于繁瑣。
于是產生了采用直接用freemarker作為導出CSV文本文件模板的想法,這樣后臺只做查詢列表數據的獲取,WebWork配置文件只需要定位到指定freemarker模板,而且導出排版可以在freemarker中更靈活的得到控制。
其中,WebWork在導出CSV文本過程中,中文亂碼問題上尤其需要注意的是,一定要設置模板文件的編碼。
以下是實現實例:
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獲取顯示內容方法。
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〉看完上述內容,你們對WebWork實現數據怎么導出CSV文本有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。