Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set default value for drop-down/select in JSP?

I have an arraylist for Strings eqArray.

I need to show it in a drop-down list for which I'm using the following in my JSP:

<% 

    for(int count=0;count<eqArray.size();count++){ %>
            <option value="<%=eqArray.get(count)%>"><%=eqArray.get(count)%></option>  
     <%} 
%>

I have an eqName String which is part of eqArray and should be the selected value by default.

How do I go about it without having to check and set the first option as eqName always?

like image 790
Chillax Avatar asked Dec 17 '22 01:12

Chillax


2 Answers

<% for(int count=0; count<eqArray.size(); count++){ %>
    <option value="<%= eqArray.get(count) %>" <%= (eqArray.get(count).equals("eqName"))?"selected":"" %> ><%= eqArray.get(count) %></option>  
<%} %>
like image 121
Katerina Mpagouli Avatar answered Dec 31 '22 04:12

Katerina Mpagouli


Change the index of eqName element to 0 in the array, or use a conditional Statement.

<% 
for(int count=0; count < eqArray.size(); count++){ %>
        <%if(eqArray.equals("eqName"){ %>           
            <option  selected="selected" value="<%=eqArray.get(count)%>"><%=eqArray.get(count)%></option>  
        <%} %> 
        <option value="<%=eqArray.get(count)%>"><%=eqArray.get(count)%></option>  
 <%} %>

but use JSTL taglibs instead of using scriptlets.

like image 39
vinodn Avatar answered Dec 31 '22 02:12

vinodn