0

I have a java class in which I have the method- getFiles() that prints a list of files in a directory. Here is the code

@Property
 private String[] filesInDir;

public void getFiles(String path){
      File aDirectory = new File("C://Users/A634682/report1/src/main/java/com/example/report1/reports2");

        // get a listing of all files in the directory
        filesInDir = aDirectory.list();

        // sort the list of files (optional)
        // Arrays.sort(filesInDir);

        System.out.println("File list begins here >>>>>");
        // have everything i need, just print it now
        for ( int i=0; i<filesInDir.length; i++ )
        {
          System.out.println( "file: " + filesInDir[i] );
        }
      }

In the respective tml file, I want to print this string array containing file names in form of table on the webpage.

Any help in how I can proceed?

1 Answers1

0

You can use a simple t:Loop component with your filesInDir as a source. One example can be found here: http://jumpstart.doublenegative.com.au/jumpstart/examples/tables/loop

You'd need to initialise the property before rendering starts, i.e. in setupRender().

Dmitry Gusev
  • 881
  • 8
  • 15
  • I did it. In the tml file, I used grid: But now I get the values as False. The number of rows printed is equal to the number of files I have in the folder. But I can't get the file names. How can I get the actual String value for each row in Grid? I have a simple String array as source. – Neeraja Karandikar Apr 26 '18 at 12:21
  • `t:Grid` is a more advanced component than `t:Loop`, because you only supplied the `source`, but not the `model` parameter it created a new `BeanModel` for the `String` class automatically, and the only property it could auto-detect is its `String#isEmpty()`. So for that only property `t:Grid` created the only column and displayed value of `#isEmpty`, which is `false`, because all your strings are not empty. You can read more about grids [here](http://tapestry.apache.org/5.4/apidocs/org/apache/tapestry5/corelib/components/Grid.html). – Dmitry Gusev Apr 26 '18 at 13:58