7 Replies Latest reply on Nov 23, 2009 9:56 PM by itays100

    Navigate from external link to seam

    itays100 Newbie

      Hi all,


      I'm facing a problem and looking for the best practice:
      Once you click on the link from email:


      
      http://localhost:8080/jboss-seam-jpa/register.seam?code=c13976c6ed1fd
      
      



      You will be forward to seam action by:


      
      <page view-id="/register.xhtml">
                         
              <param name="activationKey" value="#{request.getParameter('code')}"/>
              <action execute="#{guest.doActivate(activationKey)}" if="#{activationKey != null}"/>
                                   
      <navigation from-action="#{guest.doActivate(activationKey)}">           
                      <redirect view-id="/login.xhtml"/>           
              </navigation>        
      
          </page>
      
      



      I would like to forward the user to login.xhtml with no luck so far. I tried also using the url attribute. What is the best way for this ?


      Thanks!


        • 1. Re: Navigate from external link to seam
          Marcio Dantas Newbie

          Hi there,


          assuming that guest is a backing bean, maybe you could do something like below.


          <page view-id="/register.xhtml">
            <action execute="#{guest.doActivate}"/>
          </page>



          And in the guest backing bean:




          @RequestParameter
          private String code;
          
          ...
          
          public String doActivate() {
             ...
             something using code
             ...
             return "/login.xhtml";
          }
          



          good luck

          • 2. Re: Navigate from external link to seam
            itays100 Newbie

            Hi, I don't have problem sending the request parameter.
            The problem is only navigate after this method invokation.
            I just not use to pages.xml of seam and it seems to me that the document has a lack on inforamtion on this area.


            The main problem as i see it is that you click on a link from an email message and not from the application itself!

            • 3. Re: Navigate from external link to seam
              itays100 Newbie

              I succeeded forward to login.xhtml but i realy think there is a better way to do this in seam!


              
              ExternalContext extCtxt = FacesContext.getCurrentInstance().getExternalContext();
                      try {
                             extCtxt.redirect("http://localhost:8080/jboss-seam-jpa/login.seam");
                        } catch (IOException e) {
                             // TODO Auto-generated catch block
                             e.printStackTrace();
                        }
              
              

              • 4. Re: Navigate from external link to seam
                Jonathan Tougas Newbie

                Hi Itay, I tried this out of curiousity since I was sure this worked, and indeed it does work... I created a fresh seam project (Seam 2.2.0.GA + JBoss 4.2.3.GA), and added the following (essentially the same as in your first post).


                <page view-id="/test.xhtml">     
                     <param name="myParam" value="#{request.getParameter('myParam')}"/>          
                           <action execute="#{testComponent.doAction(myParam)}" if="#{myParam != null}"/>
                
                           <navigation from-action="#{testComponent.doAction(myParam)}">
                                <redirect view-id="/login.xhtml" />
                           </navigation>
                </page>





                @Name( "testComponent" )
                public class TestComponent {
                
                     @Logger
                     private Log log;
                     
                     public void doAction( String param ) {
                          log.info( "doAction(\"" + param + "\")" );
                     }
                }
                



                Behavior is as expected: I point my browser to:


                http://localhost:8080/mywar/test.seam?myParam=OMG



                I get :


                10:01:26,700 INFO  [TestComponent] doAction("OMG")


                in the log, and redirected to login.seam


                Take a close look at your project, something is surely different.


                • 5. Re: Navigate from external link to seam
                  itays100 Newbie

                  Hi Jonathan,


                  The only difference between your code and mine is that you use the same name for request parameter and the param name.
                  And once i change it on my project it realy works!


                  
                  <param name="myParam" value="#{request.getParameter('myParam')}"/>
                  
                  



                  Take a look at my first post. BTW i used seam 2.1.2.
                  Please try differet name on 2.2.
                  I think it's a bug. should add to jira right ?

                  • 6. Re: Navigate from external link to seam
                    Jonathan Tougas Newbie

                    You're right, but not about this being a bug. The value="#{request.getParameter('myParam')}" is the binding expression, and in this case since you don't bind the value to anything this is sufficient:


                    <page view-id="/test.xhtml">     
                         <param name="myParam" />          
                               <action execute="#{testComponent.doAction(myParam)}" if="#{myParam != null}"/>
                    
                               <navigation from-action="#{testComponent.doAction(myParam)}">
                                    <redirect view-id="/login.xhtml" />
                               </navigation>
                    </page>



                    I'm surprise that the parameter makes it to the action method though. Ideally you should make a seam component for the page's parameters and inject this into the action instead of passing around parameters in the navigation rules like this.


                    Here's the same example with the seam component for the parameters:




                    @Name( "testParams")
                    public class TestParams {
                         
                         private String myParam;
                    
                         public void setMyParam(String myParam) {
                              this.myParam = myParam;
                         }
                    
                         public String getMyParam() {
                              return myParam;
                         }
                    }





                    @Name( "testComponent" )
                    public class TestComponent {
                    
                         @Logger
                         private Log log;
                         
                         @In
                         private TestParams testParams;
                         
                         public void doAction() {
                              log.info( "doAction( \"" + testParams.getMyParam() + " \")" );
                         }
                    }





                    <page view-id="/test.xhtml">     
                         <param name="myParam" value="#{testParams.myParam}" />          
                               <action execute="#{testComponent.doAction}"/>
                               <navigation from-action="#{testComponent.doAction}">
                                    <redirect view-id="/login.xhtml" />
                               </navigation>
                    </page>






                    Good luck!


                    • 7. Re: Navigate from external link to seam
                      itays100 Newbie

                      hi Jonathan,


                      I think the following piece of code


                      
                      <param name="myParam" value="#{request.getParameter('param')}"/>
                      
                      



                      Should be equivalent to the follwing code:


                      
                      String myParam = (String)request.getParameter('param');
                      
                      



                      BTW Your example works on general parameter and not on a request parameter, you should define @RequestParameter and there you're set.