• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is there any way to use a hashmap from the model in a script on the page?

 
Ryan Kostrzewa
Ranch Hand
Posts: 31
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have some paired values stored as a hashmap in my model, for example sake, we'll say the 'key' is a Manager's ID and the 'value' is the name of their department, so 'betty123' could be a key to a value 'IT', and 'dave345' could be a key for the value 'Finance'. This hashmap is populated from a database. In my form, I have two "Select" dropdown fields, one for "Department" (with fields which match the values in the hashmap), and one for "Manager ID". I want to autopopulate the "Manager ID" field when the "Department" field is changed, based on the values in the Hashmap. I understand that I can't access dynamically elements of the hashmap in a script, so, for example, I can't do this:



Because this code is dynamic based on the value of "department" at the time it's run, whereas the departmentHash object is generated when the page is loaded.

Is there some other way I can do this? Am I approaching it the wrong way? FWIW, I'm using Spring 4.1.1 and jQuery 1.8. I understand the conceptual block with code I wrote above, but I can't for the life of me think of an obvious solution although I'm sure this is a fairly commonplace problem in web development.

Thanks so much!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You's use JSP actions to re-create the hashmap in a JavaScript structure. Remember, all that the JSP is doing is "writing" the HTML page for you. So figure out what the JavaScript should look like, and then write the JSP to create that.

For example, you likely want to end up with something like:



 
Ryan Kostrzewa
Ranch Hand
Posts: 31
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, ok, but the pairings I need have to be pulled from a database, so I can't just hardcode them directly into the page (well, I could, it would just require more manual maintenance over the webpage), but is there any way at all to do it while pulling the values in from the database via the model?

Thanks much!
 
Paul Clapham
Sheriff
Posts: 21875
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course. The "model" you refer to is a servlet, or some similar component, which forwards to the JSP, right? Then in the model you extract the data from the database and put it in a suitable component in a request attribute. Then the JSP reads that request attribute and converts it into the JavaScript you're looking for.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Kostrzewa wrote:Hmm, ok, but the pairings I need have to be pulled from a database, so I can't just hardcode them directly into the page

You did not read my reply carefully enough. Once you figure out what the output needs to be, you write JSP actions to create that. Again, all JSP does is to create the HTML page to send to the browser. You can create the HTML and JavaScript markup to be anything you like, including the data from the database.

Perhaps you might benefit from reading these articles:
  • The Secret Life of JSPs
  • The Front Man

  •  
    Ryan Kostrzewa
    Ranch Hand
    Posts: 31
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ahhhh, I see. Yeah, I misinterpreted the purpose of the sample code you posted. I get what you're saying now, thanks so much to both of ya'll!
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 65522
    105
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    :thumbup:

    Yes, the example I posted is what you want to end up with on the page. You'd use JSP actions (be sure to use modern JSP with the JSTL and EL), to iterate over the list obtained from the database and generate the JavaScript code dynamically.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!