4 Replies Latest reply on Jun 6, 2008 10:16 AM by hasan_muhstaq

    RequestParameter always null

      I'm trying to get request parameters to work...


      I access the unsubscribe.xhtml page below with the url:


      localhost:8080/EmailGather2/unsubscribe.seam?pid=abcde
      



      I click the unsubscribe button, which goes into my stateful session bean's unsubscribe method.


      From my reading of the @RequestParameter documentation, the request parameters should be saved into the pid string inside the bean, but it is always null.


      Anybody know what I'm doing wrong?







      <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
                            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <ui:composition xmlns="http://www.w3.org/1999/xhtml"
           xmlns:s="http://jboss.com/products/seam/taglib"
           xmlns:ui="http://java.sun.com/jsf/facelets"
           xmlns:f="http://java.sun.com/jsf/core"
           xmlns:h="http://java.sun.com/jsf/html"
           xmlns:rich="http://richfaces.org/rich" template="layout/template.xhtml">
      
           <ui:define name="body">
      
                <h:graphicImage value="img/logo2.gif" />
      
                <table style="text-align: center" align="center">
                     
                     <tr>
                          <td style="text-align: left"><br />
                          <h:form id="emailgather" styleClass="edit" style="text-align:left">
                               <div class="actionButtons"><h:commandButton type="submit"
                                    value="unsubscribe" action="#{unsubscribeAction.unsubscribe}" />
                               </div>
                          </h:form> <h:messages globalOnly="true" styleClass="message" /></td>
                     </tr>
                </table>
           </ui:define>
      </ui:composition>
      
      




      import javax.ejb.Remove;
      import javax.ejb.Stateful;
      import javax.persistence.EntityManager;
      import javax.persistence.PersistenceContext;
      
      import org.jboss.seam.annotations.Begin;
      import org.jboss.seam.annotations.Destroy;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Scope;
      import org.jboss.seam.annotations.web.RequestParameter;
      import static org.jboss.seam.ScopeType.EVENT;
      
      @Stateful
      @Name("unsubscribeAction")
      @Scope(EVENT)
      public class UnsubscribeActionBean implements UnsubscribeAction {
          @PersistenceContext
          private EntityManager em;
          
          @RequestParameter(value="pid")
          String pid;
      
          public void unsubscribe(){
                System.out.println(pid); // prints null
           }
           
           @Destroy
           @Remove
           public void destroy(){
                
           }
      

        • 1. Re: RequestParameter always null
          hasan_muhstaq

          Your seam Component is in scope Event. According to the documentation



          Components associated with the event context are destroyed at the end of the request, but their state is available and well-defined for at least the lifecycle of the request

          So the first time your component is created (that is when u access ur url) the value of pid is set to abcde. But when u acces it again  via ur form its value is reintialized to NULL.


          I suggest u directly call ur method unsubscribe when u acess the URL. you can do this by adding this to ur pages.xml



          <page view-id="/EmailGather2/unsubscribe.xhtml" action="#{unsubscribeAction.unsubscribe}"/>



          Your pid value should be the right one when u access ur method like this.


          By the way if you want to pass request Parameters on button click u need to do something like this


          <h:commandButton type="submit" value ="unsubscribe"
          action="#{unsubscribeAction.unsubscribe}"> 
          <f:param name="pid" value="value goes here"/>
          <h:commandButton/>



          Hope this helps

          • 2. Re: RequestParameter always null

            Thanks for the reply Hasan,


            I started to understand about the request scope while I was working on this last night,  and was going to try to force it to work by using a factory method, and then force it to get called by using the bean in the jsf page.


            That wasn't really what I wanted though...  your tips are going to be a big help. I didn't know you could define an action to happen on page load with pages.xml like that.


            Thanks again!

            • 3. Re: RequestParameter always null

              One more question... how could I use that param tag on the command button to propagate the request variable to the form? 


              If the tag is:


              <f:param name="pid" value="value goes here"/>
              



              What would I put into value to have it be the actual value of the pid? If this were Coldfusion, it'd be in a variable called URL.pid, but that doesn't help me much. Do I have to dig it out of the request object like I recall doing long ago with non-seam JSPs and Servlets?

              • 4. Re: RequestParameter always null
                hasan_muhstaq

                Once your pid value is set in the backing bean unsubscribeAction all your seam components should be able to use it. So i don't see y u need to pass the value again to ur components. All u have to do is not use the Event context so that the value is not erased.


                If you still want to propogate. try oujecting it and then access your oujected variable in the view.


                 
                  @Out(required = false) 
                  // Not sure if its a good idea to outject a requestparmeter
                  @RequestParameter
                  private String pid;
                
                 


                in your view


                <f:param name="pid" value="#{pid}"/>