日期:2014-05-16  浏览次数:20716 次

struts2 ajax jquery 实现 export/download 文件
前提, 用struts-jquery-grid 生成的表单,然后导出所选的记录。

第一部分(jsp)

<
script type="text/javascript">

$.subscribe(
'beforeLink', function(event,data) {

var fData =$('#gridmultitable').jqGrid('getGridParam','selarrrow');

$(
'#select').attr('value',fData);

});

</
script>

</
head>

<
title>

Users

</
title></head>

<
body>

.......中间漏去了 表单部分

<sjg:gridColumn name="id" index="id" title="ID" hidden="true"/>

</sjg:grid>

<br/>

</
div>

<s:form id="download" action="download" >

<div id="text">

<s:hidden id="select" label="SelectIds" name="selectIds" value="wiow"/>

</div>

</s:form>

<div class="type-button">

<sj:a

id="ajaxformlink"

formIds="download"

indicator="indicator"

onClickTopics="beforeLink"

button="true"

>

Export Record

</sj:a>

</div>

 


第 二部分struts.xml

<!-- download simple-->

<action name="download" class="com.pulse.ipmanager.action.admin.FileDownloadAction">

<result name="success" type="stream">

<param name="contentType">application/octet-stream;charset=ISO8859-1</param>

<param name="inputName">inputStream</param>

<param name="contentDisposition">attachment;filename="struts2.txt"</param>

<param name="bufferSize">4096</param>

</result>

</action>

 


第三部分 action

package
com.XXX;

import
java.io.ByteArrayInputStream;

import
java.io.InputStream;

import
java.util.ArrayList;

import
java.util.Collection;

import
java.util.List;

import
java.util.Map;

 

import
org.apache.commons.collections.CollectionUtils;

import
org.apache.struts2.interceptor.SessionAware;

import
com.opensymphony.xwork2.Action;

import
com.opensymphony.xwork2.ActionSupport;

import
com.XXX.WebUserPageBean;

public
class FileDownloadAction extends ActionSupport implements Action, SessionAware{

/**

*

*/

private static final long serialVersionUID = 1L;

private Map<String, Object> session;

private String selectIds ;

private InputStream inputStream;

public InputStream getInputStream() throws Exception {

return inputStream;

}

public String execute() throws Exception {

List<WebUserPageBean> li=(List)
session.get("mylist");

List<WebUserPageBean> reli=
new ArrayList<WebUserPageBean>();

WebUserPageBean webuserTemp;

StringBuffer exportdata=
new StringBuffer();

if (this.selectIds!=null && !this.selectIds.trim().equals("")) {

String [] str=
selectIds.split(",");

for (int i = 0; i < str.length; i++ ){

for (int j = 0; j< li.size() ; j++){

webuserTemp= (WebUserPageBean) li.get(j);

String id=webuserTemp.getId();

if (str[i].trim().equalsIgnoreCase(id)){

reli.add(webuserTemp);

break;

}

}

}

// instead of doing the CSV transformation here, we can declare a custom CvsTransfermer

@SuppressWarnings("unchecked")

Collection<String> csvRows = CollectionUtils.collect(reli,
new CvsTransfermer());

for(String s : csvRows) {

exportdata.append(s);

}

}

inputStream = new ByteArrayInputStream(exportdata.toString().getBytes());

return SUCCESS;

}

public void setSession(Map<String, Object> session)

{

this.session = session;

}

public void setSelectIds(String selectIds){

this.selectIds=selectIds;

}

public String getSelectIds(){

return this.selectIds;

}

}


好了。

注意的是, sj:submit 标签必须targets 不为空。 所以必须用sj:a