How iterate over List<T> and render each item in JSF Facelets

ⅰ亾dé卋堺 提交于 2019-11-26 04:00:43

问题


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?


回答1:


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,

@Entity
public class User {
    private @Id Long id;
    private String username;
    private String email;
    private LocalDate birthdate;
    // Add/generate getters+setters.
}

and that the search results are assigned as a List<User> users property of a bean which is available as #{bean},

@Named @RequestScoped
public class Bean {
    private List<User> users;
    // Add/generate postconstruct+getter.
}

here are some examples based on it:

  1. <h:dataTable>, an UI component which generates a HTML <table>.

    <h:dataTable value="#{bean.users}" var="user">
        <h:column>#{user.id}</h:column>
        <h:column>#{user.username}</h:column>
        <h:column><a href="mailto:#{user.email}">#{user.email}</a></h:column>
        <h:column>#{user.birthdate}</h:column>
    </h:dataTable>
    
  2. <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):

    <table>
        <ui:repeat value="#{bean.users}" var="user">
            <tr>
                <td>#{user.id}</td>
                <td>#{user.username}</td>
                <td><a href="mailto:#{user.email}">#{user.email}</a></td>
                <td>#{user.birthdate}</td>
            </td>
        </ui:repeat>
    </table>
    
  3. <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:

    <table>
        <c:forEach items="#{bean.users}" var="user">
            <tr>
                <td>#{user.id}</td>
                <td>#{user.username}</td>
                <td><a href="mailto:#{user.email}">#{user.email}</a></td>
                <td>#{user.birthdate}</td>
            </td>
        </c:forEach>
    </table>
    

See also:

  • Java EE 6 tutorial - Adding components to a page - using <h:dataTable>
  • How and when should I load the model from database for h:dataTable



回答2:


You can save your List in a class variable, give it a getter and (maybe) a setter. Declare searchByString method as void and call it with let's say a (provided you are using PrimeFaces):

<p:commandLink update="@form" process="@this" action="#{myBean.searchByString}"> 

myBean:

public void searchByString(String string) {     
    userList = getEntityManager().createNamedQuery("Userdetails.findByUsername").setParameter("username", "%" + string + "%").getResultList();
}

provided your table sits in a form that you just updated you could display the List in it.

 <p:dataTable var="user" value="#{myBean.userList}">  
    <p:column headerText="Name">  
        <h:outputText value="#{user.name}" />  
    </p:column>  
 </p:dataTable>  


来源:https://stackoverflow.com/questions/20098626/how-iterate-over-listt-and-render-each-item-in-jsf-facelets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!