5 Replies Latest reply on Dec 9, 2008 2:47 PM by geziefer

    How to avoid a 404 error in a <s:graphicImage>?

    geziefer
      Hi there,

      I use a <s:graphicImage> tag within a <rich:dataTable> in my xhtml page with a value containing an URL.
      How can I avoid getting a 404 error if the image where the URL points to doesn't exist?
      I would like to show some "no image available" instead and continue with displaying the page.
      The only way I could make it work was probing the existence by url.getContent() in the bean code and in case of an IOException return a default URL instead - but this can only be a workaround since each file gets unnecessarily loaded twice.
      Any ideas to avoid it or what to use instead for displaying the image?

      Thanks,
      Alex
        • 1. Re: How to avoid a 404 error in a <s:graphicImage>?

          You could try something like:


          <s:graphicImage value="#{testAction.photo}" rendered="#{testAction.photo != null}"/>
          <h:graphicImage value="/path/to/noimage.jpg" rendered="#{testAction.photo == null}"/>
          

          • 2. Re: How to avoid a 404 error in a <s:graphicImage>?
            geziefer

            Thanks for your answer - but unfortunately it won't work that way, because what I get back from the action method is an URL which is build from a stored path from the database and which is not null but points to a non-existing location.
            So what I need is either a tag which won't give me a 404 if the URL of the image is not accessible or a cheap technique to check for an accessible URL without the need of loading the content.
            Alex

            • 3. Re: How to avoid a 404 error in a <s:graphicImage>?

              I did something like this a long time ago. I can't find that code, but you have to use java.net.URL to check your image url, it throws an exception if the resource does not exist.



              Regards
              Franco



              • 4. Re: How to avoid a 404 error in a <s:graphicImage>?
                geziefer

                So you are saying, I should use a method on the retrieved URL to provoke an IOException if not accessible to replace it then by a default URL?
                I thought openConnection() would be enough to do, but it seems, that only if I add openStream() it works that way.
                And do you think, this is a sensible way to deal with the problem - sensible in terms of persormance?

                • 5. Re: How to avoid a 404 error in a <s:graphicImage>?
                  geziefer

                  Hi all again


                  I have to come back to this discussion - unfortunately the solution is not acceptable, because if for example the server on which all the images in the list are located is not available and thus all the URLs in the list point to a non-accessible ressource, it takes a long time to display the page, I guess due to a timeout for each test.


                  So, the question would be, why is a 404 error generated and how can I avoid that and display something else instead?


                  Alex