我在使用素数面获取表格字段下的行选择值时遇到问题。要求是当我选择行时,相关数据和相关信息必须显示在同一页面上,就像我只想在表格下显示特定用户的性别和地址一样。
我正在使用 jsf 2.0 和 prime faces 3.5
这是我的代码,请检查我有错误的地方
package com.poc.faces;
all imports
.............
..............
@ManagedBean
@SessionScoped
public class UserManagedBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
public static List<User> list = new ArrayList<User>();
static {
list.add(new User(3, "ankurjadiya", "jadiyaankur@gmail.com",
"88849113", new Date(), "M", "sare ga ma pa"));
list.add(new User(1, "Administrator", "admin@gmail.com", "9000510456",
new Date(), "M", "Hyderabad"));
list.add(new User(2, "Guest", "guest@gmail.com", "9247469543",
new Date(), "M", "Hyderabad"));
list.add(new User(3, "John", "John@gmail.com", "9000510456",
new Date(), "M", "Hyderabad"));
list.add(new User(4, "Paul", "Paul@gmail.com", "9247469543",
new Date(), "M", "Hyderabad"));
list.add(new User(5, "raju", "raju@gmail.com", "9000510456",
new Date(), "M", "Hyderabad"));
}
private String username;
private String password;
private String searchUser;
private User selectedUser;
private UserDataModel dataModel;
public UserManagedBean() {
this.dataModel = new UserDataModel(list);
}
setter and getter of fields
..............
...............
public String login() {
if ("ankur".equalsIgnoreCase(getUsername())
&& "ankur".equals(getPassword())) {
return "home";
} else {
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage("username", new FacesMessage(
"Invalid UserName and Password"));
return "login";
}
}
用户数据模型类
public class UserDataModel extends ListDataModel<User> implements Serializable,
SelectableDataModel<User> {
public UserDataModel() {
}
public UserDataModel(List<User> users) {
super(users);
}
public User getRowData(String username) {
List<User> list = (List<User>) getWrappedData();
for (User user : list) {
if (user.getUsername().equals(username))
return user;
}
return null;
}
public Object getRowKey(User user) {
// TODO Auto-generated method stub
return user.getUsername();
}
}
public void onRowSelect(SelectEvent event) {
FacesMessage msg = new FacesMessage("User Selected",
((User) event.getObject()).getUsername());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public void onRowUnselect(UnselectEvent event) {
FacesMessage msg = new FacesMessage("User Unselected",
((User) event.getObject()).getUsername());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
这是我检索值的 home.xml
<html xmlns="http://www.w3c.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<link href="../css/style.css" type="text/css" rel="stylesheet" />
</h:head>
<h:body>
<h:form prependId="false" id="form">
<p:layout style="min-width:400px;min-height:200px;" id="layout"
fullPage="true">
<p:layoutUnit position="west" header="Menu" collapsible="true">
<p:menu>
<p:submenu label="Resources">
<p:menuitem value="Option1"
action="#{userManagedBean.getSearchUsersResults()}"
update="left_column" />
</p:submenu>
</p:menu>
</p:layoutUnit>
<p:layoutUnit position="center">
<p:panel id="left_column"
style="width: 2px; left: 2px; margin-left: 2px">
<center>
<H1>UserData</H1>
<br />
<p:dataTable value="#{userManagedBean.dataModel}" var="user"
pagination="true" rows="5" selection="#{userManagedBean.selectedUser}" selectionMode="single" >
<p:column>
<f:facet name="header">UserId</f:facet>
<h:outputText value="#{user.userId}"></h:outputText>
</p:column>
<p:column>
<f:facet name="header">Username</f:facet>
<h:outputText value="#{user.username}"></h:outputText>
</p:column>
<p:column>
<f:facet name="header">EmailId</f:facet>
<h:outputText value=" #{user.emailId}"></h:outputText>
</p:column>
<p:column>
<f:facet name="header">Phone</f:facet>
<h:outputText value="#{user.phone}"></h:outputText>
</p:column>
</p:dataTable>
</center>
</p:panel>
<h:panelGrid id="display" columns="2" cellpadding="4">
<h:outputText value="Gender:" />
<h:inputText value="#{userManagedBean.selectedUser.gender}" />
<h:outputText value="Address:" />
<h:inputText value="#{userManagedBean.selectedUser.address}" />
</h:panelGrid>
` </p:layoutUnit>
<p:layoutUnit position="south" footer="bottom">
</p:layoutUnit>
</p:layout>
</h:form>
</h:body>
</html>