在OneToOne关系中下拉菜单中的jsf

时间:2012-08-15 08:43:38

标签: drop-down-menu jsf-2 selectonemenu

我想在jsf中使用SIMPLE下拉菜单获取OneToOne / ManyToOne关系

Person.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'.)]

1 个答案:

答案 0 :(得分:0)

您需要创建一个自定义JSF Converter,它可以在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.
    }

}

此外,您应将itemValue替换为#{ph}而不是#{ph.id}

另见: