在两个 bean 上使用 ViewScope 我通过 commandLink 使用 f:param 将值从一个 bean (browseUsers) 传递到另一个 bean,但在 bean 2 (editUsers) 上,当在构造函数上按下命令按钮并且所有值都丢失时。请指教。
browseUsers.xhtml 上的 commandlink 和 datatable 代码:
<h:form id="form">
<p:dataTable id="dataTable" var="users" value="#{browseUsers.userList}" rowKey="#{users.firstName}" widgetVar="usersTable" paginator="true" paginatorAlwaysVisible="false" rows="10"
filteredValue="#{browseUsers.filteredUser}" emptyMessage="No users found with given criteria">
<f:facet name="header">
<p:outputPanel>
<h:outputText value="Search all fields" />
<p:inputText id="globalFilter" onkeyup="usersTable.filter()" style="width:150px" />
</p:outputPanel>
</f:facet>
<p:column style="width:40px">
<h:panelGrid columns="3" styleClass="actions" cellpadding="2">
<p:commandLink action="#{browseUsers.editUser}" title="View">
<p:graphicImage styleClass="ui-icon ui-icon-pencil" style="display: inline-block;"/>
<h:outputText value="#{users.id}" />
<f:param name="userId" value="#{users.id}" />
</p:commandLink>
</h:panelGrid>
</p:column>
<p:column id="firstNameColumn" filterBy="#{users.firstName}" sortBy="#{users.firstName}" headerText="First Name" filterMatchMode="contains">
<h:outputText value="#{users.firstName}" />
</p:column>
<p:column id="lastNameColumn" filterBy="#{users.lastName}" sortBy="#{users.lastName}" headerText="Last Name" filterMatchMode="contains">
<h:outputText value="#{users.lastName}" />
</p:column>
<p:column id="userNameColumn" filterBy="#{users.userName}" sortBy="#{users.userName}" headerText="Username" filterMatchMode="contains">
<f:facet name="header">
<h:outputText value="Username" />
</f:facet>
<h:outputText value="#{users.userName}" />
</p:column>
</p:dataTable>
editUsers.xhtml 上的命令按钮代码如下:
<h:form id="saveForm">
<p:panelGrid id="display" columns="2" cellpadding="4" tableStyle="width:auto">
<h:outputText value="First Name:" />
<h:inputText value="#{editUsers.selectedUser.firstName}" id="firstName"/>
<h:outputText value="Last Name:" />
<h:inputText value="#{editUsers.selectedUser.lastName}" id="lastName"/>
<h:outputText value="Username:" />
<h:inputText value="#{editUsers.selectedUser.userName}" id="userName"/>
<h:outputText value="Password:" />
<h:inputText value="#{editUsers.selectedUser.password}" id="password"/>
<p:commandButton value="Reset" type="reset" />
<p:commandButton value="Submit" actionListener="#{editUsers.saveUser}" action="#{editUsers.browseUser}" ajax="false" />
</p:panelGrid>
</h:form>
editUsers.java 上的 bean 方法如下:
@ManagedBean(name = "editUsers")
@ViewScoped
公共类editUsers实现可序列化{
private List<User> userList;
private User selectedUser;
private Long userId;
public editUsers()
{
AdminFacadeRemote afr = ContextUtil.contextLookup(AdminFacadeRemote.class, AdminFacade.class);
setSelectedUser(new User());
}
@PostConstruct
public void initialize()
{
String s = JSFContextUtil.getRequestParameter("userId");
if (! StringUtils.isEmpty(s))
{
userId = Long.parseLong(s);
try {
AdminFacadeRemote afr = ContextUtil.contextLookup(AdminFacadeRemote.class, AdminFacade.class);
User u = afr.findUser(userId);
setSelectedUser(u);
}
catch (Exception e){
e.printStackTrace();
}
}
}
public void saveUser(ActionEvent event)
{
AdminFacadeRemote afr = ContextUtil.contextLookup(AdminFacadeRemote.class, AdminFacade.class);
afr.saveUsers(selectedUser);
}
public String browseUser()
{
return "browseusers";
}