如何迭代 List<T>并在 JSF Facelets 中渲染每个项目 [英] How iterate over List<T> and render each item in JSF Facelets

查看:27
本文介绍了如何迭代 List<T>并在 JSF Facelets 中渲染每个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在 Facelet 中显示如下获得的 List:

公共列表searchByString(字符串字符串){return getEntityManager().createNamedQuery("Userdetails.findByUsername").setParameter("username", "%" + string + "%").getResultList();}

是一种合适的方式吗?

解决方案

您将需要对其进行迭代.JSF 2 提供了三个开箱即用的迭代组件.假设 User 实体如下所示,

@Entity公共类用户{私人 @Id 长 ID;私人字符串用户名;私人字符串电子邮件;私人 LocalDate 生日;//添加/生成 getter+setter.}

并且搜索结果被分配为List;可用作 #{bean},

的 bean 的 users 属性

@Named @RequestScoped公共类豆{私人列表<用户>用户;//添加/生成 postconstruct+getter.}

这里有一些基于它的例子:

  1. <h:dataTable>,一个生成 HTML

    的 UI 组件.

    <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>,一个不生成 HTML 标记的 UI 组件(因此,您必须自己以所需的方式编写所有 HTML,可以很容易地将其更改为例如

    • ,或
      ,或

      > 等):

  3. <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>

  4. <c:forEach>,一个在视图构建时间而不是视图渲染时间运行的标签处理程序(背景解释在这里:JSF2 Facelets 中的 JSTL ......有意义吗?),它也不产生任何 HTML 标记:

    <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>

另见:

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,

@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:

这篇关于如何迭代 List&lt;T&gt;并在 JSF Facelets 中渲染每个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆