7 Replies Latest reply on Sep 8, 2009 3:13 AM by nbelaevski

    a4j:mediaOutput - Only one Image is shown instead of multipl

    gamba

      I would like to render multiple JPG-images with an a4j:mediaOutput-component. If I use the component once the renderImage methode is called correctly.

      But I want to call the method multiple-times for multiple images. So I used the a4j:repeat - component, but the renderImage - method is still called once:

      <a4j:repeat value="#{imageViewerBean.allPages}" var="page"
       rowKeyVar="page">
      
       <a4j:mediaOutput id="pisPainter" mimeType="image/jpeg"
       element="img" cacheable="false" createContent="#
       {pdfViewerBean.renderImage}" session="true">
       <f:param value="#{pdfDocumentManagerBean.lastModified}"
       name="time"/>
       </a4j:mediaOutput>
      </a4j:repeat>
      


      How to render unknown size of mutliple images with mediaOutput?

        • 1. Re: a4j:mediaOutput - Only one Image is shown instead of mul
          nbelaevski

          Hi,

          Set "data" attribute of a4j:mediaOutput to some random-generated value and try.

          • 2. Re: a4j:mediaOutput - Only one Image is shown instead of mul
            gamba

            I've added the following

            data="#{pdfViewerBean.lastModified}"

            and then the value attribute, because I don't found the supported attribute "data":
            value="#{pdfViewerBean.lastModified}"

            Both examples results in the same behaviour as before. The renderImage method is only called once but I have 5 iterations to go.

            My renderImage - method looks like the following snippet:

            public void renderPdf(OutputStream out, Object data)
            {
             System.out.println("=====> CALL ON ME ...");
             ....
             ImageIO.write(renderedImage, "jpeg", out);
             ...
            }
            


            It is referenced in a request-scoped bean and data-param is never referenced because I don't need it or do I?

            Thx for any help
            Gamba

            • 3. Re: a4j:mediaOutput - Only one Image is shown instead of mul
              nbelaevski

              Gamba,

              Yes, I'm sorry, "value" is the right attribute to use. Value of this attribute should be different for each iteration item - does

              #{pdfViewerBean.lastModified}
              satisfy this condition?

              • 4. Re: a4j:mediaOutput - Only one Image is shown instead of mul
                gamba

                First of all I set

                value="#{pdfViewerBean.lastModified} in the renderImage-method to a new date-object. Actually I set it at the end of the method to a new int:

                public void renderImage(OutputStream out, Object data)
                {
                 System.out.println("=====> CALL ON ME ...");
                 ....
                 ImageIO.write(renderedImage, "jpeg", out);
                 ...
                
                 int random = new Random().nextInt();
                 System.out.println ("SetLastModified to a new Value = " +random);
                 this.setLastModified(random);
                }
                


                The images are repeated three times with this method:

                public List<Integer> getAllPages()
                {
                 List<Integer> nrOfPages = new LinkedList<Integer>();
                 nrOfPages .add(new Integer(1));
                 nrOfPages .add(new Integer(2));
                 nrOfPages .add(new Integer(3));
                
                 return nrOfPages ;
                }
                


                The image is an PDF, which is splitted in multiple jpgs, but that shouldn't matter. The first image of the PDF is rendered three times, like expected.
                But the method renderImage(...) is only called once .... I have only on output of

                System.out.println("=====> CALL ON ME ...");

                and only one newly generated Random-Number. So I can't read the page-key to load other images and the first one is displayed three times.

                Is there any cache-behaviour I did not know at this time ?

                Thx
                Gamba

                BTW: A workaround for me is rendering all images at once, when the renderImage-method is called (only once). But at the moment I'm not able to write multiple images to the outputStream: ImageIO.write(renderedImage, "jpeg", out);

                • 5. Re: a4j:mediaOutput - Only one Image is shown instead of mul
                  gamba

                  Using the value="#{page}" as changing value seems to be working and the
                  page-attribute is saved in the data-object variable ...

                  Thx for the help, now it works fine for me, although I don't even know why the solution with the date-attribute is still not working ...

                  Gamba

                  • 6. Re: a4j:mediaOutput - Only one Image is shown instead of mul
                    nbelaevski

                    Hi Gamba,

                    I've checked your example - works ok:

                    private int counter = 0;
                    
                     public int getCounter() {
                     return counter++;
                     }
                    
                    
                     public List<Integer> getAllPages() {
                     List<Integer> nrOfPages = new LinkedList<Integer>();
                     nrOfPages .add(new Integer(1));
                     nrOfPages .add(new Integer(2));
                     nrOfPages .add(new Integer(3));
                    
                     return nrOfPages ;
                     }
                    
                     public void paint(OutputStream stream, Object data) {
                     System.out.println("forum5Bean.paint()");
                     BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
                     Graphics2D g2d = GraphicsEnvironment.getLocalGraphicsEnvironment().createGraphics(image);
                     g2d.setColor(Color.ORANGE);
                     g2d.drawString(data.toString(), 50, 50);
                     g2d.dispose();
                    
                     try {
                     ImageIO.write(image, "jpeg", stream);
                     } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                     }
                     }
                    


                    <a4j:repeat value="#{forum5Bean.allPages}" var="page" rowKeyVar="page">
                     <a4j:mediaOutput id="pisPainter" mimeType="image/jpeg" element="img"
                     cacheable="false" createContent="#{forum5Bean.paint}" session="true" value="#{page}">
                     <f:param value="#{forum5Bean.counter}" name="time" />
                     </a4j:mediaOutput>
                     </a4j:repeat>


                    • 7. Re: a4j:mediaOutput - Only one Image is shown instead of mul
                      nbelaevski

                      For your code: lastModified should be changed every time it is requested; image tags are first being rendered, only then images bytes are requested and "createContent" method is called.