Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple JSP question...Please help???

 
Moined Mogul
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the below piece of code that creates a drop down menu in my JSP page. This may seems like a very silly question but how would you preselect say the 4th item in the list so that on start up the JSP would open with the 4th item selected.
Thanks very much for any assistance you can give.
<%-- Populate the pulldown list of resource titles from the database. --%>
<%
String[] ResourceTitle = Functions.getTitleList();
if (ResourceTitle != null)
{
for (int i=0; i < ResourceTitle.length; i++)
{
if (ResourceTitle.equals (strSelectedResource))
{
//If the string in the strSelectedResource is
//in the pulldown list use it as the
//Preselected option to display.
%>
<OPTION selected value="<%= ResourceTitle %
>"> <%= ResourceTitle %> </OPTION>
<%
}
else
{
%>
<OPTION value="<%= ResourceTitle %>"> <%=
ResourceTitle %> </OPTION>
<%
}
}
}
else
{
%>
<OPTION> No Titles Loaded </OPTION>
<%
}
%>
</SELECT>
</td>
 
Jeff Sunder
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I beleive all you have to do is add'SELECTED' to the first block of your if statment:

if (ResourceTitle.equals (strSelectedResource))
{
//If the string in the strSelectedResource is
//in the pulldown list use it as the
//Preselected option to display.
%>
<OPTION selected value="<%= ResourceTitle %
>"> <%= ResourceTitle %> SELECTED</OPTION>
<%
}
else
{
%>
<OPTION value="<%= ResourceTitle %>"> <%=
ResourceTitle %> </OPTION>
<%
}
 
Dipti Alurkar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that you need to add the index of the ResourceTitle array in the 'if'.
if (ResourceTitle[i].equals (strSelectedResource))
this might help.
the 'SELECTED' word is already included in the <OPTION> tag.
Dipti
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic