You are viewing a plain text version of this content. The canonical link for it is here.
Posted to taglibs-user@tomcat.apache.org by Rashmi <ra...@gmail.com> on 2009/02/11 17:53:47 UTC

Conditional formatting elements of a collection in c:forEach

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.


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
  <head>
    <title></title>
  </head>
  <body>

        <c:choose>
            <c:when test="${param.listBy == 'xFirst' }">
                <c:forEach var="item" items="${requestScope.items}">
                     ${item.x} ${item.y} ${item.z}
                     <a href="/show?id=${item.id}">show item</a>
                     <a href="/edit?id=${item.id}">edit item</a>
                     <a href="/delete?id=${item.id}">delete item</a>
                </c:forEach>
            </c:when>
            <c:otherwise>
                <c:forEach var="item" items="${requestScope.items}">
                     ${item.z} ${item.y} ${item.x}
                     <a href="/show?id=${item.id}">show item</a>
                     <a href="/edit?id=${item.id}">edit item</a>
                     <a href="/delete?id=${item.id}">delete item</a>
                </c:forEach>
            </c:otherwise>
        </c:choose>
  </body>
</html>

Any input is appreciated.

-Rashmi

---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org


Re: Conditional formatting elements of a collection in c:forEach

Posted by Rashmi <ra...@gmail.com>.
The null value display problem is also sovled (after referring to the
JSTL 1.2 spec) :

                <fmt:message key="${formatKey}" bundle="${nameOrder}">
                    <fmt:param><c:out value="${person.firstName}"
default=""/></fmt:param>
                    <fmt:param><c:out value="${person.middleInitial}"
default=""/></fmt:param>
                    <fmt:param><c:out value="${person.lastName}"
default=""/></fmt:param>
                </fmt:message>

-Rashmi

---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org


Re: Conditional formatting elements of a collection in c:forEach

Posted by Rashmi <ra...@gmail.com>.
I tried the ResourceBundle suggestion made by Martin and it works really well.

ItemFormats.properties (placed in /WEB-INF/classes folder) file has :
formatXFirst = {0} {1} {2}
formatZFirst = {2} {0} {1}

And in the JSP I have:

<fmt:setBundle basename="ItemFormats" var="itemOrder"/>

followed by setting a key variable to hold either formatXFirst and formatZFirst

and in the single loop:

                <fmt:message key="${formatKey}" bundle="${itemOrder}">
                    <fmt:param value="${item.x}"/>
                    <fmt:param value="${item.y}"/>
                    <fmt:param value="${item.z}"/>
                </fmt:message>

The only problem is that the fmt:message prints null if the variable
value is null.

as in ValueX null ValueZ ,

Without formatting applied , printing just ${item.x} ${item.y}
${item.z}  , prints
ValueX ValueZ (the null value is not printed, which is better).

-Rashmi





On Wed, Feb 11, 2009 at 12:45 PM, Rashmi <ra...@gmail.com> wrote:
> Thank you Martin, I'm new to using resource bundle, I'll learn it and
> give this a try.
>
> -Rashmi
>
>
> On Wed, Feb 11, 2009 at 12:29 PM, Martin Cooper <ma...@apache.org> wrote:
>>
>> 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.
>>
>> --
>> Martin Cooper
>>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org


Re: Conditional formatting elements of a collection in c:forEach

Posted by Rashmi <ra...@gmail.com>.
Thank you Martin, I'm new to using resource bundle, I'll learn it and
give this a try.

-Rashmi


On Wed, Feb 11, 2009 at 12:29 PM, Martin Cooper <ma...@apache.org> wrote:
>
> 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.
>
> --
> Martin Cooper
>

---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org


Re: Conditional formatting elements of a collection in c:forEach

Posted by Martin Cooper <ma...@apache.org>.
On Wed, Feb 11, 2009 at 8:53 AM, Rashmi <ra...@gmail.com> wrote:

> 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.

--
Martin Cooper



> <%@ page contentType="text/html;charset=UTF-8" language="java" %>
> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
> <html>
>  <head>
>    <title></title>
>  </head>
>  <body>
>
>        <c:choose>
>            <c:when test="${param.listBy == 'xFirst' }">
>                <c:forEach var="item" items="${requestScope.items}">
>                     ${item.x} ${item.y} ${item.z}
>                     <a href="/show?id=${item.id}">show item</a>
>                     <a href="/edit?id=${item.id}">edit item</a>
>                     <a href="/delete?id=${item.id}">delete item</a>
>                </c:forEach>
>            </c:when>
>            <c:otherwise>
>                <c:forEach var="item" items="${requestScope.items}">
>                     ${item.z} ${item.y} ${item.x}
>                     <a href="/show?id=${item.id}">show item</a>
>                     <a href="/edit?id=${item.id}">edit item</a>
>                     <a href="/delete?id=${item.id}">delete item</a>
>                </c:forEach>
>            </c:otherwise>
>        </c:choose>
>  </body>
> </html>
>
> Any input is appreciated.
>
> -Rashmi
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: taglibs-user-help@jakarta.apache.org
>
>