This week's giveaway is in the Threads forum.
We're giving away four copies of Java Concurrency Live Lessons and have Doug Schmidt on-line!
See this thread for details.
Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JSP select with multiple options  RSS feed

 
John Sutt
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • 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:
 
Chris Stehno
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • 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.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!