java JSF 'total' 变量类似于 JSTL 中的 c:set
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1559689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSF 'total' variable something like c:set in JSTL
提问by Splendid
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:
我不喜欢 JSF,但我需要用它来解决这个问题,我在“纯”JSF 中工作。所以这就是我基本需要的,但我不知道如何用 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.
现在我可以用简单的 ${total} 来显示总价值,正如你所知。
My JSF table looks like this:
我的 JSF 表如下所示:
<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?!
但我不知道如何设置在每次迭代中都会增加的总变量?这个怎么解决?!
采纳答案by Hendrik
why dont you just do the calculation in the backing bean and just use jsf to retreive it?
为什么不直接在支持 bean 中进行计算并使用 jsf 来检索它?
And to answer your question, i don't know of a possiblity to set variables using just JSF libraries.
为了回答您的问题,我不知道是否可以仅使用 JSF 库来设置变量。
回答by cetnar
回答by Toskan
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
您同时混合了树构建时间标签和渲染时间标签。用 ui:repeat 替换 c:forEach 参见这里c:foreach vs ui:repeat

