我有一个使用 PrimeFaces 3.3 数据表创建的数据表。该表是从数据库中填充的。
在行选择中,我填充了类似的字段进行编辑。编辑后,我想写入数据库。同样,我希望能够在文本项中创建新记录,并且一旦用户将其保存,数据表就会被刷新。
数据选择工作正常,但是,当我想不出添加/编辑/删除的方法时,不同的错误会带来不同的方法,我已经尝试了所有在谷歌上可以找到的方法。这些错误有时是逻辑错误,有时是 nullpointerexception 或类启动错误。我已经在网上尝试了十几个用于 jtable + crud 的示例,但似乎没有什么对我有用。
例如,数据显示在数据表中,并且文本字段中的即时选择也已完成,但是当我单击添加按钮时,没有任何反应,我希望“编辑属性”面板中的字段被清除,有时我得到一个NullPointerException
,即使我修改文本项中的值并按保存,不写入值。当我调试时,我发现错误正在em.getTransaction().begin
排队。
如果我em.joinTransaction
再次使用它会在提交语句中引发错误。由于我是 Java EE 的新手,我认为我犯了一些可怕的错误。
如果有人可以举一些类似场景的例子,我将不胜感激:jtable + 绑定字段 + 添加/编辑/删除操作。
下面是我的代码:
@Entity
@Table(name = "users")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Users.findAll", query = "SELECT u FROM Users u"),
@NamedQuery(name = "Users.findByIduser", query = "SELECT u FROM Users u WHERE u.iduser = :iduser"),
@NamedQuery(name = "Users.findByLogin_Name", query = "SELECT u FROM Users u WHERE u.login_name = :login_name"),
@NamedQuery(name = "Users.findByFullName", query = "SELECT u FROM Users u WHERE u.fullName = :fullName"),
@NamedQuery(name = "Users.findByPassword", query = "SELECT u FROM Users u WHERE u.password = :password"),
@NamedQuery(name = "Users.authenticate", query = "SELECT u FROM Users u WHERE u.login_name = :login_name and u.password = :password and u.active=1"),
@NamedQuery(name = "Users.findByActive", query = "SELECT u FROM Users u WHERE u.active = :active"),
@NamedQuery(name = "Users.findByUserType", query = "SELECT u FROM Users u WHERE u.userType = :userType"),
@NamedQuery(name = "Users.findByEmail", query = "SELECT u FROM Users u WHERE u.email = :email"),
@NamedQuery(name = "Users.findByPhone", query = "SELECT u FROM Users u WHERE u.phone = :phone"),
@NamedQuery(name = "Users.findByCreated", query = "SELECT u FROM Users u WHERE u.created = :created"),
@NamedQuery(name = "Users.findByUpdated", query = "SELECT u FROM Users u WHERE u.updated = :updated")})
public class Users implements Serializable {
@Basic(optional = false)
@NotNull
@Column(name = "created")
@Temporal(TemporalType.TIMESTAMP)
private Date created;
@Basic(optional = false)
@NotNull
@Column(name = "updated")
@Temporal(TemporalType.TIMESTAMP)
private Date updated;
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO )
@Basic(optional = false)
@NotNull
@Column(name = "iduser")
private Integer iduser;
//getter & setters ....
@Stateless
public class UsersFacade extends AbstractFacade<Users> {
@PersistenceContext(unitName = "VUProjectPU")
private EntityManager em;
///////////////////////////////////////////////////////////////////////////////
@Override
protected EntityManager getEntityManager() {
return em;
}
///////////////////////////////////////////////////////////////////////////////
public UsersFacade() {
super(Users.class);
}
///////////////////////////////////////////////////////////////////////////////
public Users authenticate(String login_id, String pwd) {
List <Users> userList = new ArrayList();
Users user =null;
userList = em.createNamedQuery("Users.authenticate")
.setParameter("login_name", login_id)
.setParameter("password", pwd)
.getResultList();
if (userList.isEmpty()) {
return null;
}
else{
user = userList.get(0);
}
return user;
}
///////////////////////////////////////////////////////////////////////////////
public Users deactivateUser (Users u){
u.setActive(false);
return u;
}
///////////////////////////////////////////////////////////////////////////////
public Users activateUser (Users u){
u.setActive(true);
return u;
}
///////////////////////////////////////////////////////////////////////////////
public Users createAnalyst(Users u){
u.setActive(false);
u.setUserType("Analyst");
u.setCreated(new Date());
u.setUpdated(new Date());
em.persist(u);
em.flush();
em.refresh(u);
return u;
}
}
@ManagedBean
@ViewScoped
public class UsersList implements Serializable {
private List<Users> uList;
private Users selUser ;
private Users user;
private String operation;
private @EJB UsersFacade ufs;
private FacesContext context;
private FacesMessage msg;
private Boolean edit=false;
public UsersList(){
ufs = new UsersFacade();
}
public void getAllUsersList(){
uList=ufs.findAll();
}
public Boolean isEdit() {
return edit;
}
public void add(){
context = FacesContext.getCurrentInstance();
operation = "new";
edit=true;
selUser = new Users();
}
public void save(){
context = FacesContext.getCurrentInstance();
if (operation.equals("new")){
ufs.create(selUser);
}
ufs.save(selUser);
operation = null;
msg = new FacesMessage(FacesMessage.SEVERITY_INFO,"Success", "Record Saved");
context.addMessage(null, msg);
edit=false;
getAllUsersList(); //refresh table
}
public void delete(Users u){
context = FacesContext.getCurrentInstance();
ufs.delete(u);
msg = new FacesMessage(FacesMessage.SEVERITY_INFO,"Success", "Record Deleted");
context.addMessage(null, msg);
}
public void refresh(){
context = FacesContext.getCurrentInstance();
getAllUsersList();
msg = new FacesMessage(FacesMessage.SEVERITY_INFO,"Success", "Data Refreshed");
context.addMessage(null, msg);
}
public List<Users> getuList() {
uList = ufs.findAll();
return uList;
}
public void setuList(List<Users> uList) {
this.uList = uList;
}
public Users getSelUser() {
return selUser;
}
public void setSelUser(Users selUser) {
this.selUser = selUser;
}
public void onRowSelect(SelectEvent event) {
edit=true;
context = FacesContext.getCurrentInstance();
msg = new FacesMessage("User Selected", ((Users) event.getObject()).getLogin_name());
context.addMessage(null, msg);
}
public void onRowUnSelect(UnselectEvent event) {
edit=false;
context = FacesContext.getCurrentInstance();
msg = new FacesMessage("User Unselected", ((Users) event.getObject()).getLogin_name());
context.addMessage(null, msg);
}
public Users getUser() {
return user;
}
}
below is user.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core">
<body>
<ui:composition template="./../../WEB-INF/Templates/Template.xhtml">
<ui:define name="menu">
</ui:define>
<ui:define name="content">
<h:form id="form">
<p:growl id="growl" showDetail="true"/>
<p:dataTable id="utable" var="users"
value="#{usersList.getuList()}"
selection="#{usersList.selUser}"
rowKey="#{users.iduser}"
selectionMode="single"
paginator="true" rows="5"
paginatorPosition="bottom"
editable="true"
>
<p:ajax event="rowSelect" listener="#{usersList.onRowSelect}" update=":form:display :form:growl" />
<p:ajax event="rowUnselect" listener="#{usersList.onRowUnSelect}" update=":form:growl"/>
<f:facet name="header">
List of Users
</f:facet>
<p:column id="login" sortBy ="#{users.login_name}">
<f:facet name="header">Login</f:facet>
<h:outputText value="#{users.login_name}" />
</p:column>
<p:column id="fullname" sortBy ="#{users.fullName}">
<f:facet name="header">User Name</f:facet>
<h:outputText value="#{users.fullName}" />
</p:column>
<p:column id="email" sortBy ="#{users.email}">
<f:facet name="header">E-mail</f:facet>
<h:outputText value="#{users.email}" />
</p:column>
<p:column id="phone" sortBy ="#{users.phone}">
<f:facet name="header">Phone</f:facet>
<h:outputText value="#{users.phone}" />
</p:column>
<p:column id="created" sortBy ="#{users.created}">
<f:facet name="header">Created On</f:facet>
<h:outputText value="#{users.created}" />
</p:column>
<p:column id="active" sortBy ="#{users.active}">
<f:facet name="header">Active</f:facet>
<h:outputText value="#{users.active}" />
</p:column>
<p:column id="userType" sortBy ="#{users.userType}">
<f:facet name="header">User Type</f:facet>
<h:outputText value="#{users.userType}" />
</p:column >
</p:dataTable>
<p:panel header="Edit User Properties">
<h:panelGrid id="display" columns="6" cellpadding="4" >
<h:outputText value="Login:" />
<p:inputText readonly="#{usersList.edit}"
value="#{usersList[usersList.edit ? 'user' : 'selUser'].login_name}" />
<h:outputText value="Password:" />
<p:inputText readonly="#{usersList.edit}"
value="#{usersList[usersList.edit ? 'user' : 'selUser'].password}" />
<h:outputText value="User Name:" />
<p:inputText readonly="#{usersList.edit}"
value="#{usersList[usersList.edit ? 'user' : 'selUser'].fullName}" />
<h:outputText value="E-mail:" />
<p:inputText readonly="#{usersList.edit}"
value="#{usersList[usersList.edit ? 'user' : 'selUser'].email}" />
<h:outputText value="Phone:" />
<p:inputText readonly="#{usersList.edit}"
value="#{usersList[usersList.edit ? 'user' : 'selUser'].phone}" />
<h:outputText value="User Type:" />
<p:inputText readonly="#{usersList.edit}"
value="#{usersList[usersList.edit ? 'user' : 'selUser'].userType}" />
<h:outputText value="Active:" />
<p:selectBooleanCheckbox
value="#{usersList[usersList.edit ? 'user' : 'selUser'].active}"/>
</h:panelGrid>
<h:panelGrid id="command" columns="6" cellpadding="4" >
<p:commandButton id="new" value="New" actionListener="#{usersList.add()}"
update="result,utable,display"
rendered="#{usersList.edit}}">
</p:commandButton>
<p:commandButton id="save" value="Save" actionListener="#{usersList.save()}" update="result utable">
</p:commandButton>
<p:commandButton id="delete" value="Delete" actionListener="#{usersList.delete()}" update="result utable">
</p:commandButton>
<p:commandButton id="refresh" value="Refresh" actionListener="#{usersList.refresh()}" update="result utable">
</p:commandButton>
</h:panelGrid>
<p:messages id="result" showDetail="true" autoUpdate="true"/>
</p:panel>
</h:form>
</ui:define>
</ui:composition>
</body>
</html>