0

p:selectonemenuに問題があります。サーバーログと私のコードは以下のとおりです。問題:CRUD機能を備えた単純なアプリケーションをコーディングしようとしています。h:selectonemenuは期待どおりに機能します。しかし、p:selectonemenuに切り替えると、コンバーターの例外が発生します。通常、私のオプションデータにはデータベースの3行が含まれています。デバッグの目的で初期化後にオプションを印刷しました。リストは通常​​どおり要素を使用して初期化されます。

ただし、p:selectmenuを使用すると、リストは再びそこの要素で初期化されます(init関数のsysoによって証明されます)が、追加のselectonemenuオプション、nullはコンバーターに到達します。そして、それは例外を発生させます。

私はアプリケーションをデバッグし、そこにも余分なnullオブジェクトを見ました。私のコンバーターは、h:selectonemenuで3回、p:selectmenuで少なくとも4回呼び出されます。

私は間違いを犯した可能性があります。しかし、これは私の間違いですか、それとも他に何がありますか?

Glassfish 3.1.2、PM 3.3.1

サーバーログ(h:selectmenu):

INFO: Hibernate: 
    select
        grup0_.grup_id as grup1_8_,
        grup0_.grup_adi as grup2_8_ 
    from
        Grup grup0_
INFO: Selectonemenu Option@Init :Test Value 1
INFO: Selectonemenu Option@Init :Test Value 2
INFO: Selectonemenu Option@Init :Test Value 3
INFO: Selectonemenu Option@Converter :Test Value 1
INFO: Selectonemenu Option@Converter :Test Value 2
INFO: Selectonemenu Option@Converter :Test Value 3

サーバーログ(p:selectmenu):

INFO: Hibernate: 
    select
        grup0_.grup_id as grup1_8_,
        grup0_.grup_adi as grup2_8_ 
    from
        Grup grup0_
INFO: Selectonemenu Option@Init :Test Value 1
INFO: Selectonemenu Option@Init :Test Value 2
INFO: Selectonemenu Option@Init :Test Value 3
INFO: Selectonemenu Option@Converter :Test Value 1
INFO: Selectonemenu Option@Converter :Test Value 2
INFO: Selectonemenu Option@Converter :Test Value 3

INFO: Hibernate: 
    select
        grup0_.grup_id as grup1_8_,
        grup0_.grup_adi as grup2_8_ 
    from
        Grup grup0_
INFO: Selectonemenu Option@Init :Test Value 1
INFO: Selectonemenu Option@Init :Test Value 2
INFO: Selectonemenu Option@Init :Test Value 3
INFO: Selectonemenu Option@Converter :Test Value 1
INFO: Selectonemenu Option@Converter :Test Value 2
INFO: Selectonemenu Option@Converter :Test Value 3
**INFO: Selectonemenu Option@Converter :null**
SEVERE: Error Rendering View[/NewFile.xhtml]
javax.faces.convert.ConverterException: getAsString Error
    at converters.GrupConverter.getAsString(GrupConverter.java:60)
    at org.primefaces.util.ComponentUtils.getValueToRender(ComponentUtils.java:76)
    at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.encodeLabel(SelectOneMenuRenderer.java:120)
    at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.encodeMarkup(SelectOneMenuRenderer.java:89)
    at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.encodeEnd(SelectOneMenuRenderer.java:62)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1764)
    at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1757)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:722)

WARNING: StandardWrapperValve[Faces Servlet]: PWC1406: Servlet.service() for servlet Faces Servlet threw exception
javax.faces.convert.ConverterException: getAsString Error
    at converters.GrupConverter.getAsString(GrupConverter.java:60)
    at org.primefaces.util.ComponentUtils.getValueToRender(ComponentUtils.java:76)
    at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.encodeLabel(SelectOneMenuRenderer.java:120)
    at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.encodeMarkup(SelectOneMenuRenderer.java:89)
    at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.encodeEnd(SelectOneMenuRenderer.java:62)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1764)
    at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1757)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:722)

test.xhtml

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
            xmlns:p="http://primefaces.org/ui"

      xmlns:ui="http://java.sun.com/jsf/facelets"> 

<h:head></h:head> 
<body> 
<h:form>
<p:selectOneMenu id="grup" value="#{grupBean.grup}" converter="grupConverter">
                            <f:selectItems value="#{grupBean.grupList}" var="grp" itemLabel="#{grp.grup_adi}" itemValue="#{grp}" />
                        </p:selectOneMenu>
</h:form>

</body> 
</html>

Grup.java

package model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;

@Entity
public class Grup implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 8730335332086520080L;
    @Id
    @GeneratedValue
    private Integer grup_id;
    private String grup_adi;

    @ManyToMany(mappedBy="gruplar")
    private List<Kullanici> kullanicilar = new ArrayList<Kullanici>();

    public Integer getGrup_id() {
        return grup_id;
    }

    public void setGrup_id(Integer grup_id) {
        this.grup_id = grup_id;
    }

    public String getGrup_adi() {
        return grup_adi;
    }

    public void setGrup_adi(String grup_adi) {
        this.grup_adi = grup_adi;
    }

    public List<Kullanici> getKullanicilar() {
        return kullanicilar;
    }

    public void setKullanicilar(List<Kullanici> kullanicilar) {
        this.kullanicilar = kullanicilar;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((grup_id == null) ? 0 : grup_id.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (!(obj instanceof Grup))
            return false;
        Grup other = (Grup) obj;
        if (grup_id == null) {
            if (other.grup_id != null)
                return false;
        } else if (!grup_id.equals(other.grup_id))
            return false;
        return true;
    }

}

GrupConverter.java

package converters;

import model.Grup;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;
import javax.persistence.EntityManager;

import util.EntityUtil;

@FacesConverter(value = "grupConverter")
public class GrupConverter implements Converter {

    EntityManager em = EntityUtil.getEntityManager();

    @Override
    public Object getAsObject(FacesContext context, UIComponent component,
            String deger) {

        Grup findGrup = null;
        if (deger == null || deger.isEmpty()) {
            return null;
        }

        try {
            findGrup = em.find(Grup.class, Integer.parseInt(deger));
            // System.out.println(findGrup.);
        } catch (Exception e) {
            throw new ConverterException(new FacesMessage(String.format(
                    "Cannot convert %s to Grup", deger)), e);
        }
        return findGrup;

    }

    @Override
    public String getAsString(FacesContext context, UIComponent component,
            Object deger) {
        String val = null;

        // ....................

        Grup d = (Grup) deger;
        System.out
                .println("Selectonemenu Option@Converter :" + d.getGrup_adi());

        // .....................

        try {
            Grup b = (Grup) deger;
            val = Integer.toString(b.getGrup_id());

        } catch (Exception e) {
            throw new ConverterException(new FacesMessage("getAsString Error"));

        }

        return val;

    }

}

GrupBean.java

package beans;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
import javax.persistence.EntityManager;

import model.Birim;
import model.Grup;

import util.EntityUtil;



@ManagedBean
@ViewScoped
public class GrupBean implements Serializable{

private static final long serialVersionUID = 2166603645975073743L;
List<Grup> grupList=null;
List<Birim> birimList;

EntityManager em = EntityUtil.getEntityManager();


private boolean edit;

Grup grup=new Grup();



@PostConstruct 
public void init()
    {
    grupList = em.createQuery("from Grup").getResultList();
    //birimList=em.createQuery("from Birim").getResultList();

     for (Grup grup : grupList) {
     System.out.println("Selectonemenu Option@Init :"+grup.getGrup_adi());

         }

    }



public void add()
    {
    System.out.println("Add");

    em.getTransaction().begin();
    em.persist(grup);
    em.getTransaction().commit();

    System.out.println("End of Add");
    grupList = em.createQuery("from Grup").getResultList();
    edit = false;
    grup = new Grup(); // Reset placeholder.    

    }



public void edit(Grup grup) {
    this.grup = grup;
    //System.out.println("Grup Edit"+grup.anAdi());
    edit = true;
   }

public void save() {
    // dao.update(item);

    System.out.println("save");
    em.getTransaction().begin();
    em.merge(grup);
    em.getTransaction().commit();
    System.out.println("End of save");
    edit = false;
    grupList = em.createQuery("from Grup").getResultList();
    grup = new Grup(); // Reset placeholder.    
}

public void delete(Grup grup) {

    System.out.println("delete");

    em.getTransaction().begin();
    em.remove(grup);
    em.getTransaction().commit();

    System.out.println("End of delete");
    grup = new Grup(); // Reset placeholder.
    edit = false;
    grupList = em.createQuery("from Grup").getResultList();

}



public List<Grup> getGrupList() {
    return grupList;
}



public void setGrupList(List<Grup> grupList) {
    this.grupList = grupList;
}



public Grup getGrup() {
    return grup;
}



public void setGrup(Grup grup) {
    this.grup = grup;
}

public boolean isEdit() {
    return edit;
}



public List<Birim> getBirimList() {
    return birimList;
}



public void setBirimList(List<Birim> birimList) {
    this.birimList = birimList;
}



}
4

0 に答える 0