struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "chiji nwankwo" <>
Subject Re: struts selection and Javascript onchange question
Date Wed, 29 Aug 2001 10:13:49 GMT
<html><div style='background-color:'><DIV>
<P>I had a similar problem and tried to find the best way to handle the problem, without
mixing&nbsp; JavaScript and&nbsp;jsp syntax.&nbsp; What I came up with was a series
of&nbsp;select boxes, some of&nbsp;which were dependant on others,&nbsp;that changed
their values based on&nbsp;the selection made in a different select box.&nbsp;&nbsp;I
have&nbsp;a series of select boxes that&nbsp;call the submit method&nbsp;everytime
a change is made.&nbsp; Calling the submit method in the onchange event handler, means
that the form posts itself every time selection is made.&nbsp;&nbsp;I can then track
which of the&nbsp;select boxes was changed, read the new value, which has just been submitted
and populate the other lists based on the newly changed&nbsp;value.&nbsp; This is
an example of what I mean.</P>
<P>Within jsp page.</P>
<P><EM>&lt;html:form action="/"&gt;&nbsp;<BR>&nbsp;&nbsp;&nbsp;
&lt;bean:message key="select.title.seasons" /&gt;<BR>&nbsp;&nbsp;&nbsp;
&lt;html:select property="season" onchange="submit()"&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;html:options property="value" collection="seasonsList" /&gt;<BR>&nbsp;&nbsp;&nbsp;
<P><EM>&nbsp;&nbsp;&nbsp;&lt;html:hidden property="option" value="season"
/&gt;<BR>&nbsp;&nbsp; &lt;html:hidden property="action" value="Populate"
key="select.title.oppositions" /&gt;<BR>&nbsp;&nbsp; &nbsp;&nbsp;&lt;html:select
property="opposition" onchange="submit()"&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;html:options property="value" collection="oppositionsList" /&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;
property="option" value="opposition" /&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;
&lt;html:hidden property="action" value="Populate" /&gt;<BR>&nbsp;&lt;/html:form&gt;&nbsp;</EM></P>
<P>Within perform method:</P>
<P><EM>String action = &lt;form instance&gt;.getAction();&nbsp;&nbsp;
// action set by hidden variable in jsp page</EM></P>
<P><EM>String option = &lt;form-instance&gt;.getOption(); // option set
by hidden variable in jsp page</EM></P>
<P>if the action is equal to the action specified in the form's hidden variable and
option is equal to option specified in the forms hidden variable, then retrieve the selected
value of the first select box and populate the arrayList of options for the second select
<P>I hope this makes sense.</P>
<P>&gt;From: Robin Whitley <RWHITLEY@SILVERSTREAM.COM></P></DIV>
<DIV></DIV>&gt;Subject: struts selection and Javascript onchange question

<DIV></DIV>&gt;Date: Tue, 28 Aug 2001 23:25:50 -0400 
<DIV></DIV>&gt;MIME-Version: 1.0 
<DIV></DIV>&gt;Received: from [] by (3.2) with ESMTP
id MHotMailBD55ABD5004540043217407D85140B790; Tue, 28 Aug 2001 20:26:13 -0700 
<DIV></DIV>&gt;Received: (qmail 11536 invoked by uid 500); 29 Aug 2001 03:24:58
<DIV></DIV>&gt;Received: (qmail 11524 invoked from network); 29 Aug 2001 03:24:58
<DIV></DIV>&gt;Received: from (
by with SMTP; 29 Aug 2001 03:24:58 -0000 
<DIV></DIV>&gt;Received: by COSMO with Internet Mail Service (5.5.2653.19)id
<RZZ50VDZ>; Tue, 28 Aug 2001 23:25:51 -0400 
<DIV></DIV>&gt;From struts-user-return-16241-cn081 Tue, 28 Aug 2001 20:27:23
<DIV></DIV>&gt;Mailing-List: contact;
run by ezmlm 
<DIV></DIV>&gt;Precedence: bulk 
<DIV></DIV>&gt;list-help: <?xml:namespace prefix = mailto /><>
<DIV></DIV>&gt;list-unsubscribe: <>
<DIV></DIV>&gt;list-post: <>
<DIV></DIV>&gt;Delivered-To: mailing list 
<DIV></DIV>&gt;Message-ID: &lt;3125D8065A0AD411A90A009027B11742058942@COSMO&gt;

<DIV></DIV>&gt;X-Mailer: Internet Mail Service (5.5.2653.19) 
<DIV></DIV>&gt;X-Spam-Rating: 1.6.2 0/1000/N 
<DIV></DIV>&gt;I have a form that has a selection box for country. If "UNITED
<DIV></DIV>&gt;selected, the form has a stateProvince selection box that should
<DIV></DIV>&gt;populated with "state" options. If "CANADA" is selected, the
<DIV></DIV>&gt;selection box should be populated with the "province" options.
I use 
<DIV></DIV>&gt;struts_logic:equal based on the "country" bean property 
<DIV></DIV>&gt;to accomplish this. The form populates the struts_html:options
from a 
<DIV></DIV>&gt;collection. The logic works properly if there is an initial
value for 
<DIV></DIV>&gt;country. However, when the country option is changed, the onchange
<DIV></DIV>&gt;does not appear to fire the javascript function fillStateProvince.

<DIV></DIV>&gt;What I tried to do in the fillStateProvince function was to
test the 
<DIV></DIV>&gt;selected option for country, and set the value on the bean,
hoping that this 
<DIV></DIV>&gt;would set my struts_html:options properly. My sysouts indicate
that when 
<DIV></DIV>&gt;the form loads, it sets the country value to UNITED STATES
and then to 
<DIV></DIV>&gt;CANDADA, but that when I change country, the function doesn't
<DIV></DIV>&gt;I've included the pertinent code. My question, is this the
proper way to 
<DIV></DIV>&gt;handle this or is there a better way to accomplish the same
<DIV></DIV>&gt;Does anybody see anything silly that I am doing in the Javascript,
or struts 
<DIV></DIV>&gt;logic that is causing this result. 
<DIV></DIV>&gt;Any help would be appreciated. 
<DIV></DIV>&gt;Robin Whitley 
<DIV></DIV>&gt;<% -- Javascript -- %> 
<DIV></DIV>&gt;<%@ page language="Java" contentType="text/html; charset=UTF-8"
</DIV>>errorPage="error.jsp" %> 
<DIV></DIV>&gt;<?xml:namespace prefix = jsp /><jsp:useBean
id=user scope="session"></jsp:useBean> 
<SCRIPT language=JavaScript>
</DIV>><function fillStateProvince(inForm,selected) {
</DIV>>  if (selected == 'UNITED STATES') {
</DIV>>   <%
</DIV>>	user.setCountry("UNITED STATES");
</DIV>>	System.out.println(" Set Country United States");
</DIV>>   %>
</DIV>>  }
</DIV>>   if (selected == 'CANADA') {
</DIV>>    <%
</DIV>>	user.setCountry("CANADA");
</DIV>>	System.out.println(" Set Country CANADA");
</DIV>>    %>
</DIV>>  }
<DIV></DIV>&gt;<%-- In the form - country -- %> 
<DIV></DIV>&gt;<TD colspan="2"> 
<DIV></DIV>&gt; <?xml:namespace prefix = struts_html /><struts_html:select
property="country" onchange="javascript:&#13;</DIV>>optionsStateProvince(document.userRegistrationForm,document.userRegistration&#13;</DIV>>[].te&#13;</DIV>>xt)">
<DIV></DIV>&gt; <struts_html:options property="value" DIV < collection="countryOptions">&gt;labelProperty="label"
<DIV></DIV>&gt; </struts_html:select>
<DIV></DIV>&gt;<%-- In the form - stateProvince -- %> 
<DIV></DIV>&gt; <TR valign="top">
<DIV></DIV>&gt; <TD class=bodytext width="500">
<DIV></DIV>&gt; <?xml:namespace prefix = struts_bean /><struts_bean:message
<DIV></DIV>&gt; </TD>
<DIV></DIV>&gt; <?xml:namespace prefix = struts_logic /><struts_logic:equal
scope="session" property="country" DIV < name="user">&gt;value="UNITED STATES"&gt;

<DIV></DIV>&gt; <TD colspan="2" align="left" height="29">
<DIV></DIV>&gt; <struts_html:select property="state">
<DIV></DIV>&gt; <struts_html:options property="value" DIV < collection="stateOptions">&gt;labelProperty="label"
<DIV></DIV>&gt; </struts_html:select>
<DIV></DIV>&gt; </TD>
<DIV></DIV>&gt; </struts_logic:equal>
<DIV></DIV>&gt; <struts_logic:equal scope="session" property="country"
DIV < name="user">&gt;value="CANADA"&gt; 
<DIV></DIV>&gt; <TD colspan="2" align="left" height="29">
<DIV></DIV>&gt; <struts_html:select property="state">
<DIV></DIV>&gt; <struts_html:options DIV < collection="provinceOptions">&gt;property="value"
labelProperty="label" /&gt; 
<DIV></DIV>&gt; </struts_html:select>
<DIV></DIV>&gt; </TD>
<DIV></DIV>&gt; </struts_logic:equal>
<DIV></DIV>&gt; </TR>
clear=all><hr>Get your FREE download of MSN Explorer at <a href=''></a><br></html>

View raw message