1

我的数据表

<h:form>
    <h:panelGroup>
        <h:dataTable columnClasses="input-radio,input-label" id="account-table" value="#{accountController.items}" var="item" border="0">
            <h:column>
                <h:outputText value="#{item.id}"/>
            </h:column>
            <h:column>
                <h:outputText value="#{item.name}"/>
            </h:column> 
        </h:dataTable>
    </h:panelGroup>
</h:form>

我有一个与用户实体相关的帐户实体。有没有办法从帐户中检索用户数据,而不是为帐户和用户制作 2 个数据表?

getItems 方法

public DataModel getItems() {
    if (items == null) {
        items = getPagination().createPageDataModel();
    }
    return items;

}

使用 JSF 2 并且使用 CRUD 自动生成管理 bean

4

1 回答 1

1

如果DataModel该类可以访问user实体,则可以通过链接对象来遍历 JSF 页面中的关系:

  • #{item.name}- 访问项目名称
  • #{item.user.name}- 如果用户在 item 中有 getter,则访问用户名

例如你的DataModel

public class DataModel {

    private Long id;
    private String name;
    private User user;
    ...
}

JSF 页面:

<h:form>
    <h:panelGroup>
        <h:dataTable columnClasses="input-radio,input-label" id="account-table" value="#{accountController.items}" var="item" border="0">
            <h:column>
                <h:outputText value="#{item.id}"/>
            </h:column>
            <h:column>
                <h:outputText value="#{item.user.name}"/> <!-- traverse to user entity here -->
            </h:column> 
        </h:dataTable>
    </h:panelGroup>
</h:form>
于 2013-02-28T06:35:24.873 回答