Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render a component only if another component is not rendered?

I have a page that the user can access via Android, iPhone, BlackBerry or via an unknown browser. I have 4 rich:panels, one for each platform and the latter is a generic one.

The code:

<rich:panel id="dlAndroid" rendered="#{fn:containsIgnoreCase(request.getHeader('User-Agent'), 'Android')}">
    ...
</rich:panel>

<rich:panel id="dlIphone" rendered="#{fn:containsIgnoreCase(request.getHeader('User-Agent'), 'iPhone')}">
    ...
</rich:panel>

<rich:panel id="dlBlackberry" rendered="#{fn:containsIgnoreCase(request.getHeader('User-Agent'), 'BlackBerry')}">
    ...
</rich:panel>

<rich:panel id="dlGeneric" rendered="#{ ---> WHAT TO WRITE HERE? <--- }">

How can I render the last rich:panel only if none of the others has been rendered?

like image 529
Alex Oliveira Avatar asked Feb 05 '13 19:02

Alex Oliveira


1 Answers

To the point, your question as stated in the title can concretely be answered as:

<rich:panel binding="#{panel1}" ...>
    ...
</rich:panel>
<rich:panel binding="#{panel2}" ...>
    ...
</rich:panel>
<rich:panel binding="#{panel3}" ...>
    ...
</rich:panel>
<rich:panel ... rendered="#{not panel1.rendered and not panel2.rendered and not panel3.rendered}">
    ...
</rich:panel>

However, in this particular case it's perhaps nicer to alias those long winded expressions with <c:set>:

<c:set var="android" value="#{fn:containsIgnoreCase(header['User-Agent'], 'Android')}" scope="request" />
<c:set var="iPhone" value="#{fn:containsIgnoreCase(header['User-Agent'], 'iPhone')}" scope="request" />
<c:set var="blackBerry" value="#{fn:containsIgnoreCase(header['User-Agent'], 'BlackBerry')}" scope="request" />

<rich:panel ... rendered="#{android}">
    ...
</rich:panel>
<rich:panel ... rendered="#{iPhone}">
    ...
</rich:panel>
<rich:panel ... rendered="#{blackBerry}">
    ...
</rich:panel>
<rich:panel ... rendered="#{not android and not iPhone and not blackBerry}">
    ...
</rich:panel>

Note that there's a shorter way to get the request header by the implicit #{header} map.

like image 172
BalusC Avatar answered Nov 07 '22 04:11

BalusC