我在xhtml中设置了对象的值,但在托管bean中它变为null。
“用户”实体与“组”实体具有多对多关系,因此在形式上我动态地从db重新加载“组”,但在单击命令按钮“user”对象变为null之后。
这是我的xhtml:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Kullanıcı İşlemleri</title>
</h:head>
<h:body>
<h:form id="addUser">
<p:panel header="Kullanıcı Tanımlama">
<p:messages autoUpdate="true"/>
<h:panelGrid columns="2">
Adı:<p:inputText value="#{userBean.user.name}"/>
Soyadı : <p:inputText value="#{userBean.user.surname}"/>
Kullanıcı Adı : <p:inputText value="#{userBean.user.username}"/>
E-mail: <p:inputText value="#{userBean.user.email}"/>
Parola : <p:password value="#{userBean.user.password}"/>
Kullanıcı Tipi:
<p:selectManyCheckbox value="#{userBean.user.groupsCollection}">
<f:selectItems value="#{userBean.groupList}" var="grp" itemLabel="#{grp.groupName}" itemValue="#{grp.groupId}"/>
</p:selectManyCheckbox>
<p:commandButton value="Kullanıcıyı Ekle" action="#{userBean.persist()}" process="@this" update="@form" />
</h:panelGrid>
</p:panel>
</h:form>
</h:body>
Managed Bean:
package com.mkmturizm.bean;
import com.mkmturizm.entity.Groups;
import com.mkmturizm.entity.Users;
import com.mkmturizm.service.GroupService;
import com.mkmturizm.service.UserService;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
@ManagedBean
@ViewScoped
public class UserBean implements Serializable {
private Users user = new Users();
private List<Groups> groupList = new ArrayList<Groups>();
@EJB
UserService userService;
@EJB
GroupService groupService;
public void persist() throws Exception
{
userService.persist(user);
user = new Users();
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Kayıt başarıyla gerçeklerşti", ""));
}
@PostConstruct
public void getAllGroups()
{
groupList = groupService.groupsList();
}
public List<Groups> getGroupList()
{
return groupList;
}
public void setGroupList(List<Groups> groupList)
{
this.groupList = groupList;
}
public Users getUser()
{
return user;
}
public void setUser(Users user)
{
this.user = user;
}
}
更新:(已获批准)
我将所选项的值更改为grp因为我发送了group的id,所以它不能转换Groups对象但不能抛出任何异常。之后我写了一个自定义转换器,我想在我更改xhtml之前,自定义转换器可以正常工作。
<f:selectItems value="#{userBean.groupList}" var="grp" itemLabel="#{grp.groupName}" itemValue="#{grp}"/>