0

jsf で OneToOne/ManyToOne 関係に SIMPLE ドロップダウン メニューを使用したい

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

}

さらに、 を の代わりに に置き換える必要がitemValueあり#{ph}ます#{ph.id}

以下も参照してください。

于 2012-08-15T11:09:56.817 に答える