• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JSP select with multiple options

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear all,

I used to have a Select list where only a single option could be selected. It worked fine.
After the page was submitted (with the action set to the same page), the select list still contained
the same select option as was chosen right before the submit.
Of course, this is due to the value="..." in the jsp:setProperty tag in the code below.

Now, I learned how to change this all into a Select list with multiple options. It works fine still, with one
small exception. The selection gets lost. I assume this is because ${param['multiSelect']} is converted
to a String here, or one cannot set the initial values like this?

Could somebody explain me how to do this? Or should I do it myself with some JavaScript?
(I know how to do that, but I thought it should not be necessary).

Thanks in advance.

Here's the relevant part of my sources:



The thing I learned was to simple use String arrays in the bean:
 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I am understanding you correctly, yes, you are having an issue with rendering it that way... if you look at the rendered html you will probably see a comma separated string or something similar.

What you need to do is use JSP tags (or scriptlets if you have to) to loop through the values in the array when you generate the options so that the selected values are selected in the html

Something like:


That is somewhat psuedo code since I am not really clear on the details of what you are doing. The "c" tags are Core JSTL tags that should be available on any current version JSP server or you can install them yourself from the Apache Taglibs (http://jakarta.apache.org/taglibs) project.

What you will end up with is that the "selected" items will show up preselected in the html.

Hope this helps.
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to clear things out:

Chris Stehno wrote:That is somewhat psuedo code since I am not really clear on the details of what you are doing.

The implicit variable ${param} points to HttpServletRequest#getParameterMap(). It look like that you wasn't aware of this.

Try this:

It will set the selected attribute when the multiSelect array contains the value.
 
John Sutt
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!
The point I totally missed here, was that - instead of seting the value of the input element - I should add the 'selected="selected"' parameter
to all input options that I want to have initially selected !
John.
 
girl power ... turns out to be about a hundred watts. But they seriuosly don't like being connected to the grid. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic