10 Replies Latest reply on May 2, 2007 4:02 AM by christian.bauer

    @RequestParameter issue

    sammy8306

      I'm trying to create a nice user experience for pages that require requestparameters. However, when a value passed in a parameter is not convertible to the type it is injected into (I'm using Longs), Seam throws an exception, redirecting me to the debug page. Can I catch this exception somehow in an @Create method?
      Ofcourse I could make all my RequestParams String and handle the conversion myself, but hey...

        • 1. Re: @RequestParameter issue
          christian.bauer

          @RequestParameter conversion from a String request parameter to a Long field or property is working fine in my app. You need to show some code if you want help.

          • 2. Re: @RequestParameter issue
            sammy8306

            I think there's a little misunderstanding... indeed, the conversion is working just fine when I do:

            /app/somepage.xhtml?id=1

            which is convertible (by parseLong() I guess) to Long and therefore assignable to this field:

            @RequestParameter("id")
            private Long id;
            


            However, my problem is with something like this:

            /app/somepage.xhtml?id=a

            Now, a NumberFormatException is raised in the convertor. I would like to gracefully fail (display a nice error message or something). The question is whether this is possible (without explicitly doing the conversion my self)




            • 3. Re: @RequestParameter issue
              christian.bauer

              Well, 'a' is not a number! :) You can't gracefully fail, just don't put illegal request arguments into your application logic.

              • 4. Re: @RequestParameter issue
                sammy8306

                Well uhm... what about (ab)users? Of course I'm not going to code such things into the application logic, but it seemed only natural to me that my application could respond to malformed data.

                • 5. Re: @RequestParameter issue
                  christian.bauer

                  It does react to malformed data, it throws a runtime exception. What more do you want? Redirect this exception to an error page.

                  • 6. Re: @RequestParameter issue
                    sammy8306

                    I wasn't trying to be hostile or anything, sorry if I came across like that?

                    What I want, is some mechanism to create (for example) a facesmessage containing a reference to the parameter name stating that invalid information is entered. However, 1) we are dealing with a GET, so facesmessages are not displayed directly (can work around that using a conditionally rendered div or something) 2) the runtime exception is not being thrown in the context of the component that accesses the requestparameter.
                    If redirecting based on the exception is the only mechanism available, then I'll use it - if there was an easier solution (which is why I asked the question) I would like to use that.

                    • 7. Re: @RequestParameter issue
                      p-a

                      How about taking care of the conversion yourself, by letting the requestparam being just a String? A try-catch in your method takes care of any Exceptions, and some static code somewhere which you can call with params like the name of the reqparam, and some errorstring that it will set to the facesmessages?

                      • 8. Re: @RequestParameter issue
                        p-a

                        Ahh, guess I shold have read your first post to the end..
                        "Ofcourse I could make all my RequestParams String and handle the conversion myself, but hey..."

                        Nvm ;)

                        • 9. Re: @RequestParameter issue
                          sammy8306

                          Hm, should I go that route, is there any way to force the facesmessages to be displayed after a GET-request instead of after a POSTback? Right now I'm using <s:div rendered="!#{component.initialized}"> to display an error message, where I manually manage the initialized boolean.

                          • 10. Re: @RequestParameter issue
                            christian.bauer

                            You are over-engineering, is all. An error page is perfectly acceptable if a user plays with the URL.