获取ui:托管bean中的param值

时间:2013-03-02 13:50:43

标签: java jsf jsf-2 facelets

我有以下p:tabView

<p:tabView id="tabView" dynamic="true" cache="false" activeIndex="#{navigationController.activeTabIndex}"
    onTabChange="tabChangeCommand([{name:'index', value:index}])">
    <p:tab title="#{adbBundle['nav.manageUser']}" id="manageUserTab">
        <h:panelGroup layout="block">
            <h:form id="manageUserListForm">
                <ui:include src="/WEB-INF/includes/userList.xhtml">
                    <ui:param name="showOnlyActiveUsers" value="#{true}" />
                </ui:include>
            </h:form>
        </h:panelGroup>
    </p:tab>
    <p:tab title="#{adbBundle['nav.manageRole']}" id="manageRoleTab">
        <h:panelGroup layout="block">
            <h:form id="manageRoleListForm">
                <ui:include src="/WEB-INF/includes/userList.xhtml">
                    <ui:param name="manageRole" value="manageRole" />
                    <ui:param name="showOnlyActiveUsers" value="#{false}" />
                </ui:include>
            </h:form>
        </h:panelGroup>
    </p:tab>
</p:tabView>

两个标签都有相同的包括,即userList.xhtml。在userList.xhtml内,我放置了h:outputText以查看ui:param showOnlyActiveUsers的值以进行测试:

<h:outputText id="showOnlyActiveUsers" value="#{showOnlyActiveUsers}" />

h:outputText的值在标签更改时从true更改为false,反之亦然。

视图范围内的托管bean UserListControlleruserList.xhtml的后端。由于它在视图范围内,因此对所有这两个选项卡实例化一次。

我有一个UserDataModel课,它扩展了Primefaces的LazyDataModel个。 UserListController中存在此类的实例。我正在尝试从UserDataModel的getter方法将值设置为h:inputHidden字段:

<h:inputHidden value="#{userListController.showActiveOnly}" />

位于userList.xhtml的{​​{1}}中:

UserListController

public String getShowActiveOnly() { ValueExpression expression = getFacesContext().getApplication().getExpressionFactory().createValueExpression(getFaceletContext(), "#{showOnlyActiveUsers}", Boolean.class); userDataModel.setShowOnlyActiveUsers(expression.getValue(getFaceletContext()); return ""; } 生成的值始终为expression.getValue(getFaceletContext()。我想知道为什么false中的值会从h:outputText更改为true,但false生成的值始终为ValueExpression

我该如何解决这个问题。任何指针都对我很有帮助。

修改

false

我想要实现的是将protected final FaceletContext getFaceletContext() { return (FaceletContext) getFacesContext().getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY); } 值传递给托管bean并从那里传递给数据模型。

修改

ui:param

userList.xhtml

我想要的是从<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:p="http://primefaces.org/ui" xmlns:pe="http://primefaces.org/ui/extensions"> <h:form id="userListForm"> <p:panel id="userListPanel" header="#{adbBundle['userList.panel.header']}" toggleable="true"> <p:dataTable var="user" id="userTable" value="#{userListController.userDataModel}" lazy="true" paginator="true" rows="10" paginatorPosition="bottom" paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}" rowsPerPageTemplate="10,50,100" widgetVar="userDataTable" styleClass="userTable" selectionMode="single"> <f:facet name="header"> <p:outputPanel> <h:panelGroup layout="block" id="resetFiter" styleClass="dataTableResetFilter"> <p:outputLabel value="#{adbBundle['filter.resetText']}" /> <p:spacer width="10" /> <p:commandButton icon="ui-icon-circle-close" actionListener="#{userListController.resetUserTable}"/> </h:panelGroup> </p:outputPanel> </f:facet> <p:column id="nameColumn" headerText="#{adbBundle['name']}" sortBy="#{user.fullName}" filterBy="#{user.fullName}" styleClass="name"> <h:outputText value="#{user.fullName}" /> </p:column> <p:column id="statusColumn" headerText="#{adbBundle['status']}" sortBy="#{user.active}" styleClass="center status" filterBy="#{user.statusText}" filterMatchMode="exact" filterOptions="#{userListController.statusOptions}"> <h:outputText value="#{user.statusText}" /> </p:column> <p:column id="manageRoleColumn" headerText="#{adbBundle['role']}" sortBy="#{user.role}" styleClass="center manageRole" filterBy="#{user.role}" filterOptions="#{userListController.roleOptions}" rendered="#{manageRole != null and manageRole != ''}"> <h:panelGroup layout="block"> <p:selectOneRadio id="roleRadio" value="#{user.role}" styleClass="roleRadio"> <f:selectItem itemValue="user" itemLabel="User" /> <f:selectItem itemValue="manager" itemLabel="Manager" /> <p:ajax listener="#{userListController.changeRole}" /> <f:attribute name="user" value="#{user}" /> </p:selectOneRadio> </h:panelGroup> </p:column> </p:dataTable> </p:panel> </h:form> </ui:composition> 获取ui:param的当前值,并通过某个事件将其传递给托管bean,并将其设置为userList.xhtml。同样,datamodel在创建托管bean时只实例化一次,因为它在视图范围内。

1 个答案:

答案 0 :(得分:2)

您可以使用类似的内容来显示活跃用户

<h:form id="userListForm">
    <p:panel id="userListPanel" header="#{adbBundle['userList.panel.header']}" toggleable="true">
        <p:dataTable var="user" id="userTable" value="#{userListController.userDataModel}" lazy="true" paginator="true" rows="10"
            paginatorPosition="bottom"
            paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
            rowsPerPageTemplate="10,50,100" widgetVar="userDataTable" styleClass="userTable" selectionMode="single">

            <f:facet name="header">
                <p:outputPanel>
                    <h:panelGroup layout="block" id="resetFiter" styleClass="dataTableResetFilter">
                        <p:outputLabel value="#{adbBundle['filter.resetText']}" />
                        <p:spacer width="10" />
                        <p:commandButton icon="ui-icon-circle-close" actionListener="#{userListController.resetUserTable}"/>
                    </h:panelGroup>
                </p:outputPanel>
            </f:facet>
            <ui:fragment rendered="#{showOnlyActiveUsers and user.active}">
            <p:column id="nameColumn" headerText="#{adbBundle['name']}" sortBy="#{user.fullName}" filterBy="#{user.fullName}" styleClass="name">
                <h:outputText value="#{user.fullName}" />
            </p:column>

            <p:column id="statusColumn" headerText="#{adbBundle['status']}" sortBy="#{user.active}" styleClass="center status" filterBy="#{user.statusText}"
                filterMatchMode="exact" filterOptions="#{userListController.statusOptions}">
                <h:outputText value="#{user.statusText}" />
            </p:column>

            <p:column id="manageRoleColumn" headerText="#{adbBundle['role']}" sortBy="#{user.role}" styleClass="center manageRole" filterBy="#{user.role}"
                filterOptions="#{userListController.roleOptions}" rendered="#{manageRole != null and manageRole != ''}">
                <h:panelGroup layout="block">
                    <p:selectOneRadio id="roleRadio" value="#{user.role}" styleClass="roleRadio">
                        <f:selectItem itemValue="user" itemLabel="User" />
                        <f:selectItem itemValue="manager" itemLabel="Manager" />

                        <p:ajax listener="#{userListController.changeRole}" />
                        <f:attribute name="user" value="#{user}" />
                    </p:selectOneRadio>
                </h:panelGroup>
            </p:column>
             </ui:fragment>   
        </p:dataTable>
    </p:panel>

</h:form>

你也可以使用这种格式的布尔表达式

<ui:param name="showOnlyActiveUsers" value="false" />

其他解决方案可能是将用户列表传递给你的ui include。保留两个方法'userListController.userList'和'userListController.activeUserList'并将其作为ui:param传递给你的usersList.xhtml