问题描述:

my datatable

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

i have an account entity that is in relation with a user entity. Is there anyway to retrieve user datas from account rather than making 2 datatables for account and user?

getItems method

public DataModel getItems() {

if (items == null) {

items = getPagination().createPageDataModel();

}

return items;

}

using JSF 2 and the manage beans are autogenerated using CRUD

网友答案:

If the DataModel class has access to the user entity, you can just traverse the relationship in your JSF page by chaining the objects:

  • #{item.name} - access to the item name
  • #{item.user.name} - access to the users name, if the user has a getter in item

E.g. your DataModel:

public class DataModel {

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

The JSF page:

<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>
相关阅读:
Top