2

I don't like JSF but I need to solve this problem with it, I am working in "pure" JSF. So this is what I baisicly need but I don't know how to accomplish it with JSF:

<c:set var="total" value="0"></c:set>

<c:forEach var="item" items="${cart}">
    <tr>
        <td>${item.product.name}</td>
        <td>${item.product.price}</td>
        <td>${item.quantity}</td>
        <td>${item.product.price * item.quantity}</td>
    </tr>
    <c:set var="total" value="${total + item.product.price * item.quantity}"></c:set>
</c:forEach>

Now I can display total value with simple ${total} as u know.

My JSF table looks like this:

    <h:dataTable var="item" value="#{mbProducts.cart_items}" binding="#{mbProducts.tableComponent}" border="1">

        <h:column>
            <f:facet name="header">
                <h:outputText value="NAME" />
            </f:facet>

            <h:outputText value="#{item.product.name}" />
        </h:column>

        <h:column>
            <f:facet name="header">
                <h:outputText value="PRICE" />
            </f:facet>

            <h:outputText value="#{item.product.price}" />
        </h:column>

        <h:column>
            <f:facet name="header">
                <h:outputText value="NUM" />
            </f:facet>

            <h:outputText value="#{item.quantity}" />
        </h:column>

        <h:column>
            <f:facet name="header">
                <h:outputText value="TOTAL PRICE" />
            </f:facet>

            <h:outputText value="#{item.product.price * item.quantity}"/>
        </h:column>

    </h:dataTable>

But I don't know how to set total variable which will be increased in each iteration? How to solve this?!

Splendid
  • 1,317
  • 4
  • 20
  • 41
  • I didn't find JSF solution for this which is retarded, and I modified backend bean, so problem is kinda solved but I am still interested to hear is this possible to solve only with JSF – Splendid Oct 13 '09 at 14:13

3 Answers3

2

why dont you just do the calculation in the backing bean and just use jsf to retreive it?

And to answer your question, i don't know of a possiblity to set variables using just JSF libraries.

Hendrik
  • 1,355
  • 3
  • 11
  • 30
  • I want to avoid that because I am learning JSF, it's not a problem to implement that in backing bean but I want to make it in JSF if possible? – Splendid Oct 13 '09 at 11:50
  • Because you are learning JSF, you must define what to do on presentation side, and what to do on server side. A total is typically the kind of value that you can calculate on the server side and only use a getter on JSF pages. – Romain Linsolas Oct 13 '09 at 12:16
  • 2
    The goal of solving something only with JSF doesn't mean you're confined to only solving it in your markup. JSF includes your backing beans too, so it's up to you to figure out if something is better calculated in your beans or on your pages. A total calculation definitely belongs in your backing bean, not on the page. At the end of the day, you're STILL doing this with JSF. – InverseFalcon Dec 17 '09 at 23:13
2

Interesting detail - <c:set> tag will available be in JSF 2.0.

cetnar
  • 9,357
  • 2
  • 38
  • 45
0

you are mixing tree build time tags vs rendertime tags at the same time. replace c:forEach with ui:repeat see here c:foreach vs ui:repeat

Roger Keays
  • 3,117
  • 1
  • 31
  • 23
Toskan
  • 13,911
  • 14
  • 95
  • 185