5 Replies Latest reply on Aug 21, 2006 7:16 AM by Raja

    Calling a method when a Session scope starts?

    None None Master

      Here's the situation: We have a site that needs to track affiliates. A customer clicks on a banner somewhere, and the banner link has a parameter like "affiliateId=4949". That ID number needs to be saved in the user's session, or even better in a cookie, so he can surf around the site, come back later, and sign up, and we know who the affiliate was.

      Back in the days of Servlets and Filters, I would have written a simple filter that would look for the affiliateId parameter, and if it's there, I would have set the value as a cookie, and that would be it.

      Is there a way to do that within Seam? I'm sure there, because when a user hits a Seam page, it immediately starts a session. There must be a few ways to do it. Any recommendations?

      Thanks

        • 1. Re: Calling a method when a Session scope starts?
          None None Master

          Actually...

          It looks like I could make up a Stateless bean, with scope=EVENT. That bean would be in scope for every single page request within the Seam application, right? So that bean would be present at every one.

          Then I could annotate a member of that bean with @RequestParameter.

          From there I'm not sure what to do. Could I use the @Observer annotation somehow so that a method of the bean would be called on every request?

          Alternatively, I could put @RequestParameter on a getter/setter pair, and the setter could do the logic needed. The advantage is that the method would only be called when the affiliateId parameter is actually set.

          I guess I'm trying to use a bean as a smart / light filter, which seems like it should be possible.

          • 2. Re: Calling a method when a Session scope starts?
            None None Master

            Ok, I tried that and it didn't work. The bean didn't get used.

            Next I'm going to try to force the bean to be used on the page by using a value from it somewhere.

            • 3. Re: Calling a method when a Session scope starts?
              Pete Muir Master

              How about using page actions?

              • 4. Re: Calling a method when a Session scope starts?
                None None Master

                That's what I ended up doing, and it works beautifully. It's a lot more elegant than writing a filter. No filter, and with about 5 lines of code, I can check a database and set a cookie. It does create a new object with every request, but it's a lightweight object so I don't think that matters.

                I haven't found any docs that give a good description of what you can do in the pages.xml file, but obviously this part of it is pretty cool.

                I assume that these components could do other stuff like do redirects. The ultimate in cool is if they could do effectively a JSP include, so I could use it for my SEO needs, and hide parameters within URLs (ie, map /blog-5885.seam to /blog-entry.jsp, and use the 5885 to set a parameter in it). I'm guess that's possible but I have no idea how, because as I said, there are no docs for what you can do in the pages.xml file.

                I still have no idea how Seam figures out whether it creates an instance of a component for a given page or not. It seems like it is buggy, because I can create components with scope of EVENT, and they are not always accessible within a page. I don't get it.

                • 5. Re: Calling a method when a Session scope starts?
                  Raja Master

                   

                  "SmokingAPipe" wrote:

                  I assume that these components could do other stuff like do redirects. The ultimate in cool is if they could do effectively a JSP include, so I could use it for my SEO needs, and hide parameters within URLs (ie, map /blog-5885.seam to /blog-entry.jsp, and use the 5885 to set a parameter in it). I'm guess that's possible but I have no idea how, because as I said, there are no docs for what you can do in the pages.xml file.


                  This is possible and IIRC is also documented. You can have a page action that responds to blog-* and have that action return a totally different view id to be processed. For that view, you could use 5885 or whatever as a parameter.