2013-07-02 73 views
0

我在嘗試轉換String to a Modelo entity using the JSF Converter`。@EJB裏面@FacesConverter爲空

我有這樣的看法:

<h:outputText value="Modelo*: "/> 
<p:selectOneMenu style="width: 300px" value="#{modeloBean.modelo.nome}" id="modelo" converter="modeloConverter"> 
    <f:selectItem itemLabel="Select One" itemValue="" /> 
    <f:selectItems value="#{modeloBean.listaModelo()}" var="modelo" itemLabel="#{modelo.nome}" itemValue="#{modelo}" />         
</p:selectOneMenu> 
<p:message for="modelo"/> 

Modelo實體:

@Entity 
@Table(name = "MODELO") 
@XmlRootElement 
@NamedQueries({ 
    @NamedQuery(name = "Modelo.findAll", query = "SELECT m FROM Modelo m"), 
    @NamedQuery(name = "Modelo.findById", query = "SELECT m FROM Modelo m WHERE m.id = :id"), 
    @NamedQuery(name = "Modelo.findByNome", query = "SELECT m FROM Modelo m WHERE m.nome = :nome")}) 
public class Modelo implements Serializable { 
    private static final long serialVersionUID = 1L; 
    @Id 
    @GeneratedValue(strategy= GenerationType.AUTO) 
    @Column(name = "ID") 
    private Integer id; 
    @Size(max = 255) 
    @Column(name = "NOME") 
    private String nome; 
    @OneToMany(mappedBy = "modeloId") 
    private List<Dipositivo> dipositivoList; 
    // with respectives getters and setters 

我試圖創建Converter如下:

@FacesConverter(value="modeloConverter", forClass=Modelo.class) 
@SessionScoped 
public class ModeloConverter implements Converter{ 

    @EJB 
    private ModeloFacade modeloFacade = new ModeloFacade(); 

    @Override 
    public Object getAsObject(FacesContext context, UIComponent component, String value) { 
     if(value == null || value.isEmpty()) { 
      return null; 
     } 
     try { 
      Object o = modeloFacade.consultarPorId(Long.valueOf(value)); 
      return o; 
     } catch (Exception e) { 
      throw new ConverterException(new FacesMessage(String.format("Cannot convert %s to Modelo", value)), e); 
     } 
    } 

    @Override 
    public String getAsString(FacesContext context, UIComponent component, Object value) { 
     if (!(value instanceof Modelo)) { 
      return null; 
     } 

     return String.valueOf(((Modelo) value).getId()); 
    } 
} 

但是下面一行拋出一個NullPointerException,表示modeloFacadenull

Object o = modeloFacade.consultarPorId(Long.valueOf(value)); 

這是怎麼造成的,我該如何解決呢?

其引發此:

SEVERE: java.lang.NullPointerException 
    at bean.AbstractFacade.consultarPorId(AbstractFacade.java:70) 
    at converter.ModeloConverter.getAsObject(ModeloConverter.java:35) 
    at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:171) 
    at com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectOneValue(MenuRenderer.java:202) 
    at com.sun.faces.renderkit.html_basic.MenuRenderer.getConvertedValue(MenuRenderer.java:319) 
    at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.getConvertedValue(SelectOneMenuRenderer.java:55) 
    at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1030) 
    at javax.faces.component.UIInput.validate(UIInput.java:960) 
    at javax.faces.component.UIInput.executeValidate(UIInput.java:1233) 
    at javax.faces.component.UIInput.processValidators(UIInput.java:698) 
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214) 
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214) 
    at org.primefaces.component.panel.Panel.processValidators(Panel.java:284) 
    at javax.faces.component.UIForm.processValidators(UIForm.java:253) 
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214) 
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214) 
    at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1172) 
    at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76) 
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) 
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) 
    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) 

SEVERE:  at bean.AbstractFacade.consultarPorId(AbstractFacade.java:70) 
SEVERE:  at converter.ModeloConverter.getAsObject(ModeloConverter.java:35) 
SEVERE:  at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:171) 
SEVERE:  at com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectOneValue(MenuRenderer.java:202) 
SEVERE:  at com.sun.faces.renderkit.html_basic.MenuRenderer.getConvertedValue(MenuRenderer.java:319) 
SEVERE:  at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.getConvertedValue(SelectOneMenuRenderer.java:55) 
SEVERE:  at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1030) 
SEVERE:  at javax.faces.component.UIInput.validate(UIInput.java:960) 
SEVERE:  at javax.faces.component.UIInput.executeValidate(UIInput.java:1233) 
SEVERE:  at javax.faces.component.UIInput.processValidators(UIInput.java:698) 
SEVERE:  at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214) 
SEVERE:  at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214) 
SEVERE:  at org.primefaces.component.panel.Panel.processValidators(Panel.java:284) 
SEVERE:  at javax.faces.component.UIForm.processValidators(UIForm.java:253) 
SEVERE:  at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214) 
SEVERE:  at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214) 
SEVERE:  at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1172) 
SEVERE:  at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76) 
SEVERE:  at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 
SEVERE:  at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) 
SEVERE:  at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) 
SEVERE:  at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550) 
SEVERE:  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281) 
SEVERE:  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) 
SEVERE:  at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655) 
SEVERE:  at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595) 
SEVERE:  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161) 
SEVERE:  at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331) 
SEVERE:  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231) 
SEVERE:  at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317) 
SEVERE:  at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195) 
SEVERE:  at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860) 
SEVERE:  at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757) 
SEVERE:  at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056) 
SEVERE:  at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229) 
SEVERE:  at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137) 
SEVERE:  at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104) 
SEVERE:  at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90) 
SEVERE:  at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79) 
SEVERE:  at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54) 
SEVERE:  at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59) 
SEVERE:  at com.sun.grizzly.ContextTask.run(ContextTask.java:71) 
SEVERE:  at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532) 
SEVERE:  at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513) 
SEVERE:  at java.lang.Thread.run(Thread.java:722) 
+0

它拋出什麼異常?請添加堆棧跟蹤,並指出引發異常的行。 –

+0

[JSF2,我可以使用@EJB向@FacesConverter注入服務嗎?](http://stackoverflow.com/questions/7665673/jsf2-can-i-use-ejb-to-inject-a -service-into-a-facesconverter) – BalusC

+0

Stephen C ...我添加了異常。 –

回答

0

黑傢伙,我解決我的問題。是cosultarPorId方法的問題。被用錯了實體經理。 這是工作。

public Modelo consultarPorId(Long id) { 
     CriteriaBuilder cb = getEntityManager().getCriteriaBuilder(); 
     CriteriaQuery<Modelo> c = cb.createQuery(Modelo.class);   
     Root<Modelo> modelo = c.from(Modelo.class);   
     c.where(cb.equal(modelo.get("id"), cb.parameter(String.class, "id"))); 
     TypedQuery q = getEntityManager().createQuery(c); 
     q.setParameter("id", id);  
     return (Modelo) q.getSingleResult(); 
    } 

現在的問題是不持久的數據庫=/