0

我想在 jsf 中为 OneToOne/ManyToOne 关系使用简单的下拉菜单

人.java

package com.aminpy.create;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;

@Entity
public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String firstName;
    private String lastName;
    private Phone phone;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @OneToOne
    public Phone getPhone() {
        return phone;
    }

    public void setPhone(Phone phone) {
        this.phone = phone;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return firstName + " " + lastName;
    }
}

托管 bean -> PersonPage.java

package com.aminpy.create;

import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

@ManagedBean
@SessionScoped
public class PersonPage {
    private Person person;
    private List<Phone> phoneList;
    private EntityManagerFactory emf;
    private EntityManager em;

    public Person getPerson() {
        if (person == null) {
            person = new Person();
        }
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    public List<Phone> getPhoneList() {
        if (phoneList == null) {
            emf = Persistence.createEntityManagerFactory("manager1");
            em = emf.createEntityManager();
            Query query = em.createQuery("select p from Phone p");
            phoneList = query.getResultList();
            em.close();
        }
        return phoneList;
    }

    public void setPhoneList(List<Phone> phoneList) {
        this.phoneList = phoneList;
    }

    public String btnCreate() {
        emf = Persistence.createEntityManagerFactory("manager1");
        em = emf.createEntityManager();

        em.getTransaction().begin();
        em.persist(person);
        em.getTransaction().commit();
        System.out.println("Record Stored!");
        em.close();

        return null;
    }
}

这是我的 index.xhtml 页面

<h:form id="person_form">
    <h:inputText value="#{personPage.person.firstName}" />
    <h:inputText value="#{personPage.person.lastName}" />
    <h:selectOneMenu value="#{personPage.person.phone}">
        <f:selectItems value="#{personPage.phoneList}" var="ph"
            itemLabel="#{ph.number}" itemValue="#{ph.id}" />
    </h:selectOneMenu>
    <h:commandButton value="save" action="#{personPage.btnCreate}" />
</h:form>

我的关系在 main() 函数中没有任何问题,但是当我提交带有此错误的表单时出现问题

sourceId=person_form:j_idt8[severity=(ERROR 2), summary=(Conversion Error setting value 'class com.aminpy.create.Phone' for 'null Converter'.), detail=(Conversion Error setting value 'class com.aminpy.create.Phone' for 'null Converter'.)]
4

1 回答 1

0

您需要创建一个可以在和Converter之间转换的自定义 JSF 。StringPhone

@FacesConverter(forClass=Phone.class)
public class PhoneConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext ctx, UIComponent component, java.lang.String value) {
        // Write code here which converts from String to Phone.
    }

    @Override
    public String getAsString(FacesContext ctx, UIComponent component, Object value) {
        // Write code here which converts from Phone to String.
    }

}

此外,您应该替换itemValueto be#{ph}而不是#{ph.id}.

也可以看看:

于 2012-08-15T11:09:56.817 回答