3

Is there a way to temporarily save the value of calcuation in a JSF page? I want to do the following without calculating twice:

<h:outputText value="#{complexModel.currencyAmount.currency}">
<h:outputText value="#{complexModel.currencyAmount.amount}">

I've tried using the alias bean but I get an error saying java.lang.IllegalArgumentException - row is unavailable.

e.g.

<t:aliasBean id="bean" alias="#{bean}" value="#{complexModel.currencyAmount}">
  <h:outputText value="#{bean.currency}">
  <h:outputText value="#{bean.amount}">
</t:aliasBean>

Thanks.

DD.
  • 21,498
  • 52
  • 157
  • 246

2 Answers2

6

Two ways (at least):

  1. Using lazy-init field of your complexModel. something like:

    private Currency currencyAmount;
    public Currency getCurrencyAmount() {
        if (currencyAmount == null) {
            currencyAmount = calculateCurrencyAmount();
        }
        return currencyAmount;
    }
    
  2. Using the JSTL <c:set> tag:

(the namespace first)

xmlns:c="http://java.sun.com/jstl/core"

then

<c:set var="varName" value="#{complexModel.currencyAmount}" />

And then the calculated value will be accessible through #{varName}.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • The first option - yes. The second - I have no idea. One advice - when asking questions about obsolete version of a framework, mention this explicitly. – Bozho Feb 11 '10 at 14:16
  • The first option doesnt really suit as I'm output the values in a datatable so the value is dependent on the row...I could have a map but its getting messy as this needs to be cleared when refreshing etc. – DD. Feb 11 '10 at 14:26
-2

In JSF 2.0,you can use <ui:param/> which it's powerful.

<ui:param name="foo" value="#{zhangsan.foo}"/>
Jallen
  • 1
  • 1
  • Read section " vs " here: http://stackoverflow.com/questions/3342984/jstl-in-jsf2-facelets-makes-sense – BalusC Jan 17 '16 at 14:51