1 Reply Latest reply: Jan 6, 2009 1:33 AM by 843807 RSS

    Just a little help...

    843807
      Greetings,

      I'm new in JavaBeans, and at the vary begin I have a sort of problem, and it's about the logic insede JavaBeans, or I have an incomplete information (data) within the project I'm studying.
      This is a simple page using bean:

      <html>
      <head>
      <title>A dose of Dilbert</title>
      </head>
      <body bgcolor="white">
      <h1>A dose of Dilbert</h1>

      <jsp:useBean id="cartoon"
      class="com.ora.jsp.beans.motd.CartoonBean" />
      <img src="images/<jsp:getProperty name="cartoon"
      property="fileName" />">

      </body>
      </html>

      And this is a bean(a Java's class file), where is determinated bean's logic :

      package com.ora.jsp.beans.motd;

      import java.util.*;
      public class CartoonBean implements java.io.Serializable {
      private static int index = -1;
      private List fileNames;

      public CartoonBean( ) {
      initFileList( );
      }

      public String getFileName( ) {
      index++;
      if (index > fileNames.size( ) - 1) {
      index = 0;
      }
      return (String) fileNames.get(index);
      }

      private void initFileList( ) {
      fileNames = new ArrayList( );
      fileNames.add("dilbert2001113293109.gif");
      ...
      }
      }


      In the "images" folder there is stored a four images, variously named (dilbert2001113293109.gif, dilbert2001166171108.gif, and so on...)
      Each time I reload the page (above) I got a different images displayed on the page. Well, it should be a logic of bean, right?
      But according to bean above, I cannot find any logic or program flow that allow an images of completely different names to be invoked (displayed)???? When bean is called, we retrieve a his getter method value, and here it is this line : " return (String) fileNames.get(index);" But no where in the code we store an images of different name in the collection but in one place?
      So where is the place where were stored a remained three images from the folder????
        • 1. Re: Just a little help...
          843807
          You are using a BEAN on one a single page without defining the scope of it. So by default the scope of bean is PAGE.
          This means when the page is loaded, there is a bean initialized and same is available to that particular page.

          Now in your bean, the method getFileName(), increases everytime the value of variable "index", and reading the name from list of that index.

          Definitely everytime you refresh the page, the value of "index" will be increased and you will get the next image_file_name.


          For better understanding, i will suggest to run the application in debug mode and put the dubug point inside your bean methods.