3 Replies Latest reply on Jul 21, 2006 9:16 AM by mark welch

    How do I change from session to request?

    mark welch Newbie

      Hello All,

      I am running the hello world faces portlet example using the MyFacesGenericPortlet class. I changed the scope for my bean to request and updated the concatenateNames() method to get the first and last name out of the context, but I am getting a class cast exception. This worked fine when it was in the session scope. I am using jboss-portal-2.2.1-SP2-bundled version of the portal. Please help.

      Thanks,
      Mark


      faces-config.xml setting:

      <managed-bean-scope>request</managed-bean-scope>
      


      jsp:
      <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
      <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
      
      <f:view>
       <div align="center">
       <h:form id="helloForm">
       <h:panelGrid columns="2">
       <h:outputText styleClass="portlet-font" value="First Name:"/>
       <h:inputText value="#{user.firstname}" align="center" required="true" id="firstName"/>
       <h:outputText styleClass="portlet-font" value="Last Name:"/>
       <h:inputText value="#{user.lastname}" align="center" required="true" id="lastName"/>
       </h:panelGrid>
       <br/>
       <h:commandButton action="#{user.concatenateNames}" value="Next"/>
       </h:form>
       </div>
      </f:view>
      


      ConcatenateNames method:
      public String concatenateNames() {
       FacesContext context = FacesContext.getCurrentInstance();
       HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
       String firstNameConcat = request.getParameter("helloForm:firstName");
       String lastNameConcat = request.getParameter("helloForm:lastName");
      
       setFullname(firstNameConcat + " " + lastNameConcat);
       return "done";
       }
      


      Exeption:
      
      avax.faces.FacesException: Error calling action method of component with id helloForm:jbp4641925_id3
       at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:74)
       at javax.faces.component.UICommand.broadcast(UICommand.java:106)
       at javax.faces.component.UIViewRoot._broadcastForPhase(UIViewRoot.java:90)
       at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:164)
       at org.apache.myfaces.lifecycle.LifecycleImpl.invokeApplication(LifecycleImpl.java:316)
       at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:86)
       at org.apache.myfaces.portlet.MyFacesGenericPortlet.processAction(MyFacesGenericPortlet.java:221)
       at org.jboss.portal.portlet.PortletContainer.invokeAction(PortletContainer.java:492)
       at org.jboss.portal.portlet.PortletContainer.dispatch(PortletContainer.java:428)
       at org.jboss.portal.server.app.ComponentInvocation.dispatch(ComponentInvocation.java:79)
       at org.jboss.portal.server.invocation.Invocation.invokeNext(Invocation.java:140)
       at org.jboss.portal.core.aspects.component.TransactionInterceptor.invoke(TransactionInterceptor.java:72)
       at org.jboss.portal.server.app.ComponentInterceptor.invoke(ComponentInterceptor.java:38)
       at org.jboss.portal.server.invocation.Invocation.invokeNext(Invocation.java:130)
       at org.jboss.portal.core.aspects.component.HeaderInterceptor.invoke(HeaderInterceptor.java:50)
       at org.jboss.portal.server.app.ComponentInterceptor.invoke(ComponentInterceptor.java:38)
       at org.jboss.portal.server.invocation.Invocation.invokeNext(Invocation.java:130)
       at org.jboss.portal.server.aspects.component.NavigationInterceptor.invoke(NavigationInterceptor.java:76)
       at org.jboss.portal.server.app.ComponentInterceptor.invoke(ComponentInterceptor.java:38)
       at org.jboss.portal.server.invocation.Invocation.invokeNext(Invocation.java:130)
       at org.jboss.portal.server.aspects.component.CacheInterceptor.invoke(CacheInterceptor.java:167)
       at org.jboss.portal.server.app.ComponentInterceptor.invoke(ComponentInterceptor.java:38)
       at org.jboss.portal.server.invocation.Invocation.invokeNext(Invocation.java:130)
       at org.jboss.portal.server.aspects.component.ModesInterceptor.invoke(ModesInterceptor.java:88)
       at org.jboss.portal.server.app.ComponentInterceptor.invoke(ComponentInterceptor.java:38)
       at org.jboss.portal.server.invocation.Invocation.invokeNext(Invocation.java:130)
       at org.jboss.portal.server.aspects.component.WindowStatesInterceptor.invoke(WindowStatesInterceptor.java:87)
       at org.jboss.portal.server.app.ComponentInterceptor.invoke(ComponentInterceptor.java:38)
       at org.jboss.portal.server.invocation.Invocation.invokeNext(Invocation.java:130)
       at org.jboss.portal.bridge.BridgeInterceptor.invoke(BridgeInterceptor.java:49)
       at org.jboss.portal.server.app.ComponentInterceptor.invoke(ComponentInterceptor.java:38)
       at org.jboss.portal.server.invocation.Invocation.invokeNext(Invocation.java:130)
       at org.jboss.portal.portlet.aspects.component.SessionPostDispatchInterceptor.invoke(SessionPostDispatchInterceptor.java:65)
       at org.jboss.portal.server.app.ComponentInterceptor.invoke(ComponentInterceptor.java:38)
       at org.jboss.portal.server.invocation.Invocation.invokeNext(Invocation.java:130)
       at org.jboss.portal.server.aspects.component.ContextDispatcherInterceptor$InvokeNextCommand.execute(ContextDispatcherInterceptor.java:129)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:585)
       at org.jboss.portal.server.servlet.CommandServlet.doGet(CommandServlet.java:101)
       at org.jboss.portal.server.servlet.CommandServlet.doPost(CommandServlet.java:149)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:672)
       at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:539)
       at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:499)
       at org.jboss.portal.server.app.impl.AbstractRequestContext.include(AbstractRequestContext.java:247)
       at org.jboss.portal.server.aspects.component.ContextDispatcherInterceptor$1.include(ContextDispatcherInterceptor.java:73)
       at org.jboss.portal.server.servlet.CommandServlet.include(CommandServlet.java:81)
       at org.jboss.portal.server.aspects.component.ContextDispatcherInterceptor.invoke(ContextDispatcherInterceptor.java:79)
       at org.jboss.portal.server.app.ComponentInterceptor.invoke(ComponentInterceptor.java:38)
       at org.jboss.portal.server.invocation.Invocation.invokeNext(Invocation.java:130)
       at org.jboss.portal.portlet.aspects.component.SessionPreDispatchInterceptor.invoke(SessionPreDispatchInterceptor.java:94)
       at org.jboss.portal.server.app.ComponentInterceptor.invoke(ComponentInterceptor.java:38)
       at org.jboss.portal.server.invocation.Invocation.invokeNext(Invocation.java:130)
       at org.jboss.portal.server.aspects.component.ContextTrackerInterceptor.invoke(ContextTrackerInterceptor.java:49)
       at org.jboss.portal.server.app.ComponentInterceptor.invoke(ComponentInterceptor.java:38)
       at org.jboss.portal.server.invocation.Invocation.invokeNext(Invocation.java:130)
       at org.jboss.portal.portlet.aspects.component.SecureTransportInterceptor.invoke(SecureTransportInterceptor.java:69)
       at org.jboss.portal.server.app.ComponentInterceptor.invoke(ComponentInterceptor.java:38)
       at org.jboss.portal.server.invocation.Invocation.invokeNext(Invocation.java:130)
       at org.jboss.portal.portlet.aspects.component.ValveInterceptor.invoke(ValveInterceptor.java:64)
       at org.jboss.portal.server.app.ComponentInterceptor.invoke(ComponentInterceptor.java:38)
       at org.jboss.portal.server.invocation.Invocation.invokeNext(Invocation.java:130)
       at org.jboss.portal.server.invocation.Invocation.invoke(Invocation.java:175)
       at org.jboss.portal.core.command.InvokeWindowActionCommand.execute(InvokeWindowActionCommand.java:134)
       at org.jboss.portal.core.command.ControllerCommand.dispatch(ControllerCommand.java:78)
       at org.jboss.portal.server.invocation.Invocation.invokeNext(Invocation.java:140)
       at org.jboss.portal.core.aspects.controller.EventBroadcasterInterceptor.invoke(EventBroadcasterInterceptor.java:85)
       at org.jboss.portal.core.command.CommandInterceptor.invoke(CommandInterceptor.java:38)
       at org.jboss.portal.server.invocation.Invocation.invokeNext(Invocation.java:130)
       at org.jboss.portal.core.aspects.controller.PolicyEnforcementInterceptor.invoke(PolicyEnforcementInterceptor.java:168)
       at org.jboss.portal.core.command.CommandInterceptor.invoke(CommandInterceptor.java:38)
       at org.jboss.portal.server.invocation.Invocation.invokeNext(Invocation.java:130)
       at org.jboss.portal.core.aspects.controller.PortalNodeInterceptor.invoke(PortalNodeInterceptor.java:61)
       at org.jboss.portal.core.command.CommandInterceptor.invoke(CommandInterceptor.java:38)
       at org.jboss.portal.server.invocation.Invocation.invokeNext(Invocation.java:130)
       at org.jboss.portal.core.aspects.controller.NavigationInterceptor.invoke(NavigationInterceptor.java:58)
       at org.jboss.portal.core.command.CommandInterceptor.invoke(CommandInterceptor.java:38)
       at org.jboss.portal.server.invocation.Invocation.invokeNext(Invocation.java:130)
       at org.jboss.portal.server.invocation.Invocation.invoke(Invocation.java:175)
       at org.jboss.portal.core.command.CommandContext.execute(CommandContext.java:107)
       at org.jboss.portal.core.command.ExecutionContext.execute(ExecutionContext.java:93)
       at org.jboss.portal.core.command.ExecutionContext.execute(ExecutionContext.java:77)
       at org.jboss.portal.core.CoreController.handle(CoreController.java:190)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:585)
       at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:155)
       at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
       at org.jboss.mx.interceptor.AbstractInterceptor.invoke(AbstractInterceptor.java:133)
       at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
       at org.jboss.mx.interceptor.ModelMBeanOperationInterceptor.invoke(ModelMBeanOperationInterceptor.java:142)
       at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
       at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:264)
       at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
       at org.jboss.mx.util.JMXInvocationHandler.invoke(JMXInvocationHandler.java:287)
       at $Proxy136.handle(Unknown Source)
       at org.jboss.portal.server.ServerInvocation.dispatch(ServerInvocation.java:76)
       at org.jboss.portal.server.invocation.Invocation.invokeNext(Invocation.java:140)
       at org.jboss.portal.core.aspects.server.SubjectAssociationInterceptor.invoke(SubjectAssociationInterceptor.java:47)
       at org.jboss.portal.server.ServerInterceptor.invoke(ServerInterceptor.java:38)
       at org.jboss.portal.server.invocation.Invocation.invokeNext(Invocation.java:130)
       at org.jboss.portal.core.aspects.server.PolicyAssociationInterceptor.invoke(PolicyAssociationInterceptor.java:52)
       at org.jboss.portal.server.ServerInterceptor.invoke(ServerInterceptor.java:38)
       at org.jboss.portal.server.invocation.Invocation.invokeNext(Invocation.java:130)
       at org.jboss.portal.server.aspects.server.ContentTypeInterceptor.invoke(ContentTypeInterceptor.java:68)
       at org.jboss.portal.server.ServerInterceptor.invoke(ServerInterceptor.java:38)
       at org.jboss.portal.server.invocation.Invocation.invokeNext(Invocation.java:130)
       at org.jboss.portal.core.aspects.server.LocaleInterceptor.invoke(LocaleInterceptor.java:69)
       at org.jboss.portal.server.ServerInterceptor.invoke(ServerInterceptor.java:38)
       at org.jboss.portal.server.invocation.Invocation.invokeNext(Invocation.java:130)
       at org.jboss.portal.core.aspects.server.UserInterceptor.invoke(UserInterceptor.java:184)
       at org.jboss.portal.server.ServerInterceptor.invoke(ServerInterceptor.java:38)
       at org.jboss.portal.server.invocation.Invocation.invokeNext(Invocation.java:130)
       at org.jboss.portal.server.aspects.server.SessionInvalidatorInterceptor.invoke(SessionInvalidatorInterceptor.java:87)
       at org.jboss.portal.server.ServerInterceptor.invoke(ServerInterceptor.java:38)
       at org.jboss.portal.server.invocation.Invocation.invokeNext(Invocation.java:130)
       at org.jboss.portal.core.aspects.server.TransactionInterceptor.invoke(TransactionInterceptor.java:75)
       at org.jboss.portal.server.ServerInterceptor.invoke(ServerInterceptor.java:38)
       at org.jboss.portal.server.invocation.Invocation.invokeNext(Invocation.java:130)
       at org.jboss.portal.server.invocation.Invocation.invoke(Invocation.java:175)
       at org.jboss.portal.server.servlet.PortalServlet.doGet(PortalServlet.java:221)
       at org.jboss.portal.server.servlet.PortalServlet.doPost(PortalServlet.java:149)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
       at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
       at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175)
       at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:432)
       at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)
       at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
       at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
       at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
       at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
       at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
       at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
       at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
       at java.lang.Thread.run(Thread.java:595)
      Caused by: javax.faces.el.EvaluationException: Exception while invoking expression #{user.concatenateNames}
       at org.apache.myfaces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:153)
       at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:63)
       ... 148 more
      Caused by: java.lang.ClassCastException: org.jboss.portlet.JBossActionRequest
       at com.nielsenmedia.foundations.faces.beans.User.concatenateNames(User.java:38)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:585)
       at org.apache.myfaces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:129)
       ... 149 more
      


        • 1. Re: How do I change from session to request?
          mark welch Newbie

          Nevermind I figured it out. FYI for any other newbies out there. In order to get a handle on the request within a portlet you have to cast to PortletRequest. I'm trying to do a proof of concept, and I've never worked with portlets before but have done lots of java server faces applications. It took me a while to find this.

          new java code:

          FacesContext context = FacesContext.getCurrentInstance();
           ExternalContext externalContext = context.getExternalContext();
           if (externalContext.getRequest() instanceof PortletRequest) {
           PortletRequest request = (PortletRequest)externalContext.getRequest();
          
           System.out.println(request.getParameter("helloForm:firstName"));
           System.out.println(request.getParameter("helloForm:lastName"));
           }else if(externalContext.getRequest() instanceof HttpServletRequest) {
           //do something outside of portal portlet api not in classpath
           }
          


          • 2. Re: How do I change from session to request?
            Thomas Heute Master

            Since you address this to newbie, i want to mention that you should try to avoid casting that at all costs while writing components, this is mostly a hack.

            We found enough components that do a cast from ExternalContext to HttpServletContext and then make the component unusable inside a portlet environment (for no technical reason). This is often not required, just a wish of the programmer to fall back to an API that he feels more comfortable with.


            In your example, you could simply use:

            FacesContext context = FacesContext.getCurrentInstance();
            ExternalContext externalContext = context.getExternalContext();
            System.out.println(externalContext.getRequestParameterMap().getAttribute("helloForm:firstName"));
            System.out.println(externalContext.getRequestParameterMap().getAttribute("helloForm:lastName"));
            


            And it will do the same thing but work in both environment.

            • 3. Re: How do I change from session to request?
              mark welch Newbie

              Very true and thanks for the tip. Inline would work better. You are definitely right; I was just testing things to make sure I could do all the same things in this environment that have helped me in previous environments. Your code example makes more sense.

              Thanks,
              Mark