Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare and use variable in JSF2 xhtml page [duplicate]

Tags:

jsf-2

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

like image 769
Pirzada Avatar asked Nov 29 '12 03:11

Pirzada


1 Answers

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?

like image 107
Daniel Avatar answered Oct 19 '22 07:10

Daniel