I solved the dependency problem with JavaScript. I basically added an `onChange'
handler, made it generic as possible.

<script language="JavaScript" >
<!-- HIDE

function refreshPageFromSelect( selector, command_value )
{
    // Dynamic set options for the city  "secondary" combo box.
    var N = selector.options.length;
    var index = selector.options.selectedIndex;
    var item = selector.options[index].value;

    // alert("You selected item:`"+item+"' index:"+index+" 
command_value:`"+command_value+"'" );

    selector.form.command.value = command_value ;
    if ( item != "#" ) {
        // Submit the form data
        selector.form.submit();
    }
}

// END HIDE -->
</script>

In my form I added the hidden input field called "command" around with the Scriptlets.
The JavaScript onChangeHandler is passed the select HTML element and the
value to set the "command" hidden input field. In this way the LocationController 
servlet
knows how to up date the LocationModel. The location model is decide what
data is available for the field.
See below:

<form action="/servlet/learn.jsp.LocationController" >

<input type="hidden" name="command" value="nocommand" >

<%-- Fill in the primary pull down --%>
<select name="country" size="1" onChange="refreshPageFromSelect(this, 
'updateCountry')" >
    <option value="#">PLEASE SELECT ONE</option>
    <option value="#">=================</option>
<%
     // This JSP gets the model from the session and
     // retrieves the list of available countries in the
     // primary pull down
     String [] countries = model.getAvailableCountries();
     for ( int k=0; k<countries.length; ++k ) {
        String selected =
            ( model.getCountry().equals( countries[k] ) ? "selected" : "" );
 %>
    <option value="<%= countries[k] %>" <%= selected %> ><%= countries[k] %></option>
<%
     }
 %>
</select>

<br><br>

<%-- Fill in the secondary pull down --%>
<select name="city" size="1" onChange="refreshPageFromSelect(this, 'updateCity' )" >
    <option value="#">PLEASE SELECT ONE</option>
    <option value="#">=================</option>
<%
     // This JSP gets the model from the session and
     // retrieves the list of available cities in the
     // secondary pull down provided that a valid country
     // has been chosen beforehand.
     String chosen_country = model.getCountry();
     if ( !chosen_country.equals("nocountry") ) {
        String [] cities = model.getAvailableCities();
        for ( int k=0; k<cities.length; ++k ) {
            String selected =
               ( model.getCity().equals( cities[k] ) ? "selected" : "" );
 %>
             <option value="<%= cities[k] %>" <%= selected %> ><%= cities[k] 
%></option>
<%
        }
     }
 %>

</select>

</form>

This is mode of operation which you suggested exactly and this JSP example is
working fine. The MVC is a bit messy with the model being accessed
inside the JSP page (view) itself.

I would like to change this JSP into reusable components, but I am not sure how?
Something tells me, custom taglibs is the answer. I just wanted know if anyone else
has solve this problem.

The easiest part is to write a OptionListBean that fills itself from a database

1) "select Country from CountryTable"

2) "select City from CityTable where CityTable.Country_KEY = CountryTable.Country.KEY "

The next bit is binding these beans together so that they can easily be reuse
in a web page. Hmmmm

More reading and researching required methinks.

--
Peter Pilgrim
G.O.A.T
                    "The Greatest of All Time"



---------------------------------------- Message History 
----------------------------------------


From: David Bullock <[EMAIL PROTECTED]> on 21/11/2000 22:20 ZE10B

To:   Peter Pilgrim/DMGIT/DMG UK/DeuBa@DMG UK
cc:   [EMAIL PROTECTED]
Subject:  Re: JSP and Form Pulldown that depends on another Form Pulldown


Hi Peter.

A few comments below.

On Tue, 21 Nov 2000, Peter Pilgrim wrote:

> See the problem ? Second pulldown depends on the first.
>
> It is easy to write this as a static HTML web page if the data is static
> and bind it with a little JavaScript.
>
> But what if the values for the HTML Option element all come from a database.

You should still be able to use JavaScript in this case, except that you will have to 
partially generate your JavaScript as well ;-)

Another way that I've seen this done, (I think, on the BEA or Oracle sites) is, once 
the country is seleted, a small piece of JavaScript automatically POSTs the form to 
the server from the onChange event of the list box.  The server returns the form, 
slightly modified for the specific country, but with the values that the user has 
already entered, intact.  This works quite well if the server response is snappy.  One 
of the 'little extras' required to make this work well, however, is to somehow scroll 
the window back to roughly the same spot if the form is longer than their window.  I 
don't know if it's possible to do this in JavaScript.

> I think you write some sort of OptionPulldownBean. But how do get these
> beans depended on one another?

You could do this in a number of ways:

 - in Netscape, so-called 'LiveScript' allows applets to talk to each other in the 
same page.
 - in IE, probably some equivalent technology, but different ;-)
 - in both, using CORBA, and having each bean talk to a session on the server, and 
having the server do a 'callback' on one.  This is fairly fun to do, but a lot of 
effort, and because of the CORBA libraries, it means large-ish download times for the 
applets.


> How would you write this requirement in JSP ( and the MVC pattern (sic) )?

Can't answer that for you - I'm a dedicated XMLC user http://xmlc.enhyrda.org/ ... but 
I just saw something about the 'Model 2 presentation paradigm', that I'm going to look 
at tomorrow ...


have fun!
David.



--

This e-mail may contain confidential and/or privileged information. If you are not the 
intended recipient (or have received this e-mail in error) please notify the sender 
immediately and destroy this e-mail. Any unauthorised copying, disclosure or 
distribution of the material in this e-mail is strictly forbidden.

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to