20

I am using <s:set var="buttonText" value="getText('person.button.add')"/> in JSTL to use buttonText variable to set text.

Someone tell me how to accomplish this in JSF EL please?

Question asked here IF-ELSE condition in JSF form. Need to know right approach but told to ask as a separate question.

EDITED:

I already added a link above for detail but here is again.

I am using JSTL like in struts2 project

 <s:if test="person==null || person.id==null || person.id==''">
                <s:set var="buttonText" value="getText('person.button.add')"/>
            </s:if>
            <s:else>
                <s:set var="buttonText" value="getText('person.button.edit')"/>
            </s:else>

I want to use same approach in JSF2 but with Expression Language. I am saving value in a variable called buttonText and accessing it below somewhere like #{buttonText}. I need solution to how to write variable in EL and use it. Hope its clear.

I need solution to <s:set var="buttonText" value="getText('person.button.add')"/> line of code only.

Thanks

Community
  • 1
  • 1
Pirzada
  • 4,685
  • 18
  • 60
  • 113
  • please clear about your question? What do you actually want to know? – Freak Nov 29 '12 at 03:38
  • **WARNING** do NOT use `` as mentioned in 1st option of the currently high rated answer! ALWAYS use ``, no excuses. – BalusC Nov 13 '18 at 12:32
  • Why, @BalusC? Is that because the expression get reevaluated every time it's used? – bruno Nov 14 '18 at 14:16
  • 1
    @bruno: It's explained in the abovelinked dupe. In a nutshell: it's NOT where the `` is originally intended for. It's exactly the same problem as trying to use a hammer to fasten a screw: it might work in some circumstances, but it's utterly the wrong tool for the job. – BalusC Nov 14 '18 at 15:01

1 Answers1

52

You can use ui:param

In the following ways:

<ui:param name="buttonText" value="#{myBean.MyValue}" />

or

<ui:param name="buttonText" value="someText" />

or

<ui:param name="buttonText" value="someText#{myBean.MyValue}" />

and use later on like this

<h:outputText value="#{buttonText}"/>

Another option would be using <c:set in the following way

<c:set var="buttonText" value="#{myBean.MyValue}" />

But note that <c:set is a JSTL tag,

JSTL tags runs during building the view (when the XHTML file is converted to JSF component tree)

Also you might want to read this JSTL in JSF2 Facelets… makes sense?

Community
  • 1
  • 1
Daniel
  • 36,833
  • 10
  • 119
  • 200