Sunday, August 25, 2019

java - How to convert scriptlets into jstl tags




I tried turning these scriptlets into jstl tags with no success, is it not doable with these lines of codes, and if it can how so? thanks



<% 
//String file = application.getRealPath("C:/science/");


File f = new File("C:/uploads");
String [] fileNames = f.list();
File [] fileObjects= f.listFiles();
%>

    <%
    for (int i = 0; i < fileObjects.length; i++) {
    if(!fileObjects[i].isDirectory()){


    %>

  • <%= fileNames[i] %> Download
    <%= fileNames[i] %>
        
    (<%= Long.toString(fileObjects[i].length()) %> bytes long)

    <%
    }
    }

    %>

Answer



All that code you have in the scriptles you have to do it in the java code and pass it to the jsp. I don't know if you are using any framework to do this but this is easy in frameworks like Spring MVC.



In your case you should create in java a bean with the properties you need such as isDirectory, length, name of the file, etc. Then you create a list of these beans and pass it to the jsp. Finally, you just iterate these list of beans in the jsp with a forEach loop of JSTL (http://docs.oracle.com/javaee/5/jstl/1.1/docs/tlddocs/c/forEach.html). You can google about it and you'll find lots of examples about how to do this.



Doing this you don't need to use scriptlets.


No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...