Il bean @SessionScoped perde lo scope e viene ricreato continuamente, i campi diventano nulli

Ho un problema molto strano con un bean SessionScoped in un progetto JSF 2.0. Utilizzo di Netbeans 6.9.1, server Glassfish 3 e PrimeFaces 3 come libreria dei componenti JSF.

Ecco un codice:

package com.hia.jsf; import com.hia.netlabel.jpa.Genre; import com.hia.netlabel.jpa.Label; import java.io.Serializable; import java.util.List; import javax.annotation.PostConstruct; import javax.enterprise.context.SessionScoped; import javax.faces.bean.ManagedBean; import javax.faces.bean.ManagedProperty; @ManagedBean @SessionScoped public class LabelDetailJSF implements Serializable{ @ManagedProperty("#{genreLabelListJSF}") private GenreLabelListJSF genreLabelListJSF; private List DetailLabelGenreList; private Label DetailLabel; /** Creates a new instance of LabelDetailJSF */ public LabelDetailJSF() { } @PostConstruct public void init(){ System.out.print("Running init LabelDetailJSF"); if(genreLabelListJSF.getSelectedLabel()!=null) { System.out.print("genreLabelListJSF was not null"); this.DetailLabelGenreList=genreLabelListJSF.getSelectedLabel().getGenreList(); this.DetailLabel= (genreLabelListJSF.getSelectedLabel()); } if(this.DetailLabelGenreList==null){ System.out.println("Bloody thing became null"); } } /** * @return the DetailLabel */ public Label getDetailLabel() { return DetailLabel; } /** * @param DetailLabel the DetailLabel to set */ public void setDetailLabel(Label DetailLabel) { System.out.println("Setting Detail Label For LabelDetailJSF to:"+DetailLabel.getLabelName()); this.DetailLabel = DetailLabel; } /** * @return the DetailLabelGenreList */ public List getDetailLabelGenreList() { System.out.println("There is:"+this.DetailLabelGenreList.size()+ " Genres available"); return DetailLabelGenreList; } /** * @param DetailLabelGenreList the DetailLabelGenreList to set */ public void setDetailLabelGenreList(List DetailLabelGenreList) { System.out.println("Setting the DetailLabelGenre List to have "+DetailLabelGenreList.size()); this.DetailLabelGenreList = DetailLabelGenreList; } /** * @return the genreLabelListJSF */ public GenreLabelListJSF getGenreLabelListJSF() { return genreLabelListJSF; } /** * @param genreLabelListJSF the genreLabelListJSF to set */ public void setGenreLabelListJSF(GenreLabelListJSF genreLabelListJSF) { this.genreLabelListJSF = genreLabelListJSF; } } 

Quindi, in pratica, ho iniettato un altro bean con sessioni di sessione chiamato GenreLabelListJSF in LabelDetailJSF. Voglio visualizzare DetailLabelGenreList all’interno di un dataTable. Ecco lo snippet XHTML utilizzato per visualizzare il datatable

 

#{genre.genreName}

Quando la pagina viene caricata inizialmente ottengo la tabella con alcune righe in. Quando clicco sui pulsanti di paging, è qui che inizia il divertimento.

Ho controllato l’output di tutte le istruzioni System.out e ho trovato quanto segue quando la pagina si carica inizialmente.

 INFO: Running init LabelDetailJSF INFO: genreLabelListJSF was not null INFO: There is:29 Genres available INFO: There is:29 Genres available INFO: There is:29 Genres available INFO: There is:29 Genres available INFO: There is:29 Genres available INFO: There is:29 Genres available 

Quando faccio clic sui pulsanti della pagina del datatable ottengo il seguente:

 INFO: Running init LabelDetailJSF INFO: Bloody thing became null SEVERE: javax.el.ELException: /LabelDetail.xhtml @62,165 value="# {labelDetailJSF.detailLabelGenreList}": java.lang.NullPointerException at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:107) at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:190) at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:178) at javax.faces.component.UIData.getValue(UIData.java:554) at org.primefaces.component.datatable.DataTable.getValue(DataTable.java:731) at javax.faces.component.UIData.getDataModel(UIData.java:1248) at javax.faces.component.UIData.setRowIndex(UIData.java:447) at javax.faces.component.UIData.visitTree(UIData.java:1184) at javax.faces.component.UIComponent.visitTree(UIComponent.java:1457) at javax.faces.component.UIComponent.visitTree(UIComponent.java:1457) at javax.faces.component.UIForm.visitTree(UIForm.java:333) at javax.faces.component.UIComponent.visitTree(UIComponent.java:1457) at javax.faces.component.UIComponent.visitTree(UIComponent.java:1457) at com.sun.faces.application.view.StateManagementStrategyImpl.restoreView(StateManagementStrategyImpl.java:223) at com.sun.faces.application.StateManagerImpl.restoreView(StateManagerImpl.java:177) at com.sun.faces.application.view.ViewHandlingStrategy.restoreView(ViewHandlingStrategy.java:131) at com.sun.faces.application.view.FaceletViewHandlingStrategy.restoreView(FaceletViewHandlingStrategy.java:430) at com.sun.faces.application.view.MultiViewHandler.restoreView(MultiViewHandler.java:143) at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:199) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:110) at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312) at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215) at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:277) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641) at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97) at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185) at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226) at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165) at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791) at com.sun.grizzly.comet.CometEngine.executeServlet(CometEngine.java:473) at com.sun.grizzly.comet.CometEngine.handle(CometEngine.java:341) at com.sun.grizzly.comet.CometAsyncFilter.doFilter(CometAsyncFilter.java:84) at com.sun.grizzly.arp.DefaultAsyncExecutor.invokeFilters(DefaultAsyncExecutor.java:161) at com.sun.grizzly.arp.DefaultAsyncExecutor.interrupt(DefaultAsyncExecutor.java:137) at com.sun.grizzly.arp.AsyncProcessorTask.doTask(AsyncProcessorTask.java:88) at com.sun.grizzly.http.TaskBase.run(TaskBase.java:189) at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330) at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309) at java.lang.Thread.run(Thread.java:662) Caused by: java.lang.NullPointerException at com.hia.jsf.LabelDetailJSF.getDetailLabelGenreList(LabelDetailJSF.java:72) at sun.reflect.GeneratedMethodAccessor951.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at javax.el.BeanELResolver.getValue(BeanELResolver.java:302) at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:175) at com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:72) at com.sun.el.parser.AstValue.getValue(AstValue.java:116) at com.sun.el.parser.AstValue.getValue(AstValue.java:163) at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:219) at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:102) ... 48 more 

Da quello che vedo, è quasi come se il fagiolo perdesse di vista e si ricreasse tutto il tempo. Inizialmente i valori sono lì quindi quando il paging inizia il bean perde valore. Ad esempio ho notato che anche il bean @ManagedProperty diventa nullo all’avvio del paging. Che è un po ‘folle in quanto è anche un bean con scope di sessione.

Ancora una nuova mano a JSF quindi probabilmente ho fatto qualche errore di noob. Devo dire che inizialmente l’ho creato come bean ViewScoped per sbaglio, e dopo aver cambiato il bean in SessionScoped ho annullato la distribuzione, l’applicazione ha riavviato Glassfish e lo ha creato e distribuito nuovamente.

Qualche idea?

Stai utilizzando l’annotazione @SessionScoped errata. Stai utilizzando quello di CDI, mentre hai registrato il bean con l’annotazione JSF @ManagedBean anziché l’annotazione CDI @Named .

Sostituire l’importazione CDI @SessionScoped

 import javax.enterprise.context.SessionScoped; 

dall’importazione di JSF @SessionScoped

 import javax.faces.bean.SessionScoped; 

Guarda anche:

  • Come scegliere l’ambito del bean giusto?
  • Backing beans (@ManagedBean) o CDI Beans (@Named)?