Am using JSF Primefaces 7.0 to render an XHTML file to display a list of users (along with their e-mail addresses) inside a single HTML dropdown list.
pom.xml:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
<scope>provided</scope>
</dependency>
<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>
User class:
package com.myapp.model;
import static org.apache.commons.lang3.StringUtils.isBlank;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.NoArgsConstructor;
@NoArgsConstructor
@Data
public class User implements Serializable {
private String firstName;
private String lastName;
// this is their e-mail address
private String username;
public String getName() {
final StringBuilder sb = new StringBuilder();
if (!isBlank(getFirstName())) {
sb.append(getFirstName()).append(' ');
}
if (!isBlank(getLastName())) {
sb.append(getLastName());
}
return sb.toString().trim();
}
}
Inside users.xhtml:
<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"
xmlns:c="http://java.sun.com/jstl/core"
>
<f:selectItems
value="#{userReport.users}"
var="user"
itemLabel="#{user.name} (#{user.username})"
itemValue="#{user}"/>
</p:selectOneMenu>
</html>
This renders correctly for an individual item if the (user.name is not null) inside an HTML dropdown list:
John Doe (jdoe@email.com)
How can I use a conditional (<c:if> with <c:else> or even the ? : ternary operator) to set the itemLabel attribute to render just the user.username without parenthesis if the user.name is empty string or null?
So, it should set to this (the e-mail address) if user.name is null or an empty string:
itemLabel="#{user.username}"
To make display (if user.name is null or empty string):
jdoe@email.com
Tried doing this:
itemLabel="#{not empty user.name ? user.name (user.username) : user.username}"
Which gives me the following stack trace:
javax.servlet.ServletException: Method not found: class com.myapp.model.User.name(java.lang.String)
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)
Caused by: javax.el.MethodNotFoundException: Method not found: class com.myapp.model.User.name(java.lang.String)
at javax.el.Util.findWrapper(Util.java:373)
at javax.el.Util.findMethod(Util.java:219)
at javax.el.BeanELResolver.invoke(BeanELResolver.java:149)
at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:79)
... 35 more
Don't understand why this is the case because the original attribute did find the User.getName() method:
itemLabel="#{user.name} (#{user.username})"
Is there a better way to do this?