开发者

JSF Temporary Variable

开发者 https://www.devze.com 2022-12-19 17:48 出处:网络
Is there a way to temporarily save the value of calcuation in a JSF page? I want to do the following without calculating twice:

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 unavailabl开发者_C百科e.

e.g.

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

Thanks.


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


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

<ui:param name="foo" value="#{zhangsan.foo}"/>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号