• Post Reply Bookmark Topic Watch Topic
  • New Topic

JSTL EL: Loop though HashMap with two ArrayLists<String>  RSS feed

 
Michael Portman
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all.

I've successfully written my FileIO API and ive tested in a prototype servlet. Now I need to implement it into my project.

In my JSP i have the following code:



But its giving me unexpected results. Like say if I want to display the contents of C drive it would display it as follows:



Whereas in my prototype servlet it would display it as:



How do I fix the JSP EL version so it displays it like my servlet does? Many thanks
 
Dave Tolls
Ranch Foreman
Posts: 3009
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is ViewDirectory being populated?
That output looks to my like it's a List of Objects, and the first Object is an array, with the rest being simple Strings.
 
Dave Tolls
Ranch Foreman
Posts: 3009
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, no.
Completely misread that.

So it's a Map?
Looks like the single key for the Map is an array.
 
Michael Portman
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My HashMap ArrayList looks like this:

 
Michael Portman
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first arraylist contains the folders of the given directory, and the second arraylist contains the files of the given directory
 
Dave Tolls
Ranch Foreman
Posts: 3009
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you have an ArrayList as the key to a Map?
That sounds wrong.

But that's why you have this output:
Folder: [C:\CINEBENCH, C:\Documents and Settings, C:\Fraps, C:\Intel, C:\PerfLogs, C:\Program Files, C:\Program Files (x86), C:\Temp, C:\Users, C:\Windows]

That's the key, and it looks like you have only the one key.
 
Michael Portman
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im trying to get 2 arraylists returned of a function. so i thought about putting them in a hashmap by setting it up as the folders as keys and the files as values. obviously this isnt gonna work.... or would it?
 
Michael Portman
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
.... returned from a function *
 
Michael Portman
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do multidimensional arrays work in java? cos i think that would be a better option
 
Dave Tolls
Ranch Foreman
Posts: 3009
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly are you trying to model?
What are you trying to achieve?

With that information we might be able to come up with a suitable solution for you.

Looking at your prototype output, I'm not sure what use that list is.  What's it for?
 
Michael Portman
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I scanning a particular directory and storing the results (file names and folder name) in separate arrays (or one array if poss)
 
Michael Portman
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the whole point of it is to manage the servers file system through a browser
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66263
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A multi-dimensional array (nit: no such thing really in Java, just arrays of arrays, but that's moot) is certainly not the right approach.

Sounds like a simple list of beans (folders) that in turn have a list of children (other folder and files). Modeling your data correctly is 99% of the battle. Modeled correctly, writing the code in a JSP should be almost trivial. If your JSP is complex, it's a good sign that the data wasn't prepared correctly.
 
Michael Portman
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there must be a way to get 2 data collections out of a single method without making 2 separate methods to handle each data collection
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66263
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Who said anything about two methods? It's all about proper modeling.

I'd create two bean classess: (1) One that contains the info about files (perhaps just name? but other info as necessary) and (2) One that captures the info about folders (again with name and any other info as necessary) that also possesses a list of its children, which can be either another folder or a file.

To make thing tidy, the file and folder class would either implement a common interface, or inherit from a common superclass, so that they can be easily added to a single list as children.

 
Dave Tolls
Ranch Foreman
Posts: 3009
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what does the current code do?
Does it find all the folders then all the files?
Or does it iterate over the contents and check which is a file and which a folder?

And just to clarify, this is to list the contents of a single directory so that you can eventually display them, folders first, later on?
 
Michael Portman
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:So what does the current code do?
Does it find all the folders then all the files?
Or does it iterate over the contents and check which is a file and which a folder?

And just to clarify, this is to list the contents of a single directory so that you can eventually display them, folders first, later on?


yeah it loops through a dir using isdirectory() etc then puts the results in the relevant arraylist (file or folder)
 
Michael Portman
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the API does a lot more than this tho. like creating/deleting files/folders, renaming, moving et etc
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66263
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All the more reason to make sure that your model accurately represents the data. Two flat lists doesn't reflect the hierarchal nature of a file system.
 
Michael Portman
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ill show you a screenshot of the prototype servlet ive made. it wont look like this when its finished



I just want it to be very basic
 
Michael Portman
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So is there no possible way to print all of the "key/s" names from the first arraylist in the hashmap, then print all the "values" from the second arraylist in the hashmap in a nested EL forEach loop? Thats alls im asking for. I know you may think this is a bad approach to the problem but I dont want to change the code nor do I have the time. Maybe at a later date ill revise it.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66263
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Portman wrote:Maybe at a later date ill revise it.

Sorry to be blunt, but this would get you kicked off my team. Later never comes.

You don't have the time to not do it right.

My opinion. Take it or leave it.
 
Michael Portman
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
Michael Portman wrote:Maybe at a later date ill revise it.

Sorry to be blunt, but this would get you kicked off my team. Later never comes.

You don't have the time to not do it right.

My opinion. Take it or leave it.


I've done it now anyway on my own.  It may be a bit crude it works and it does what I want it to do.....



Its two separate loops, one for the folders arraylist and one for the files arraylist. It yields exactly the same results as the prototype.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!