• Post Reply Bookmark Topic Watch Topic
  • New Topic

Add Options to Select List Using a String and JSTL

 
Steve Dyke
Ranch Hand
Posts: 1717
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a string that is delimited by commas(minor delimiter) and semicolons(major delimiter).

I cannot get the correct syntax to populate list:

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be sure to ask JSP questions in the JSP forum. I have moved this post there for you.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any particular reason you're doing this in the JSP and not in Java, where it's both easier, and easier to test?
 
Steve Dyke
Ranch Hand
Posts: 1717
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:Is there any particular reason you're doing this in the JSP and not in Java, where it's both easier, and easier to test?


I am building the string in Java and trying to display it as a list on client.

How else should I do it?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I always send a Map to the JSP to populate a select. I never build HTML in Java code unless it's unavoidable.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Dyke wrote:How else should I do it?

Build the collections in Java. It reduces JSP clutter, increases testability, and is overall a much cleaner solution.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Collections, yes. I would not build any markup in the Java, but I do not think that's what anyone is talking about.

I use a Map because (at least for me) the value of the options rarely matches the display text. An ordered Map (LinkedHashMap is a favorite) is perfect for this purpose.
 
Steve Dyke
Ranch Hand
Posts: 1717
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Collections, yes. I would not build any markup in the Java, but I do not think that's what anyone is talking about.

I use a Map because (at least for me) the value of the options rarely matches the display text. An ordered Map (LinkedHashMap is a favorite) is perfect for this purpose.


I went back in the java code and created an array instead of the delimited string. The JSTL was able to work with it and I am getting desired results.

Thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An array (or List) is fine if your option value and display text are the same (as I said, mine rarely are).
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(I should have been more clear in my original reply and differentiated between the view and server-side bits.)
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We know what you meant!
 
Steve Dyke
Ranch Hand
Posts: 1717
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:An array (or List) is fine if your option value and display text are the same (as I said, mine rarely are).


In this case the value and display were different but I used JSTL function to put array elements in respective positions.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad it works--but Bear's suggestion of using a map is cleaner yet unless you have a strong suite of client-side integration/functional tests.

The idea is to get as much logic as possible *out* of the JSP. JSPs are irritating to test, whereas you can trivially test the method that creates your map of key/text values.

If you're not testing then I guess it doesn't matter much, I suppose, but that would make me nervous for a different set of reasons.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to agree with David. Relying upon tricks and implicit rules like that is a recipe for trouble later on down the line.

What's your beef with using a Map, which is much better suited to the task?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!