JSF 'total' variable something like c: set in JSTL

I don't like JSF, but I need to solve this problem, I work in "pure" JSF. So this is what I need, but I don't know how to do 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 show the total with a simple $ {total} as you 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 the shared variable to be incremented with each iteration? How to solve this ?!

+2


source to share


3 answers


why don't you just do the calculations in the bean backup and just use jsf to restore it?



And to answer your question, I am not aware of the ability to set variables using only JSF libraries.

+2


source


An interesting detail - the tag <c:set>

will be available in JSF 2.0 .



+1


source


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

0


source







All Articles