【发布时间】:2013-02-16 23:38:53
【问题描述】:
我有以下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 UserListController 是 userList.xhtml 的主干。由于它在视图范围内,因此它会为所有这两个选项卡实例化一次。
我有一个类 UserDataModel,它扩展了 Primefaces 的 LazyDataModel。此类的一个实例存在于UserListController 中。我正在尝试从h:inputHidden 的getter 方法为UserDataModel 的字段设置一个值:
<h:inputHidden value="#{userListController.showActiveOnly}" />
在userList.xhtml 中来自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);
}
我想要实现的是将 ui:param 值传递给托管 bean,然后从那里传递给数据模型。
编辑
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>
我想要的是从userList.xhtml 中获取ui:param 的当前值,并通过某个事件将其传递给托管bean 并将其设置为datamodel。同样,datamodel 在创建托管 bean 时仅实例化一次,因为它在视图范围内。
【问题讨论】: