> Hello everyone,
> I'm trying to display a list of items in a collection using JSTL.
> Each item in the collection has x, y, z and id variables.
> The user can choose whether he wants the display order (of the items
> in the collection) to be
> 1) x y z
> 2) z y x
> As far as I know the display ordering can be accomplished in two ways:
> 1) as shown in the code snippet below, create two different loops for
> each display format. The drawback with this approach is that a lot of
> the common stuff is duplicated between the two loops.
> 2) Have a c:choose , selection within a single loop, which decides
> between the two formats. The drawback is that the the same condition
> checking repeats for each loop iteration.
> Is there a way to set the formatting outside the loop , and use that
> format in the loop to address the two drawbacks above.
Assuming you have a resource bundle around somewhere, add two format strings
to the bundle, one for each of your two display orders; use a condition
('when', 'if' or whatever) to set a variable with the appropriate key; then
use that variable to specify the key to <fmt:message> within your loop.