0

平台:JBOSS 5.x,Struts2,JSP 我在 gui 上显示一个 csv 文件。要做到这一点 1. 首先我调用 TestAction 类的 showFileData 函数。2. showFileData 函数将控制权转发给'Test.jsp' 类。我成功地能够在 gui 上显示 csv 文件。但是如果行之间有空格,那么我无法在 gui 上显示空格。

测试动作.java

package cdot.oss.cmsat.importMap.struts;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Map;
import org.apache.struts.util.LabelValueBean;
import com.opensymphony.xwork2.ActionContext;

public class TestAction 
{
public String showFileData() throws Exception 
{       
    Map session = (Map)ActionContext.getContext().getSession();

    String filePath=System.getProperty("jboss.server.home.dir")+File.separator+"x.csv";
    System.out.println("[][]filePath:*"+filePath);

    File drs = new File(filePath);
    ArrayList<String> fileRecords=new ArrayList();
    ArrayList<LabelValueBean> Recordnum=new ArrayList();

    try{

        if(drs.isFile())
        {
        FileInputStream fstream = new FileInputStream(filePath);

        BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
        String strLine;
        int lines = 1;
        /*br.readLine() shall first line of file, and it shall assigned to strLine variable
         * first 10 lines of file shall be added to fileRecords arrayList*/
        //System.out.println("[][] br.readLine() "+br.readLine());
        while ((strLine = br.readLine()) != null)   
        {
            System.out.println("[][] strLine:"+strLine+":"+" length "+strLine.length());
            fileRecords.add(strLine);
            Recordnum.add(new LabelValueBean(Integer.toString(lines),Integer.toString(lines)));
            if(lines==10)
            {
                break;
            }
            lines++;
        }

        br.close();
        in.close();
        fstream.close();
        session.put("RecordNum",Recordnum);

        }
        else
            fileRecords.add("Not a valid file");
    }

    catch(Exception e){
        System.out.println("[][]exception");
        fileRecords.add("Not a valid file");

    }
    System.out.println("[][] fileRecords.size() "+fileRecords.size()+" fileRecords "+fileRecords);
    session.put("FileRecords",fileRecords);
    return "ForwardToJsp";
}

}

测试.jsp

<html>
<head>
<%@ taglib prefix="s" uri="/struts-tags" %>
<s:head />
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<s:head />

<title>Test</title>
    </head>
    <body>
    <center>
    <br>
    <br></br>
    <div style="width: 1200px;height: auto;border: 2px solid steelblue">
    <s:form name="Test" enctype="multipart/form-data">
    <br></br>
    <s:if test="%{#session.FileRecords}">
    <table border="0" cellpadding="5" cellspacing="5" width="80%" class="input-table" >
    <tr>
    <td align="center">
    <b> First ten rows of file are :  </b>
    <br></br>
    </td>
    </tr>
    <tr>
    <td align="left">
    <div style="width: 950px;height: 100px;background-color: #FFFFFF;border: 1px solid steelblue;overflow: auto;">
    <dl>
    <s:iterator value="#session.FileRecords">
    <dt class="C_normal"><s:property/></dt>
    </s:iterator>
    </dl>
    </div>
    </td>
    </tr>
    </table>
    <br/>
    <br></br>
    </s:if>
    </s:form>
    </div>
    </center>
    </body>
    </html>

例如。x.csv

a,b,c,d

e,f,g,h

i,j,k,l

m,n,o,p

q,r,s,t

u,v,w,x

y,z,ab,cd

和上面的文件一样,行之间有空格,但是在 gui 上它们没有显示?我怎样才能准确地向 gui 显示前 10 行?

4

0 回答 0