I am wondering how to display a List<T>
as obtained below in a Facelet:
public List<T> searchByString(String string) {
return getEntityManager().createNamedQuery("Userdetails.findByUsername").setParameter("username", "%" + string + "%").getResultList();
}
Would a <h:dataTable>
be a suitable way?
You're going need to iterate over it. JSF 2 offers three iteration components out the box. Provided that the
User
entity look like below,and that the search results are assigned as a
List<User> users
property of a bean which is available as#{bean}
,here are some examples based on it:
<h:dataTable>
, an UI component which generates a HTML<table>
.<ui:repeat>
, an UI component which generates no HTML markup (so, you'd have to write all that HTML in the desired fashion yourself, which could easily be changed to e.g.<ul><li>
, or<dl><dt><dd>
, or<div><span>
, etc):<c:forEach>
, a tag handler which runs during view build time instead of view render time (background explanation here: JSTL in JSF2 Facelets... makes sense?), it also doesn't produce any HTML markup:See also:
<h:dataTable>