2

我想显示显示 Glassfish 日志文件内容的 JSF 页面。到目前为止,我只做了这个:

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.Serializable;
    import javax.annotation.PostConstruct;
    import javax.faces.bean.ViewScoped;
    import javax.faces.context.FacesContext;
    import javax.inject.Named;

    @Named("GlassfishLogFileController")
    @ViewScoped
    public class GlassfishLogFile implements Serializable
    {

        String id;
        String fileData;

        // Constructor
        public GlassfishLogFile()
        {
            // Get the ID value 
            try
            {
                this.id = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id");
            }
            catch (Exception e)
            {
                this.id = null;
            }
        }
        // Path for Glassfish directory with log files
        String path = "/opt/glassfish3/glassfish/domains/domain1/logs/" + id;

        @PostConstruct
        public void redFile()
        {
            File file = new File(path);

            try (FileInputStream fis = new FileInputStream(file))
            {

                int content;
                while ((content = fis.read()) != -1)
                {
                    // convert to char and display it
                    fileData = fileData + (char)content;
                }

            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }

        public String getdata()
        {
            return fileData;
        }
    }

看法:

<div id="settingsdiv" style="width:350px; height:400px; position:absolute;  background-color:r; top:20px; left:1px">

    <div id="settingsHashMap" style="width:1050px; height:400px; position:absolute;  background-color:r; top:20px; left:1px">

        <h:form id="form" >
            <p:growl id="growl" showDetail="true" sticky="true" />

            <h:inputTextarea rows="30" cols="50" value="#{GlassfishLogFileController.data}" />                            

        </h:form>                    
    </div> 

</div>       

我很感兴趣是否有任何可能的方法来实现延迟加载到我将用来显示文本文件内容的文本区域。我想这样做是因为我想节省内存。日志文件有时会非常消耗内存。

而且我还发现了许多如何将 .pdf 文档显示到 JSF 页面的示例,但没有一个示例如何将文本文档显示到文本区域中。

最良好的祝愿

PS我更新了代码。现在我得到空的输入字段。不显示日志文件中的数据。

4

1 回答 1

2

试试这个,我刚刚测试过:

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

@Named("GlassfishLogFileController")
@ViewScoped
public class GlassfishLogFile implements Serializable {

String data;

String id;

private int offset = 0;
private int pageSize = 30;
// Path for Glassfish directory with log files
String path = "/opt/glassfish3/glassfish/domains/domain1/logs/" + this.id;

// Constructor
public GlassfishLogFile() {
// Get the ID value
try {
    this.id = FacesContext.getCurrentInstance().getExternalContext()
            .getRequestParameterMap().get("id");
    // We load the first page initially!
    this.actionNextPage();
} catch (Exception e) {
    this.id = null;
}
}

public String actionClearOffset() {
this.offset = 0;
this.actionNextPage();
return "SUCCESS";
}

public String actionNextPage() {
StringBuilder page = new StringBuilder();

for (int i = 0; i < this.pageSize; i++) {
    String line = this.readLine(this.offset);
    if (line == null) {
    break;
    }
    System.out.println(this.offset);
    this.offset += line.length() + 2;
    page.append(line).append(System.getProperty("line.separator"));
}
this.data = page.toString();
return "SUCCESS";
}

public String getData() {
return this.data;
}

public int getPageSize() {
return this.pageSize;
}

public String readLine(long offset) {
String strLine = null;
DataInputStream in = null;
try {
    // Open the file that is the first
    // command line parameter
    FileInputStream fstream = new FileInputStream(new File(this.path));
    // Get the object of DataInputStream
    in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));

    br.skip(offset);

    strLine = br.readLine();
    // Close the input stream
} catch (Exception e) {// Catch exception if any
    System.err.println("Error: " + e.getMessage());
} finally {
    try {
    in.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
}
return strLine;
}

public void setData(String data) {

}
}

看法:

<div id="settingsdiv" style="width:350px; height:400px; position:absolute;  background-color:r; top:20px; left:1px">

    <div id="settingsHashMap" style="width:1050px; height:400px; position:absolute;  background-color:r; top:20px; left:1px">

        <h:form id="form" >
            <p:growl id="growl" showDetail="true" sticky="true" />

            <h:commandButton value="Show next #{GlassfishLogFileController.pageSize} lines" 
                                 action="#{GlassfishLogFileController.actionNextPage}" />
            <h:commandButton value="Clear offset" 
                                 action="#{GlassfishLogFileController.actionClearOffset}" />
            <h:inputTextarea rows="30" cols="1000" value="#{GlassfishLogFileController.data}" />   
        </h:form>                    
    </div> 

</div> 
于 2012-09-19T16:56:34.373 回答