Search code examples
jsfprimefacesjstl

How to use Ternary Operator using Primefaces using JSF EL to render specific text when items are null?


Am using JSF Primefaces 7.0 to render an xhtml file to display a user (alongside the username inside parenthesis) if his name is null or an empty string, I wish to just show the username without the parenthesis.


pom.xml:

<dependency>
    <groupId>org.primefaces</groupId>
    <artifactId>primefaces</artifactId>
    <version>7.0</version>
</dependency>
    <dependency>
    <groupId>org.primefaces.extensions</groupId>
    <artifactId>primefaces-extensions</artifactId>
    <version>7.0.3</version>
</dependency>

So, right now this is what I have rendering properly:

user.xhtml:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html
        xmlns="http://www.w3.org/1999/xhtml"
        xmlns:p="http://primefaces.org/ui"
        xmlns:ui="http://java.sun.com/jsf/facelets"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:f="http://java.sun.com/jsf/core">

    User: #{userReport.selectedUser.name} (#{userReport.selectedUser.username})
</html>

Renders (inside a web browser):

User: John Doe (jdoe)

However, if the #{userReport.selectedUser.name} is null, it renders the username inside parenthesis:

User: (jdoe)

I need it to render like this (without the parenthesis) :

User: jdoe

Tried using the ternary operator ? :

#{userReport.selectedUser.name ? userReport.selectedUser.name (#{userReport.selectedUser.username}) : #{userReport.selectedUser.username} }

This causes the following stack trace:

javax.servlet.ServletException: The identifier [#] is not a valid Java identifier as required by section 1.19 of the EL specification (Identifier ::= Java language identifier). This check can be disabled by setting the system property org.apache.el.parser.SKIP_IDENTIFIER_CHECK to true.
    at javax.faces.webapp.FacesServlet.executeLifecyle(FacesServlet.java:749)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:475)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:111)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Solved it using this:

User:<c:choose>
         <c:when test="#{not empty userReport.selectedUser.name}">
             #{userReport.selectedUser.name} ({userReport.selectedUser.username})
          </c:when>
          <c:otherwise>
             #{userReport.selectedUser.username}
          </c:otherwise>
     </c:choose>

Is there a way to do this in a simpler way? For example, using the ternary ? : operator?


Solution

  • Got it working using the ? : ternary operator and the += concatenation operator:

    User: #{!empty(userReport.selectedUser.name) ? userReport.selectedUser.name += ' (' += userReport.selectedUser.username += ')' : userReportView.selectedUser.username}