0

I'm trying to load the selectOneMenu and gives the following exception: java.lang.IllegalStateException: Cannot create a session after the response has been committed. I have the xhtml:

<h:outputText value="Nome:" />
 <p:selectOneMenu value="" id="nome"  >
   <f:selectItem itemValue="#{solicitarPortabilidadeBean.listaDoadora}" var="doadora" itemLabel="#{doadora.nomeOperadora}"/>                
 </p:selectOneMenu>

the bean:

@ViewScoped
@ManagedBean
public class SolicitarPortabilidadeBean implements Serializable {

  private static final long serialVersionUID = 1L;  
  private List<Operadora> listaDoadora = null;
  private Operadora operadora;
//getters and setters

  @EJB
  private ConsultasSpnService consultaOp;   

  public List<Operadora> getListaDoadora() {

if (listaDoadora == null) {

 listaDoadora = new ArrayList<Operadora>();
 listaDoadora = consultaOp.listarOp();          

}   

return listaDoadora;
    }

public void setListaDoadora(List<Operadora> listaDoadora) {
    this.listaDoadora = listaDoadora;
}

public Operadora getOperadora() {
    return operadora;
}

public void setOperadora(Operadora operadora) {
    this.operadora = operadora;
}

public List<Eot> getListaEot() {
    return listaEot;
}

public void setListaEot(List<Eot> listaEot) {
    this.listaEot = listaEot;
}

public Eot getEot() {
    return eot;
}

public void setEot(Eot eot) {
    this.eot = eot;
}

public ConsultasSpnService getConsultaOp() {
    return consultaOp;
}

public void setConsultaOp(ConsultasSpnService consultaOp) {
    this.consultaOp = consultaOp;
}   

}

the Operadora class:

public class Operadora {
 private String srvprovid = null;   
 private String nomeOperadora = null;   
 private String indicadorFuncoes = null;
 private String funcaoSuporte = null;
 private String tipoTempReceptora = null;
 private String tipoTempDoadora = null;
 private String horaTrabSuportado = null;
 private Long qtdMaximaTn = null;
 private String tipoServico = null;
 //getters and setters
public String getSrvprovid() {
        return srvprovid;
    }
    public void setSrvprovid(String srvprovid) {
        this.srvprovid = srvprovid;
    }
    public String getNomeOperadora() {
        return nomeOperadora;
    }
    public void setNomeOperadora(String nomeOperadora) {
        this.nomeOperadora = nomeOperadora;
    }
    public String getIndicadorFuncoes() {
        return indicadorFuncoes;
    }
    public void setIndicadorFuncoes(String indicadorFuncoes) {
        this.indicadorFuncoes = indicadorFuncoes;
    }
    public String getFuncaoSuporte() {
        return funcaoSuporte;
    }
    public void setFuncaoSuporte(String funcaoSuporte) {
        this.funcaoSuporte = funcaoSuporte;
    }
    public String getTipoTempReceptora() {
        return tipoTempReceptora;
    }
    public void setTipoTempReceptora(String tipoTempReceptora) {
        this.tipoTempReceptora = tipoTempReceptora;
    }
    public String getTipoTempDoadora() {
        return tipoTempDoadora;
    }
    public void setTipoTempDoadora(String tipoTempDoadora) {
        this.tipoTempDoadora = tipoTempDoadora;
    }
    public String getHoraTrabSuportado() {
        return horaTrabSuportado;
    }
    public void setHoraTrabSuportado(String horaTrabSuportado) {
        this.horaTrabSuportado = horaTrabSuportado;
    }
    public Long getQtdMaximaTn() {
        return qtdMaximaTn;
    }
    public void setQtdMaximaTn(Long qtdMaximaTn) {
        this.qtdMaximaTn = qtdMaximaTn;
    }
    public String getTipoServico() {
        return tipoServico;
    }
    public void setTipoServico(String tipoServico) {
        this.tipoServico = tipoServico;
    }

I know the error is in calling the bean inside the xhtml, but I tried everything I knew. Can anyone help me?

Thank you!!!

Deb
  • 431
  • 4
  • 12
  • 26

2 Answers2

2

Apart from severe logic errors in the code, which should in turn however not have thrown any exception during rendering at all, for sure not particularly the mentioned exception,

IllegalStateException: Cannot create a session after the response has been committed

your concrete problem is most likely caused by a bug in Mojarra which is fixed in Mojarra 2.1.8. This bug can manifest when a view scoped or session scoped bean is referenced for the first time "late" in a relatively large JSF page, long after the response has been committed.

View and session scoped beans needs to be stored in the HTTP session. For that, the HTTP session needs to be created first, if not done yet (e.g. first-time request). For that, a cookie needs to be put on the HTTP response headers first. For that, the response should not have been sent to the client at all. The default response buffer size is usually 2KB, so if a page is larger than 2KB and the view or session scoped bean is referenced for the first time after the first piece of 2KB, then you will get exactly this exception.

Upgrade to at least Mojarra 2.1.8 (it's currently already at 2.1.13) and this problem should disappear.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I made this change "Add a context parameter with name of javax.faces.STATE_SAVING_METHOD and value of client to web.xml. Advantage: session will not be created at all unless you have session scoped beans. It also immediately solves potential ViewExpiredException cases. Disadvantage: increased network bandwidth usage. If you're using partial state saving, then the impact should however be minimal." and he gave another exception: "java.io.NotSerializableException: br.com.cpmbraxis.ebt.model.gateway.Operadora" – Deb Sep 12 '12 at 19:02
  • That's a different problem. Just let it implement the `Serializable` interface, exactly as the exception message is trying to hint you. – BalusC Sep 12 '12 at 19:06
  • I added "public class Operadora implements Serializable { private static final long serialVersionUID = 1L;" It stopped the exception but continues without loading the make up, what can be? – Deb Sep 12 '12 at 19:09
  • That's a different problem. Just press `Ask Question` button on right top. Your current question has been answered. – BalusC Sep 12 '12 at 19:12
  • BarlusC I edit this question or post a new one? Which is better? Thank you for your help! – Deb Sep 12 '12 at 19:18
  • Oops! I understand, forgive me I do not know English very well. Thank you! – Deb Sep 12 '12 at 19:21
  • BlausC, this was a fantastic information provided by you. Even i wasn't aware of such kind of situation. I will bookmark this question. Thanks for sharing your knowledge. – benz Sep 12 '12 at 19:22
0

Firs of all, here is one syntax problem with your code

<h:outputText value="Nome:" />
 <p:selectOneMenu value="" id="nome"  >
   <f:selectItem itemValue="#{solicitarPortabilidadeBean.listaDoadora}" var="doadora" itemLabel="#{doadora.nomeOperadora}"/>                
 </p:selectOneMenu>

You are not binding the value attribute to the backing bean. Additionally IllegalStateException comes when you have already committed the response, means written something, committed it and then passed the control to another servlet or something. Before seeing at the proper ManagedBean code, it is hard to tell where you are mistaking. How is your list getting populated. Please post the ManagedBean code and try to map the value attribute to some ManagedBean property. HTH, Ben

benz
  • 4,561
  • 7
  • 37
  • 68