7 Replies Latest reply on Feb 27, 2008 12:13 PM by pmuir

    Call a method and redirect browser afterwards

    m23

      Dear all!


      I'm a seam-beginner and I'm depserately looking for a solution of the following problem:


      I want to provide a link on a webpage (e.g. test.jspx), the following steps have to be executed:
      1. First off i want to call a method on the server like #{someController.someMethod}
      2. After this method has completed, I want to redirect the browser to the following Java Servlet /servlets/someServlet


      I'm already familiar with commandLinks and commandButtons...
      But I didn't find a way to do both, calling a method and afterwards redirecting the to a Servlet (not a jsf-page)...


      I'd really appreciate your help!
      Michael

        • 1. Re: Call a method and redirect browser afterwards

          This works for me. I don't know if is the correct Seam way though...



          <h:commandButton action="#{someBean.dothis}"/>




          and


          @Name("someBean")
          public class SomeBean {
                      @In(value = "org.jboss.seam.faces.facesContext")
                      private FacesContext context;
                      
                      public void dothis() {
                          // do your stuff
                          ((HttpServletResponse) context.getExternalContext().getResponse()).sendRedirect("/some/Servlet");
                      }
          }



          • 2. Re: Call a method and redirect browser afterwards
            keithnaas

            There may be ways to do this using the basic Seam stuff, but the ExternalContext in JSF simply fronts the Servlet apis.


            In order to redirect, you should be able to call FacesContext.getCurrentInstance().getExternalContext().redirect(/somewhere/that/isnot/jsf)


            Within a Seam managed component, you could even inject the FacesContext and then cut out the call to getCurrentInstance().

            • 3. Re: Call a method and redirect browser afterwards

              That looks a bit nicer, no casting and stuff. :-)

              • 4. Re: Call a method and redirect browser afterwards
                m23

                Thanks a lot Keith and Daniel!


                Unfortunately I just ran into the next problem...
                The servlet to which the method redirects is configured in web.xml to maintain the seam contexts (Seam Servlet Filter).


                The whole thing works if i just provide an outputLink on the webpage. I'm able to access data from the conversation context within the Servlet:


                <h:outputLink value="/some/Servlet?conversationId=#{conversation.id}&amp;conversationIsLongRunning=true" />
                



                But if I do the following within my method, my conversation context seems to be empty:


                FacesContext.getCurrentInstance().getExternalContext().redirect("/some/Servlet?conversationId=#{conversation.id}&amp;conversationIsLongRunning=true")
                



                Am I wrong to assume that this should actually be working?
                Thank you in advance for your answers!


                Michael

                • 5. Re: Call a method and redirect browser afterwards

                  what about seams @Redirect annotation?

                  • 6. Re: Call a method and redirect browser afterwards
                    hubaghdadi

                    I never tried it but maybe you would like to  give org.jboss.seam.faces.redirect a shot.

                    • 7. Re: Call a method and redirect browser afterwards
                      pmuir

                      Try


                      FacesContext.getCurrentInstance().getExternalContext().redirect("/some/Servlet?conversationId=" + Conversation.instance().getId() + "&amp;conversationIsLongRunning=true");