Defining and reusing an EL variable in JSF page
Asked Answered
C

2

66

Is it possible to define variable and reuse the variable later in EL expressions ?

For example :

<h:inputText 
   value="#{myBean.data.something.very.long}"
   rendered="#{myBean.data.something.very.long.showing}"
/>

What i have in mind is something like :

<!-- 
     somehow define a variable here like : 
     myVar = #{myBean.data.something.very.long} 
-->
<h:inputText 
   value="#{myVar}"
   rendered="#{myVar.showing}"
/>

Any ideas ? Thank you !

Children answered 22/6, 2011 at 4:22 Comment(0)
K
119

You can use <c:set> for this:

<c:set var="myVar" value="#{myBean.data.something.very.long}" scope="request" />

This EL expression will then be evaluated once and stored in the request scope. Note that this works only when the value is available during view build time. If that's not the case, then you'd need to remove the scope attribtue so that it becomes a true "alias":

<c:set var="myVar" value="#{myBean.data.something.very.long}" />

Note thus that this does not cache the evaluated value in the request scope! It will be re-evaluated everytime.

Do NOT use <ui:param>. When not used in order to pass a parameter to the template as defined in <ui:composition> or <ui:decorate>, and thus in essence abusing it, then the behavior is unspecified and in fact it would be a bug in the JSF implementation being used if it were possible. This should never be relied upon. See also JSTL in JSF2 Facelets... makes sense?

Kliman answered 22/6, 2011 at 14:17 Comment(7)
Wow, thanks ! I thought ui:param only supplies a variable to be used in a template, which makes use of ui:composition. Does that mean every jsf page that i make is a facelet even though im not using ui:composition ?Children
@BalusC, what do you mean by don't cache the value ?Oedipus
@Msaleh: Everytime you call #{myVar} then the #{myBean.data.something.very.long} will be re-evaluated. It will not be evaluated only once during the set. It's thus merely an "alias".Kliman
ok, it doesn't cache, but is there any reason why it shouldn't ?Marvelous
@user: Because, by default, EL expressions may evaluate to a different value depending on the moment you invoke it. E.g. depending on the current phase, iteration status of a repeating component, etc. If you want to explicitly set it in request, view, session or application scope use as answered the <c:set>.Kliman
Somewhere there was a mention of <ui:param> to which bertie further refers, but is now gone. He is actually asking a very valid question: <ui:param> documents: "Use this tag to pass parameters to an included file (using ui:include), or a template (linked to either a composition or decorator). Embed ui:param tags in either ui:include, ui:composition, or ui:decorate to pass the parameters." - can we use <ui:param> outside that context? See also https://mcmap.net/q/17374/-declare-and-use-variable-in-jsf2-xhtml-page-duplicateFadge
@YoYo: You can, but the behavior is unspecified and should absolutely not be relied on. It's in fact a bug in the JSF implementation if abusing <ui:param> as <c:set> would be possible. See also https://mcmap.net/q/17365/-jstl-in-jsf2-facelets-makes-senseKliman
T
19

Like any view in MVC, the page should be as simple as possible. If you want a shortcut, put the shortcut into the controller (the @ManagedBean or @Named bean).

Controller:

@Named
public MyBean
{
    public Data getData()
    {
        return data;
    }

    public Foo getFooShortcut()
    {
        return data.getSomething().getVery().getLong();
    ]
}

View:

<h:inputText 
   value="#{myBean.fooShortcut}"
   rendered="#{myBean.fooShortcut.showing}"
/>
Terce answered 22/6, 2011 at 4:34 Comment(1)
Perfect solution to grab my <p:dataTable> metadata objects from the backing bean. Thanks!Lucy

© 2022 - 2024 — McMap. All rights reserved.