• Post Reply Bookmark Topic Watch Topic
  • New Topic

Populate drop down list in JSP from .csv or xml. and which is the better way to do so.  RSS feed

 
Niza Berg
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am new to JSP/JSTL and was trying to build a page which have multiple drop down, whose results would eventually help me to make a connection to database and query accordingly to display the results. Earlier I, have added all the values of the drop down manually. But since the list may get longer over change of period, I wanted to auto populate it from either csv file or xml. But have not a very fair idea how can I proceed to do so.

Any help on this would be really great, and any learning curve on, which one is better to do so in a longer term, can also help me to get a good start.

Thanks!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Niharika Verma wrote:I wanted to auto populate it from either csv file or xml.

Why did you choose these particular formats? Neither is particularly well suited to the task.

Here are some alternatives that might be better approaches:
  • Store them in JSON files that can easily be read via Ajax.
  • Store them in the database and read them into application context with a context listener at app startup.
  • Create enums that represent the dropdowns that are easily transofrmed into Maps to send to the JSP.
  • Store the values in properties file(s).
  •  
    Niza Berg
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you Bear Bibeault, for responding. I am actually on a learning curve, and figuring out what could be the best way to do this task, I basically have 1000's of data in an excel sheet that I needed to show it in my drop down list. Thus thought if I can directly read it from excel and populate the same. But yes if that is not the preferred way. I am open to do it that way. Could you please provide any links that you know a kind of simple example to learn the same.

    Thank you
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 66207
    151
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Which way would you like to approach it? And while it might be theoretically possible to read the values from Excel, that would certainly not be a good way to approach it.

    Another factor is that you say you have thousands of items? A dropdown isn;t a good way to present a very long list of data. Perhaps there's a better approach? What's the nature of the data and how will the user interact with it?
     
    Niza Berg
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I basically have a page to display results from a database via different filters, so supposedly; a user wanted to see the result of one country say USA and in limited time frame or based on some other category, so they would select from drop down and hit "submit" to call the action through servlet that will eventually make a connection to database to query using those selected values from the filters say "country", "time frame" or "category" etc. and send back the response to display it. This is something I am doing right now. So the values in the drop down the filters may get increased, like countries could be more than just 10 and so on. Plus I needed to make my JSP page more cleaner and have these things be separate out. So was curious if I can separate this out and directly populate it from a file or JSON or create a property file and then further proceed.

    I would be fine to go via any of these either: JSON approach to parse and then display or create a property file and have them store it in key-value pair and further do such. Any basic example how step by step these things can work would be a great start for me to have the base and jump into complex structure.

    Hope I was able to answer?

    Thanks!

    P.S: I will be fine to know what other approaches you think would be a better way other then a drop down to show 1000's of data. This would be a good start for me. :)
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 66207
    151
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Niza Berg wrote: I needed to make my JSP page more cleaner and have these things be separate out. So was curious if I can separate this out

    Yes, this is a good idea.

    I would be fine to go via any of these either: JSON approach to parse and then display or create a property file and have them store it in key-value pair and further do such. Any basic example how step by step these things can work would be a great start for me to have the base and jump into complex structure.

    To be honest, since you are already connected to a DB, I'd store the values in the DB rather than deal with Ajax at the client. But if you want to go the Ajax route, I'd suggest using jQuery's $.getJSON() function to fetch the JSON data, and then jQuery's DOM manipulation API to create the <option> elements from the data. Make sure that you do this in a ready handler so that the DOM is loaded before you execute the script.

    P.S: I will be fine to know what other approaches you think would be a better way other then a drop down to show 1000's of data. This would be a good start for me. :)

    Auto-completer.

    A dropdown with more than a dozen or so options quickly becomes cumbersome. More than one hundred or so, and it becomes useless.
     
    Don't get me started about those stupid light bulbs.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!