-1

I wrote this code for read from a file and when page is load,it display in primefaces.But nothing shows in inputTextArea.

My Code:

    @Named("GlassfishLogFileController")
@SessionScoped
public class PrintReport implements Serializable {

    private static String logFile = null;

    public String logFile() {
        return logFile;
    }

    @PostConstruct
    public void init() {
        try {
            logFile = readLogFile();
        } catch (Exception e) {

        }
    }

    public String readLogFile() throws FileNotFoundException, IOException {
        BufferedReader br = null;
        br = new BufferedReader(new FileReader("D:\\Report.log"));

        //One way of reading the file
        System.out.println("Reading the file using readLine() method:");
        String contentLine = br.readLine();
        while (contentLine != null) {
            System.out.println(contentLine);
            contentLine = br.readLine();

        }
        return contentLine;
    }
}

xhtml:

 <h:inputTextarea rows="30" cols="1000" value="#{GlassfishLogFileController.logFile}" /> 
Kukeltje
  • 12,223
  • 4
  • 24
  • 47
John
  • 115
  • 2
  • 3
  • 10
  • 2
    And you have getter/setter for logFile? Also it seems to me you must change the logic in readLogFile(). It will always return null, and only sysout the content. For example add contentLine to a StringBuilder. And the standard is to start with lowercase for the bean name – Jaqen H'ghar May 22 '16 at 10:17
  • @ Jaqen H'ghar I put break point in my code.It never come to my code! – John May 22 '16 at 10:24
  • To where in the code? – Jaqen H'ghar May 22 '16 at 10:25
  • 1
    I'd guess because the bean is not referenced from the page. Current reference is wrong if you have no getter. Correct the 3 things I've told you and get back with result – Jaqen H'ghar May 22 '16 at 10:28
  • Ok,Thank you for your help :) – John May 22 '16 at 10:31
  • Let us know how goes :-) – Jaqen H'ghar May 22 '16 at 10:31
  • May I suggest to read some basic jsf tutorials... And please use correct tagging of questions. The `h:inputTextare` is not PrimeFaces but plain jsf. And it is also not java-ee related. – Kukeltje May 22 '16 at 15:37

1 Answers1

1

As @Jaqen H'ghar mentioned in his comment, you should create a getter method to access the logFile in your UI.

public String getLogFile(){
        return logFile;
    }

Change your readLogFile method :

public String readLogFile() throws FileNotFoundException,IOException{
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader("C:\\DefaultServer-diagnostic.log"));
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();

            while (line!=null) {
                sb.append(line);
                sb.append("\r\n");
                line = br.readLine();
            }
            return sb.toString();
        }
        finally {
            br.close();
        }
    }

For more info Reading a plain text file in Java

Community
  • 1
  • 1
Unknown
  • 2,037
  • 3
  • 30
  • 47