4 Replies Latest reply on Dec 8, 2009 1:15 PM by Christoph Kraus

    s:graphicImage with file protocol URL

    Christoph Kraus Newbie

      Hi guys!


      Just a short question...
      Is it possible to use the <s:graphicImage> component with an file protocol URL and if so, how?


      I tried something like this:


      <s:graphicImage url='file:///C|/test.jpg'/>



      but it hasn't worked.


      I am using Seam 2.2.0 on jboss 4.2.4.


      Thanks in advance,
      Chris




        • 1. Re: s:graphicImage with file protocol URL
          Emir Calabuch Newbie

          Don't try to use it like that, even if you managed to render a correct URL, it would not be an http:// URL but a file:// URL (and therefore it would be available only on your local machine, but not via HTTP, and a browser in another machine would not be able to see the image).


          s:graphicImage has a "value" parameter that can be an URL or file string, use it instead of the "url" parameter you were trying to use, like this:


          <s:graphicImage value='file:///C|/test.jpg' />
          



          s:graphicImage will load the image into the document store and serve it via HTTP, so the real URL of the image will be an http:// URL and not a file://. The image file has to be .jpeg, .png or .gif (other image types should not work).


          Also, be sure to setup the DocumentStoreServlet in your web.xml file, as stated in the documentation (Section 30.1.3 of the Seam 2.2.0 reference), otherwise s:graphicImage could not work (as stated previously it uses the document store).


          • 2. Re: s:graphicImage with file protocol URL
            Christoph Kraus Newbie

            Thanks for your advice.


            I configured the DocumentStoreServlet in my web.xml.
            It looks like this:



            <servlet>
               <servlet-name>Document Store Servlet</servlet-name>
               <servlet-class>org.jboss.seam.document.DocumentStoreServlet</servlet-class</servlet-class>
            </servlet>
            <servlet-mapping>
               <servlet-name>Document Store Servlet</servlet-name>
               <url-pattern>/generated/*</url-pattern>
            </servlet-mapping>
            


            and the graphicImage-Component looks like this:



            <s:graphicImage value="file:///C|/test.jpg"/>
            





            But now, I get an Exception thrown:
            Exception during request processing:
            Caused by java.lang.IllegalArgumentException with message: "Image pointed to must exist (input stream must not be null)"


            I'm a bit confused, because I'm sure that the image exists. Did I still miss something? Tell me, if you need any further information.



            • 3. Re: s:graphicImage with file protocol URL
              Emir Calabuch Newbie

              If you pass a plain string as argument to s:graphicImage, it is interpreted as a classpath based path, as there is no "file:///C/test.jpg" file in the classpath, it is not found and the exception you note is raised. You cannot indicate an URL directly in the tag like that (at least not without some EL kung-fu).


              If the image you want to display is located in your project's classpath (i.e., under the WebContent folder, where usually static images are), just provide a path that is relative to the WebContent's root (i.e., /img/test.jpg), but in this case you can simply use h:graphicImage, as the image is already visible from http.


              To render an image located elsewhere in the server, you need to create a component with a method that returns one of the following:



              • a java.lang.String that points to a file in the classpath (this is the same as passing a plain string),

              • a java.io.File or java.net.URL object that points to an image anywhere in your system (this is what you need to do),

              • the image itself via a byte[] or java.io.InputStream (this is useful if your image is not stored in a file, but in a database blob or other resource that cannot be pointed to with an URL).



              So, just create a component like this (here I'm returning a java.io.File, you can as easily return an URL with new URL("file:///C:/test.jpg")):


              @Name("imageAccessor")
              public class ImageAccessor {
                  public File getFile() throws Exception {
                      return new File("C:/test.jpg");
                  }
              }
              



              And use the component's getFile() method to provide the value to the s:graphicImage tag, like this:


              <s:graphicImage value="#{imageAccessor.file}" />
              



              Of course, in real life the component would retrieve the image to display using some business-specific logic (i.e. a database record or something else).


              Note that the image that is displayed will have a generated URL like the following:


              /myProj/generated/graphicImage/org.jboss.seam.ui.GraphicImageStore.-7430a4f-1255a1c7b1b--7f87.png
              



              If you want to provide an user-friendlier name (specially if the user is likely to save the image locally), use the fileName argument in s:graphicImage, passing the name you wish the image to have, minus the extension (Seam adds the correct extension for you), like this:


              <s:graphicImage value="#{imageAccessor.file}" fileName="test" />
              

              • 4. Re: s:graphicImage with file protocol URL
                Christoph Kraus Newbie

                Thank you Emir, now it works perfect.