You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Tom Bednarz <li...@bednarz.ch> on 2007/02/28 18:41:55 UTC

Accessing form bean properties from within JavaScript

I like to do the following: When a selection in a select box changes, 
display a value from a mapped property in a textarea field. To do this I 
need to get the currently selected value of the ComboBox by calling some 
javascript function in the onchange event of SELECT.

This is my JSP code:

<html:form action="/startReportWizard" focus="reportKind">
   <table border="0" width="500" align="center">
     <TR>
     <TD width=250><bean:message key="GenerateReportPage1.Selection" />
     <TD width=*>
       <html:select property="reportKind" 
onchange="displayReportDescription()">							<html:optionsCollection 
name="reportDescriptions" value="value.report_ID" 
label="value.report_Name"/>
       </html:select>
     </TD>
   </TR>
   <TR>
     <TD><bean:message key="GenerateReportPage1.Description" /></TD>
     <TD>
       <html:textarea property="reportDescription" rows="3" 
disabled="false"/></TD>
   </TR>
....

The javascript function displayReportDescription() returns the currently 
selected value of the ComboBox (Select list)

In the form bean I have a Hashtable as follows:

private Hashtable<String, ReportDescription> availableReports;

public synchronized Hashtable<String, ReportDescription> 
getAvailableReports()
{
	return availableReports;
}

public synchronized void setAvailableReports(
		Hashtable<String, ReportDescription> availableReports)
{
	this.availableReports = availableReports;
}

public ReportDescription getValue(String key)
{
	return availableReports.get(key);
}


With these getters I should be able to do the following in my JSP to 
retrieve a specific value from my Hashtable:

<html:textarea property="value(name)"/>
where name is the VALUE of the currently selected Item of the Select box.

So inside my javascript function I should do something like:

function displayReportDescription()
{
   var ComboBox=document.forms[0].elements["reportKind"];
   var selected = ComboBox.selectedIndex;
   var selectedValue = ComboBox.options[selected].value;
   document.forms[0].elements["reportDescription"].value = THE VALUE 
FROM MY HASHTABLE IN MY FORMBEAN!!
}

The question is how can I call the 
getAvailableReports().getValue(selectedValue) of my form bean from 
inside the above JavaScript????

Any help would be greatly appreciated.

Tom



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org