0

I write my first JSF page with ajax event but not work; when submit form exception occur IllegalStateException The idea is to change the color of the inputText activating the writing when the checkbox is selected. See what follows.

    <!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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
    <title>Ins Student</title> 
</h:head>
    <h:form id="windowPrinc" prependId="false">
        <h:panelGrid id="griglia" columns="4">    
            <h:outputLabel value="Matricola....................:"></h:outputLabel>
                <h:inputText id="id_matricola" style="background-color:#{controllerInsertStudent.color_idStudente};border-style:solid; 
                        border-color:Brown;border-width:medium;"
                        readonly="#{!controllerInsertStudent.forzaMatricola}" title="matricola dello studente"
                        value="#{controllerInsertStudent.idStudente}">  
                </h:inputText>       

            <h:outputLabel value=" Forza Matricola...........:"/>  
            <h:selectBooleanCheckbox id="id_checkBox"  value="#{controllerInsertStudent.forzaMatricola}">
                <f:ajax>  execute="id_checkBox" listener="#{controllerInsertStudent.changeForzaMatricola}" render="id_matricola" </f:ajax>  
            </h:selectBooleanCheckbox>
        </h:panelGrid>  
   <h:outputLabel value="Nome:........................:" />
        <h:inputText id="id_nome" style="border-style:solid;border-color:Brown;border-width:medium;" value="#{controllerInsertStudent.nome}">       
        </h:inputText>
    <h:commandButton value="Inserisci" action="#{controllerInsertStudent.CreateStudente}">
    <f:ajax execute="@form"  />
   </h:commandButton>
    </h:form>
</html>

This managed bean

@ManagedBean
@ViewScope
public class ControllerInsertStudent {
            FacesContext ctx = FacesContext.getCurrentInstance();   
        private Long idStudente; 
            // etc.
        public Boolean getForzaMatricola()
        {
            return forzaMatricola;
        }


        public void setForzaMatricola(Boolean forzaMatricola)
        {
            this.forzaMatricola = forzaMatricola;

        }

        public void changeForzaMatricola(AjaxBehaviorEvent ev) throws AbortProcessingException
        {
            // dummy
        }
      public String CreateStudente()
  {

    String value = ctx.getExternalContext().getRequestParameterMap().get("id_matricola");
    if(!value.equals(""))
        this.idStudente = new Long(value);
            // etc.
    }

}

Stack Error (console Eclipse)

17:54:52,916 WARNING [javax.enterprise.resource.webcontainer.jsf.lifecycle] (http-  localhost-127.0.0.1-8080-1) #{controllerInsertStudent.CreateStudente}: java.lang.IllegalStateException: javax.faces.FacesException: #{controllerInsertStudent.CreateStudente}: java.lang.IllegalStateException
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118) [jsf-impl-2.1.7-jbossorg-2.jar:]
at javax.faces.component.UICommand.broadcast(UICommand.java:315) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) [jsf-impl-2.1.7-jbossorg-2.jar:]
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) [jsf-impl-2.1.7-jbossorg-2.jar:]
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) [jsf-impl-2.1.7-jbossorg-2.jar:]
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
at java.lang.Thread.run(Thread.java:722) [rt.jar:1.7.0_13]
 Caused by: javax.faces.el.EvaluationException: java.lang.IllegalStateException
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) [jsf-impl-2.1.7-jbossorg-2.jar:]
... 20 more
 Caused by: java.lang.IllegalStateException
at com.sun.faces.context.FacesContextImpl.assertNotReleased(FacesContextImpl.java:655) [jsf-impl-2.1.7-jbossorg-2.jar:]
at com.sun.faces.context.FacesContextImpl.getExternalContext(FacesContextImpl.java:140) [jsf-impl-2.1.7-jbossorg-2.jar:]
at org.controllers.beans.ControllerInsertStudent.CreateStudente(ControllerInsertStudent.java:237) [classes:]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_13]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_13]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_13]
at java.lang.reflect.Method.invoke(Method.java:601) [rt.jar:1.7.0_13]
at org.apache.el.parser.AstValue.invoke(AstValue.java:262) [jbossweb-7.0.13.Final.jar:]
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278) [jbossweb-7.0.13.Final.jar:]
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) [jsf-impl-2.1.7-jbossorg-2.jar:]
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
... 21 more
user2414681
  • 23
  • 1
  • 5
  • Why did you put the attributes outside the `` tag? Is this a careless typo during formulating the question, or is this really the real code? That would answer the question, but it would have been a too obvious mistake as you used the attributes rightly on all other tags in the page. Even the syntax highlighter shows a big hint. – BalusC May 24 '13 at 03:20
  • Real code; Error in the code because warning in eclipse code (sorry). But now I have another problem. When I select id_checkBox editing id_matricola but when submit the form, the value of idsudente in managed bean is null. I change the scope of the managed bean in viewscope, now have this error javax.faces.el.EvalutationException (java.lang.IllegalStateException: javax.faces.FacesException: #{controllerInsertStudent.CreateStudente}) CreateSudente is the method call on submit. Up the stack error. – user2414681 May 28 '13 at 16:10
  • possible duplicate of [java.lang.IllegalStateException at com.sun.faces.context.FacesContextImpl.assertNotReleased](http://stackoverflow.com/questions/4605118/java-lang-illegalstateexception-at-com-sun-faces-context-facescontextimpl-assert) – user207421 Jun 16 '15 at 01:49

1 Answers1

1

I'll ignore the red herring in the code of incorrectly having <f:ajax> tag attributes outside the tag itself which would have resulted in the listener method never being invoked. However, this is contradicted by the stack trace, which thus indicates that you have fixed the problem in the real code, but didn't bother to update the question accordingly and thus still keeps the red herring which makes the question potentially confusing and unanswerable.

Coming back to the concrete problem as visible in the stack trace:

Caused by: java.lang.IllegalStateException
    at com.sun.faces.context.FacesContextImpl.assertNotReleased(FacesContextImpl.java:655) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.sun.faces.context.FacesContextImpl.getExternalContext(FacesContextImpl.java:140) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at org.controllers.beans.ControllerInsertStudent.CreateStudente(ControllerInsertStudent.java:237) [classes:]

This suggests that you incorrectly got hold of the FacesContext of a previous request as an instance variable of a backing bean which is been placed in a broader scope than the request scope.

In other words, you've a

@ManagedBean
@ViewScoped // Or session or application scoped.
public class ControllerInsertStudent {

    private FacesContext context = FacesContext.getCurrentInstance();

    public void CreateStudente() { // Eeeek, method name starts with uppercase? Fix it!
        // ...

        ExternalContext externalContext = context.getExternalContext(); // Fail!

        // ...
    }

}

This is absolutely not correct. Such a bean is created in a different HTTP request than the HTTP request wherein its action is invoked. You should not be using the FacesContext instance of a different HTTP request. The FacesContext is internally stored as a ThreadLocal<FacesContext> in the thread serving the HTTP request which is released/trashed by end of request and thus not valid anymore in any other thread (read: any other HTTP request).

You should be obtaining it threadlocal (read: inside the method block) instead of assigning it as an instance variable of an object which lives longer than a single HTTP request, such as a view scoped managed bean.

@ManagedBean
@ViewScoped // Or session or application scoped.
public class ControllerInsertStudent {

    public void CreateStudente() { // Eeeek, method name starts with uppercase? Fix it!
        // ...

        FacesContext context = FacesContext.getCurrentInstance();
        ExternalContext externalContext = context.getExternalContext(); // OK!

        // ...
    }

}

I'm however not sure how this relates to the code posted so far which shows a request scoped bean. It look like that you was careless once again while preparing the question with the code. I.e. the code shown so far is not the real code at all or is completely irrelevant to the initial problem and thus you mixed up various problems.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I change the code with your suggestion and now all OK, thank you, BalusC. – user2414681 May 29 '13 at 19:30
  • You're welcome. In the future, please post a real SSCCE instead of cobbled together pieces of code. A real SSCCE is copy'n'paste'n'runnable without the need to make unobvious changes to the code. This way your question will less likely be ignored and be answered much sooner. See also http://stackoverflow.com/tags/jsf/info – BalusC May 29 '13 at 19:32