Hi Goeff,

I've seen a few other suggestions, all of which look like they'll work fine.

But, it's a slow day here (I've just resigned :->), so I thought I might
share my implementation of this very common requirement.  I like to code as
much functionality as possible in beans, avoiding lots of looping scriplets
in the JSP, if I can.  But, on the other hand, you don't want move too much
HTML generation into the beans, otherwise you may as well be back using
servlets...

So, to get a dynamic drop list, I do something like this in the JSP (v1.0):

<%--
Declare the bean that generates the drop list.  Done at session scope so it
can
do some per-user caching and avoid db access on each page it is used.
--%>

<jsp:useBean id="statusList" class="barrack.ccr.StatusList"
scope="session"/>


<%--
Set the currently selected value in the drop list from the request
parameters.  This
is useful if this page forwards to itself and you want to see what was
previously
selected
--%>

<jsp:setProperty name="statusList"  property="selectedStatus"
param="filterStatus"/>


<%--
Generate the drop list options within a normal HTML Option tag.  This allows
the
HTML/JSP designer to still control most aspects of the presentation without
having to get into the beans Java code.  They can even manually include
additional
entries, like the "All" one below.
--%>

<select name="filterStatus" size="1">
  <option value="%">All</option>
  <jsp:getProperty name="statusList" property="statusOptions"/>
</select>


The StatusList bean includes the following method to get the list of HTML
Options :

    /**
    * Get status option list
    */
    public String getStatusOptions()
    {
        /*
        * Return the option list
        */
        return (JSPUtil.toHTMLOptions(this.statusList,
this.selectedStatus));

    } // getStatusOptions

The "JSPUtil.toHTMLOptions" method is a static helper function that converts
a Vector of String values into String of HTML "<option value="x">x</option>"
tags and will mark one of them "selected" if it matches the second
parameter.  The "this.statusList" is the cached list of values from the
database that I want to display.

These type of Java data -> HTML transformations (like HTML encoding of
String values) are required a lot in JSP, so I have been packing this
JSPUtil class with these sorts of functions ever since, rather than
re-coding them in each bean.

I am aware of a small problem in the above example, for those with an eye
for detail and concurrency issues.  Because the bean is stored at the
session scope, and the currently selected value is stored in the bean, if
two requests from the same session access the the bean simultaneously, one
will probably show the currently selected value from the other....buggar.
Where this might occur, I have to change the bean's scope back to request or
page.

Regards

Drew Cox
Barrack Consulting

> -----Original Message-----
> From: Orr, Geoffrey [SMTP:[EMAIL PROTECTED]]
> Sent: Wednesday, January 12, 2000 1:43 AM
> To:   [EMAIL PROTECTED]
> Subject:      has anyone implemented dynamic drop down list boxes using
> JSP?
>
> I know that I can generate my entire form using a servelet, with drop down
> values populated
> from the database, but is there anyway I can embed this sort of function
> in
> a file system html
> or jsp page?
>
> I'm convincing myself that this can't be done, but can it?  Has anyone
> figured out any clever workarounds?
>
> Thanks,
>
> Geoff Orr
>
> Application Systems Engineer 5
> Pricing Engine Group
> Norwest Mortgage Systems Development Group
> Wells Fargo Service Company
> 301-846-8403 (6-8403)
>
> ==========================================================================
> =
> 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

===========================================================================
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