有人可以帮我解决这个问题。我正在尝试在 JSF2.0 (xhtml page) 中创建一个用于显示、编辑和删除 UserGroup (MySQL DB 表) 中的数据的页面,该页面具有以下结构。我遇到的问题是数据实际上显示为 UserGroup 表的对象,而不是这些列中的可读值。我尝试了各种可用于 CRUD Web 应用程序的示例解决方案,但没有得到任何结果,我确定我缺少对显示和编辑行数据的整体理解。但是我的创建工作正常。我是否需要编写转换器来显示正确的值?
附加信息
- Glassfish v3.1.2
- JPA/Eclipselink v2.1
- 数据库
- JSF2.0
- 日食朱诺 SR2
用户组表
- rowId(自动生成)
- groupId(组表中组 ID 的外键)
- 用户名(用户表中用户名的外键)
我现在得到的输出
Users's and their group.
Row Id Group Id Username
9 model.Group@647d73bf model.User@683a3423
13 model.Group@2192bac5 model.User@2823ecbb
XHTML 代码
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html 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">
<f:metadata>
<f:viewParam name="userId" value="#{loginBean.username}"></f:viewParam>
</f:metadata>
<ui:composition>
<h:head>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1" />
<h1>Users's and their group.</h1>
<title>Displaying Users and their Groups</title>
</h:head>
<h:body>
<h:form id="form">
<h:dataTable value="#{userGroupManagedBean.userGroup}" var="item">
<h:column>
<f:facet name="header"> Row Id</f:facet> #{item.rowId}
</h:column>
<h:column>
<f:facet name="header"> Group Id</f:facet> #{item.group}
</h:column>
<h:column>
<f:facet name="header"> Username</f:facet> #{item.user}
</h:column>
</h:dataTable>
</h:form>
</h:body>
</ui:composition>
</html>
*托管 Bean 类
package beans;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
//import javax.faces.model.DataModel;
//import javax.faces.model.ListDataModel;
import model.UserGroup;
import ejb.UserGroupDaoBean;
@ManagedBean(name = "userGroupManagedBean", eager = true)
@SessionScoped
public class UserGroupManageBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@EJB
private UserGroupDaoBean uGDB;
// private DataModel<UserGroup> userGroup;
private List<UserGroup> userGroup;
private UserGroup currentUserGroup;
public String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getGroupId() {
return groupId;
}
public void setGroupId(int groupId) {
this.groupId = groupId;
}
public int groupId;
// public void setUserGroup(DataModel<UserGroup> userGroup) {
// this.userGroup = userGroup;
// }
public void setUserGroup(List<UserGroup> userGroup) {
this.userGroup = userGroup;
}
public UserGroup getCurrentUserGroup() {
return currentUserGroup;
}
@PostConstruct
public void init() {
getUserGroup();
}
public List<UserGroup> getUserGroup() {
List<UserGroup> myAllUserGroups = new ArrayList<UserGroup>(
uGDB.getAllUserGroups());
return myAllUserGroups;
}
// public DataModel<UserGroup> getUserGroup() {
// userGroup = new ListDataModel<UserGroup>(uGDB.getAllUserGroups());
// System.out.println(userGroup);
// return userGroup;
// }
// public String delete() {
// UserGroup myUserGroup = userGroup.getRowData();
// uGDB.deleteUserGroup(myUserGroup);
// getUserGroup();
// return
// "displayUserGroup.jsf?faces-redirect=true&includeViewParams=true";
// }
public String delete(UserGroup currentUserGroup) {
userGroup.remove(currentUserGroup);
getUserGroup();
return "displayUserGroup.jsf?faces-redirect=true&includeViewParams=true";
}
// public String edit(){
// currentUserGroup = userGroup.getRowData();
// System.out.println("And the current user group data is:" +
// currentUserGroup);
// getUserGroup();
// return
// "editUserGroup.jsf?faces-redirect=true&includeViewParams=true";
// }
public String save() {
uGDB.updateExistingUserGroup(currentUserGroup);
getUserGroup();
return "displayUserGroup.jsf?faces-redirect=true&includeViewParams=true";
}
public String cancel() {
// getUserGroup();
return "displayUserGroup.jsf?faces-redirect=true&includeViewParams=true";
}
public void setCurrentUserGroup(UserGroup currentUserGroup) {
this.currentUserGroup = currentUserGroup;
}
}
控制器类
package ejb;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import javax.persistence.PersistenceContext;
import model.Group;
import model.User;
import model.UserGroup;
@Stateful
@LocalBean
public class UserGroupDaoBean {
@PersistenceContext(unitName = "myPU")
private EntityManager entityManager;
public UserGroupDaoBean() {
}
public UserGroup createNewUserGroup(int groupId, String username) {
UserGroup newUserGrp = new UserGroup();
User myUsr;
myUsr = entityManager.find(User.class, username);
newUserGrp.setUser(myUsr);
Group myGrp;
myGrp = entityManager.find(Group.class, groupId);
newUserGrp.setGroup(myGrp);
saveNewUsrGrp(newUserGrp);
return newUserGrp;
}
private void saveNewUsrGrp(UserGroup usrGrp) {
entityManager.persist(usrGrp);
entityManager.flush();
}
public boolean checkUsertoGroup(String username, int groupId) {
Group chkGrp;
chkGrp = entityManager.find(Group.class, groupId);
User chkUsr;
chkUsr = entityManager.find(User.class, username);
if (chkGrp != null) {
if (chkUsr != null) {
try {
entityManager.createNamedQuery("findGroupsbyUser")
.setParameter("username", chkUsr)
.setParameter("groupId", chkGrp).getSingleResult();
System.out.println("UserGroup already exists");
return false;
} catch (NoResultException e) {
return true;
}
}
System.out.println("User doesn't exist");
return false;
}
System.out.println("Group doesn't exist");
return false;
}
public void deleteUserGroup(UserGroup userGroup) {
userGroup = entityManager.merge(userGroup);
entityManager.remove(userGroup);
}
public UserGroup update(UserGroup userGroup) {
UserGroup myUserGroup = entityManager.merge(userGroup);
return entityManager.merge(myUserGroup);
}
public UserGroup updateExistingUserGroup(UserGroup userGroup){
UserGroup myExistingUsrGrp = update(userGroup);
return myExistingUsrGrp;
}
@SuppressWarnings("unchecked")
public List<UserGroup> getAllUserGroups() {
try {
Query query = entityManager.createNamedQuery("findAllUserGroup");
List<UserGroup> result = (List<UserGroup>) query.getResultList();
return result;
} catch (NoResultException e) {
System.out.println("No Result found");
return null;
}
}
public boolean validateGroup(User username) {
Group groupId = entityManager.find(Group.class, 1);
try {
UserGroup myGroupId = (UserGroup) entityManager
.createNamedQuery("findAdminGroupId")
.setParameter("username", username)
.setParameter("groupId", groupId).getSingleResult();
if (myGroupId != null) {
System.out.println("This user is admin!!!");
return true;
}
} catch (NoResultException e) {
return false;
}
System.out.println("This user is not admin");
return false;
}
用户组 POJO
package model;
import java.io.Serializable;
import javax.persistence.*;
/**
* The persistent class for the UserGroup database table.
*
*/
@NamedQueries({
@NamedQuery(name = "findGroupsbyUser", query = "Select ug.group from UserGroup ug where ug.user=:username AND ug.group=:groupId"),
@NamedQuery(name = "findAllUserGroup", query="Select ug from UserGroup ug"),
@NamedQuery(name = "findAdminGroupId", query = "Select ug from UserGroup ug where ug.user=:username AND ug.group=:groupId"),
})
@Entity
@Table(name="usergroup")
public class UserGroup implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="RowId" )
private int rowId;
//bi-directional many-to-one association to Group
@ManyToOne
@JoinColumn(name="groupId")
private Group group;
//bi-directional many-to-one association to User
@ManyToOne
@JoinColumn(name="username")
private User user;
public UserGroup() {
}
public int getRowId() {
return this.rowId;
}
public void setRowId(int rowId) {
this.rowId = rowId;
}
public Group getGroup() {
return this.group;
}
public void setGroup(Group group) {
this.group = group;
}
public User getUser() {
return this.user;
}
public void setUser(User user) {
this.user = user;
}
}