Hi,

I am in the process of converting our site to JSP. One thing I am doing is
reworking our servlets which contain all the HTML returned on dynamic pages
in them. We have a FORM package that contains classes for each type of
<input..> type and takes arguments. For example, a ListBox takes in a
vector argument of the items to display. It also takes in a vector argument
for the multiple selected items. Actually..I dont recall if it does this or
not..it may just take a single vector and the objects in the vector may
signal if the item is selected or not. In any event, what I want to do is
something similar for my JSP pages. I am using the Model 1 (1.5) approach,
so some logic is done on the pages, to iterate through a list, if()
conditions to display some html based on a given condition, etc. Minor
stuff..but a little bit of logic none the less.

So..I want to create a "generic" list box (single selection), list box with
multiple selection, etc..but be able to include it on many pages, each page
possibly taking a different vector of data.

The way I have done this so far is EVERY page includes a "variables.inc"
file. Its a scriplet of code <% %> that defines a bunch of variables.
Oh..let me say that I am limited to JSP 1.0, so I cant use the TagLib stuff
at this point, otherwise I would go that direction instead. Until that
time, I am stuck doing it this way. So, what I do is I put something like so:


-------------
variables.inc
-------------
<%
  Vector listvector = null;
  String listselect = null;
  Vector listmultiselect = null;
%>

-----------
listbox.inc  // single select
-----------

<%
  if( listvector != null )
  {
    for( int cntr = 0; cntr < listvector.size(); cntr++ )
    {
      IListable item = (IListable) listvector.elementAt(cntr);
      if( item != null )
      {
        if( listselect.equals( Long.toString( item.getId() ) ) )
        {
%>
         <option selected value="<%= Long.toString( item.getId() ) %>"><%=
item.getName() %>
<%
        }
        else
        {
%>
        <option value="<%= Long.toString( item.getId() ) %>"><%=
item.getName() %>
<%
        }
      }
    }
  }
%>

---------------------------------------------------------------

First, I dont know if the above code is perfect. I just typed that in
without testing it. I dont have it in place yet. That is why I am asking
this. At any rate, the idea is, EVERY page includes the variables.inc at
the top of it. Then, JSP pages with forms on them that need a listbox
displayed, just includes the listbox.inc. The code would be something like:

<%@ include file="/include/variables.inc" %>
...
<jsp:useBean id="mybean" scope="session" class="com.bm.ui.beans.SomeBean" />
<jsp:setProperty name="mybean" property="*" />

<form name="FormName" method="post">
<% listselect = mybean.getSomeListSelectString() %>
<select name="SelectListBox">
<%@ include file="/include/listbox.inc" %>
</select>

....



SOOOO..with the above example..is this the BEST way to do this? I cant pass
a variable(s) to an included file in any way, so is what I am doing the
best way to go about this? Or is there a better way so that I can use the
same generic listbox on any form page I want? Oh..before I forget,
IListable is an Interface we defined that all of our classes that return a
Vector of items that are to be displayed in a listbox, implement. So, that
is why I convert the element back to an IListable object..since I only need
to call the getId() and getName() methods to get the id (value="") and the
name of the option item.

I look forward to any knowledge anyone can share with me on perhaps a
better, more effecient way to do this, or if this is good enough, bad, etc.

Thank you.


Kevin Duffey
Software Engineer
[EMAIL PROTECTED]

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html

Reply via email to