0

我在 xhtml 中将值设置为对象,但在托管 bean 中它变为 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>

托管豆:

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,因为我正在发送组的 id,因此它不能转换 Groups 对象但不能抛出任何异常。之后我写了一个自定义转换器,我想自定义转换器在我更改 xhtml 之前可以很好地工作。

<f:selectItems value="#{userBean.groupList}" var="grp" itemLabel="#{grp.groupName}" itemValue="#{grp}"/>
4

0 回答 0