6 Replies Latest reply on Jun 26, 2008 10:04 PM by dhinojosa

    Page redirects from session beans

    greatguns

      Hi,


      is there anyway to redirect a user to another page from within a session bean method?


      the method definition is void save(){}
      i want the code within save to be invoked only if the injected entity is not null.


      if the injected entity is null then the user should be redirected to an error page.


      can anyone please point me in the right directions?


      thanks

        • 1. Re: Page redirects from session beans
          lich

          FacesManager.instance().redirect(...),i am no sure. Usually you should return a String and use page navigation to redirect.

          • 2. Re: Page redirects from session beans
            mail.micke

            You can use something like this



                public static void redirectTo(final String aRedirectUrl){
                    try {
                        FacesContext.getCurrentInstance().
                                     getExternalContext().
                                     getResponse().
                                     sendRedirect( aRedirectUrl);
                    }
                    catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            



            • 3. Re: Page redirects from session beans
              greatguns

              Thank you both for the help.


              I will try it out today :).

              • 4. Re: Page redirects from session beans
                greatguns

                That worked Great!!!


                Thank you for your help :D

                • 5. Re: Page redirects from session beans
                  graben

                  Another possible solution is the seam built in redirection component. I think this should work better than the above one since it won't start the redirect immediately but after successfully ending of the methods. It's the seam standard way!


                  Example:


                  Redirect redirect = Redirect.getInstance();
                  redirect.setViewId(/xxx.xhtml)
                  ...


                  • 6. Re: Page redirects from session beans
                    dhinojosa

                    Everyone here is scaring me, and this sounds like a bad idea.


                    Can't you throw an exception from your code and then have pages.xml do the redirecting for you?


                     <exception class="com.mycorp.NoEntityInjectedStateException">
                            <redirect view-id="/login.xhtml">
                                <message>Please log in first</message>
                            </redirect>
                        </exception>
                        
                    




                    This would keep your code (excluding annotations) decoupled from the framework and thus make it reusable.