简体   繁体   中英

Getting ui:param value in Managed bean

I have the following 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 . Inside this userList.xhtml I have placed a h:outputText to see the value of ui:param showOnlyActiveUsers for testing purpose:

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

The value of this h:outputText changes from true to false on tab change and vice versa.

The managed bean UserListController which is in view scope is the back bone for userList.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. An instance of this class is present in UserListController . I am trying to set a value to a field of UserDataModel from the getter method of a h:inputHidden :

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

which is in userList.xhtml from UserListController as:

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 ?

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.

Edit

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 . It is also true that the datamodel is instantiating only once when the managed bean is created as it is in view scope.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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