简体   繁体   English

获取ui:托管bean中的param值

[英]Getting ui:param value in Managed bean

I have the following p:tabView : 我有以下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>

The two tabs both of them have same include, ie, userList.xhtml . 两个选项卡都具有相同的include,即userList.xhtml Inside this userList.xhtml I have placed a h:outputText to see the value of ui:param showOnlyActiveUsers for testing purpose: 在这个userList.xhtml我放置了一个h:outputText来查看ui:param showOnlyActiveUsers的值以进行测试:

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

The value of this h:outputText changes from true to false on tab change and vice versa. h:outputText的值在选项卡更改时从true更改为false ,反之亦然。

The managed bean UserListController which is in view scope is the back bone for userList.xhtml . 在视图范围内的托管bean UserListControlleruserList.xhtml的后端。 Since it is in view scope it is instantiating once for all these two tabs. 因为它在视图范围内,所以对所有这两个选项卡实例化一次。

I have a class UserDataModel which extends LazyDataModel of Primefaces. 我有一个UserDataModel类,它扩展了LazyDataModel的LazyDataModel。 An instance of this class is present in UserListController . UserListController存在此类的实例。 I am trying to set a value to a field of UserDataModel from the getter method of a h:inputHidden : 我试图从h:inputHidden的getter方法设置一个UserDataModel字段的值:

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

which is in userList.xhtml from UserListController as: 来自UserListController userList.xhtml中:

public String getShowActiveOnly() {
    ValueExpression expression = getFacesContext().getApplication().getExpressionFactory().createValueExpression(getFaceletContext(), "#{showOnlyActiveUsers}", Boolean.class);
    userDataModel.setShowOnlyActiveUsers(expression.getValue(getFaceletContext());
    return "";
}

BUT the value which is generated by expression.getValue(getFaceletContext() is always false . I was wondering why the value in the h:outputText changes from true to false , but the value generated by ValueExpression is always false ? 但是由expression.getValue(getFaceletContext()生成的值expression.getValue(getFaceletContext()总是为false 。我想知道为什么h:outputText的值从true变为false ,但ValueExpression生成的值总是为false

How can I solve this issue. 我该如何解决这个问题。 Any pointer would be very helpful to me. 任何指针都对我很有帮助。

Edit 编辑

protected final FaceletContext getFaceletContext() {
    return (FaceletContext) getFacesContext().getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
}

What I want to achieve is to pass the ui:param value to the managed bean and from there to the data model. 我想要实现的是将ui:param值传递给托管bean并从那里传递给数据模型。

Edit 编辑

userList.xhtml : 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>

What I want is to get the current value of that ui:param from the userList.xhtml and pass it to the managed bean by some event and set it to the datamodel . 我想要的是从userList.xhtml获取该ui:param的当前值,并通过某个事件将其传递给托管bean,并将其设置为datamodel It is also true that the datamodel is instantiating only once when the managed bean is created as it is in view scope. 同样,在创建托管bean时, datamodel仅在视图范围内实例化一次。

you can use something like this to display active users , 你可以使用这样的东西来显示活跃的用户,

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

also you can use boolean expression in this format 你也可以使用这种格式的布尔表达式

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

Other solution could be to pass list of users to your ui include .Keep two methods 'userListController.userList' and 'userListController.activeUserList' and pass this as ui:param to your usersList.xhtml 其他解决方案可能是将用户列表传递给你的ui include。保留两个方法'userListController.userList'和'userListController.activeUserList'并将其作为ui:param传递给你的usersList.xhtml

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM