2012-08-23 27 views
2

我想顯示顯示Glassfish日誌文件內容的JSF頁面。到目前爲止,我只有這個工作:如何創建延遲加載顯示文本日誌文件的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頁面中,但沒有一個如何將文本文檔顯示到文本區域。

最好的祝願

P.S我更新了代碼。現在我得到空的輸入字段。沒有顯示日誌文件中的數據。

+0

題外話,但你已經CDI的bean與面孔ViewScoped,這顯然不會工作。命名的豆類由CDI管理,而ManagedBeans由jsf管理... – StephenMeyer

+0

我想我可以用getter/setter做到這一點? – user1285928

+0

我只會一次顯示20行,並且有事件會在某些事件上加載其餘行(點擊,焦點等)。 – tartar

回答

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>