以往,在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文本
                
                文章路径:http://www.csdahua.cn/qtweb/news4/455354.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网