简体   繁体   中英

Export dynamic data from a JSP page to CSV in Struts2

I am trying to implement a function in which a user downloads a file in a CSV format. I have done the downloading part and I am able to convert the JSP to CSV format but the issue that I am facing is that I am unable to transfer the dynamic data from the JSP to CSV file. After downloading the file I see the JSP tags and all other contents of it instead I want to see the data only, not the JSP tags or forms etc.

Actions Class:

public String viewReport() throws Exception {
    boolean returnReport;

    String filePath = "fileName.jsp";

    File file = new File(filePath);
    inputStream = new FileInputStream(file);

    try {
        returnReport = validateRequest();
        if (returnReport) {
            setIntgList(generateViewIntegrationReportData(getESignUIConfig()));
        } else {
            failureResponse(msgs, 400);
            return null;
        }
    } catch (Exception e) {
        e.printStackTrace();
        msgs.add(new Message(ESignatureIntegrationMessageTypeEnum.MESSAGE_TYPE_ERROR, 
                    UiIntegrationKeyConstants.UI_INTEGRATION_ERROR_CODE_500, UiIntegrationKeyConstants.UI_INTEGRATION_ERROR_TEXT_SERVICE_ERROR));
        failureResponse(msgs, 500);
        return null;
    }

    return UiIntegrationKeyConstants.INTEGRATION_DETAILS_REPORT_REPSONSE;
}

JSP:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib uri="/WEB-INF/integration.tld" prefix="integration" %>

<link rel="stylesheet" type="text/css" href='<integration:file  name="integration.css" type="CSS" prefix="/esign/styles/"/>'/>
<script language="javascript" type="text/javascript" src="<integration:file  name="integration.js" type="JS" prefix="/esign/scripts/integration/"/>">
</script>
<h1><center>Transaction Report Page</center></h1>
<br></br>
<c:forEach items="${intgList}" var="list">
        <tr class="<%=count % 2 != 0 ? "odd" : "even"  %>">
            <td class="dataFieldCell1" align="center"><c:out  value="${list.lob}" /></td>
            <td class="dataFieldCell1" align="center"><c:out value="${list.insuredName}" /></td>
            <td class="dataFieldCell1" align="center"><c:out value="${list.custPhone}" /></td>
             <c:if test="${list.policyNbrLink eq true}">
                <td class="dataFieldCell1" align="center"><a href='#x' style="text-decoration:none" onclick="locateFunc('viewESignPolicyDetails',
                                                                                                                    {'agencyCode':'${list.agencyCode}',
                                                                                                                     'policyNumber':'${list.policyNumber}',
                                                                                                                     'policyState':'${list.policyState}',
                                                                                                                     'esignIdentifier':'${list.esignId}',
                                                                                                                     'esignVendorIdentifier':'${list.esignVendorIdentifier}',
                                                                                                                     'transId':'${list.transId}',
                                                                                                                     'lob':'${list.lob}',
                                                                                                                     'customerName':'${list.insuredName}',
                                                                                                                     'customerPhone':'${list.custPhone}',
                                                                                                                     'customerEmail':'${list.custEmail}',
                                                                                                                     'cretedDate':'${list.createdDate}'}
                                                                                                                    )"><c:out value="${list.policyNumber}"/></a></td>
            </c:if> 
            <c:if test="${list.policyNbrLink eq false}">
                <td class="dataFieldCell1" align="center"><c:out value="${list.policyNumber}"/></td>
            </c:if>
            <td class="dataFieldCell1" align="center"><c:out value="${list.createdDate}" /></td>
            <td class="dataFieldCellWrap" align="center"><c:out value="${list.custEmail}" /></td>
             <td class="dataFieldCell1" align="center"><a href='#x' onclick="locateFunc('viewESignDetails',
                                                                     {'url':'<integration:urlAction actionName="/integration/viewDetailsIntegration"><integration:urlParam key="esignIdentifier" value="${list.esignId}"/></integration:urlAction>',
                                                                     'agencyCode':'${list.agencyCode}',
                                                                     'policyNumber':'${list.policyNumber}',
                                                                     'policyState':'${list.policyState}',
                                                                     'esignIdentifier':'${list.esignId}',
                                                                     'esignVendorIdentifier':'${list.esignVendorIdentifier}',
                                                                     'lob':'${list.lob}',
                                                                     'transId':'${list.transId}',
                                                                     'customerName':'${list.insuredName}',
                                                                     'customerPhone':'${list.custPhone}',
                                                                     'customerEmail':'${list.custEmail}',
                                                                     'cretedDate':'${list.createdDate}'}
                                                                    )"><c:out value="${list.esignNumDocs}"/></a></td> 
        </tr>
        <%count++;%>
    </c:forEach> 

Struts.xml

<action name="*Integration" method="{1}" class="classPath">
        <result name="success" type="tiles">integrationView</result> 
        <result name="integrationDetailsReportResponse" type="stream">
            <param name="contentType">application/octet-stream</param>
            <param name="inputName">inputStream</param>
            <param name="contentDisposition">attachment;filename="integrationReportDetailsView.csv"</param>
            <param name="bufferSize">1024</param>
        </result>   
</action>

There are some similar questions but they are confusing and I was unable to relate them to my need so please don't duplicate it.

Maybe I misunderstood, but what you are trying to do is not possible.

You put your fileName.jsp into a InputStream and download it as integrationReportDetailsView.csv? Of course you cant get the dinamic content of the jsp, what you are doing is similar to download the file using a FTP server for example.

The dinamic content of the jsp is created by the server when you make a request but you aren't calling to the jsp, you are calling to an action that downloads the binary data of the file and sending it as an attachment. The server never processes it so the jsp tags are not evaluated and the dinamic content is not created.

I don't know why no one answered this question and I think the question wasn't that hard. So in order to get the dynamic data from a JSP to a CSV file format we need to Serialize our Java Object to CSV format. There are some libraries that can used to achieve this goal. I used OpenSCV and passed my POJO into it. Now when I click on the button, i can not only download the file but I am also getting the data from JSP to a CSV . What I was missing was the Serializing part of Object.

This link may help someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM