Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JSP and array problems

Status
Not open for further replies.

cajchris

Programmer
Oct 13, 2005
57
GB
hi,

I am currently developing a search feature for a website which allows searching within multiple directories, and uses JSP to generate the multiple selection list which all works fine. and i have checked the parameters in the browser location bar to make sure that it is picking up multiple selections which it is. here is the JSP that is in my html code:

select name="searchedDirectoryNames" id="selectedDirectories" size="2" multiple>

<option value="about us">about us</option>

<option value="news">news</option>

<option value="technology">technology</option>

</select>

the problem i am getting is that when the user selects some areas, and then hits search it only seems to take the selected item which is closest to the top of the list. here is my java code which is called when the search button is clicked:

public String search() throws Exception
{
log.debug("searchString: " + searchString);
log.debug("number of directories chosen: " + searchedDirectoryNames.length);
List searchDirectories = DirectoryController.getController().getDirectories(this.searchedDirectoryNames);

if (this.searchedDirectoryNames != null && this.searchedDirectoryNames.length>0)
{
for (int y = 0; y < this.searchedDirectoryNames.length; y++)
{
log.debug("directorties from jsp are" + this.searchedDirectoryNames[y]);
}
}
if (searchDirectories != null && searchDirectories.size() >0)
{
for(int i = 0; i < searchDirectories.size(); i++)
{
SearchDirectory sd = (SearchDirectory) searchDirectories.get(i);
log.debug("search directory: " + sd.getDirectoryName());
}
}

this.searchResult = searchController.search(LuceneIndexWriter.getDirectories(searchDirectories), this.searchString, this.startAt);

if (this.searchResult != null && this.searchResult.getResultItems().size() >0)
{
for(int i = 0; i < this.searchResult.getResultItems().size(); i++)
{
ResultItem ri = (ResultItem) this.searchResult.getResultItems().get(i);
log.debug("search result item: " + ri.getUrl());
}
}

this.directories = directoryController.getDirectories();

if(this.resultView == null)
this.resultView = PropertyHelper.getProperty("resultView");

return this.resultView;
//return Action.SUCCESS;
}

the size of the searchedDirectoryNames array is always 1 when multiple items are selected. I have already been to the html and javascript forums and they have confirmed my html and JSP are fine.

regards,
cajchris
 
Doing reqeust.getParameter() on a selection box only sends through the current selection.

You would need to do something more like this (below) to build up the obtions selected.

Code:
<script language="javascript">
	function updateIt() {
		var selectObj = document.getElementById("selectedDirectories");

		var formObj = document.getElementById("myform");
		formObj.dirNamesSelected.value += (selectObj.options[selectObj.selectedIndex].value +'|');
	}

</script>

<form id="myform" action="test.jsp">

	<select onclick="updateIt();" name="searchedDirectoryNames" id="selectedDirectories" size="2" multiple>

	 <option value="about us">about us</option>

	 <option value="news">news</option>

	 <option value="technology">technology</option>

	</select>

	<input type="submit"/>
	<input type="hidden" name="dirNamesSelected"/> <!-- this will hold all the combo box entries, pipe delimited -->
</form>

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
making these changes still results in the same problem. it only seems to be passing an array of size 1 to the Java Program.
 
Server side, you need to change which form value you are retrieving - so :

request.getParameter("dirNamesSelected")

This will give you your list of options selected in a pipe delimeted list. You must then parse the String accordingly.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top