简体   繁体   English

如何遍历列表 <T> 并在JSF Facelets中渲染每个项目

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

I am wondering how to display a List<T> as obtained below in a Facelet: 我想知道如何在Facelet中显示以下获得的List<T>

public List<T> searchByString(String string) {
    return getEntityManager().createNamedQuery("Userdetails.findByUsername").setParameter("username", "%" + string + "%").getResultList();
}

Would a <h:dataTable> be a suitable way? <h:dataTable>是合适的方法吗?

You're going need to iterate over it. 您将需要对其进行迭代。 JSF 2 offers three iteration components out the box. JSF 2提供了三个迭代组件。 Provided that the User entity look like below, 假设User实体如下所示,

@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} , 并且将搜索结果分配为bean的List<User> users属性,该属性可以作为#{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> ,这是一个生成HTML <table>的UI组件。

     <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 eg <ul><li> , or <dl><dt><dd> , or <div><span> , etc): <ui:repeat> ,一个不会生成HTML标记的UI组件(因此,您必须自己以所需的方式编写所有HTML,可以轻松将其更改为例如<ul><li><dl><dt><dd><div><span>等):

     <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: <c:forEach> ,一个在视图构建时而不是视图渲染时运行的标记处理程序(背景说明在这里: JSF2 Facelets中的JSTL ...有意义吗? ),它也不会产生任何HTML标记:

     <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: 也可以看看:

You can save your List in a class variable, give it a getter and (maybe) a setter. 您可以将List保存在一个类变量中,给它一个getter和(也许)一个setter。 Declare searchByString method as void and call it with let's say a (provided you are using PrimeFaces): searchByString方法声明为void ,并用a进行调用(假设您使用的是PrimeFaces):

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

myBean: 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. 只要您的表格位于您刚刚更新的表格中,就可以在其中显示List

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:如何遍历列表列表,同时遍历每个嵌套列表中的每个项目 - Python: how to iterate over list of list while iterating over each item in each nested list 如何遍历列表以删除项目索引? - How to iterate over a list to remove index of item? 如何遍历列表中的每个元素? - How to iterate over each element in a list? Flutter 如何遍历 ListView 中的每个项目? - Flutter How do I iterate over each item in a ListView? 为什么我的清单不遍历每个清单? - Why won't my list iterate over each one? 对于列表中的每个项目,遍历另一个列表的所有其他项目,输出数组-python - For each item in a list, iterate over all other items of another list, output an array - python 如何遍历字符串列表中的每个字符串并对其元素进行操作? - How to iterate over each string in a list of strings and operate on its elements? 如何遍历字符串列表并打印每个项目? - How do I iterate through a list of strings and print each item? 在python中的列表中每个项目的左侧和右侧迭代n个元素的切片 - Iterate over slices of n elements to the left and right of each item in a list in python 如何在将列表中的每个值传递到 API 并在每个列表列表后暂停时迭代列表列表? - How to iterate over list of lists while passing each value in the lists into API and pausing after each list of list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM