如何在下拉列表中将默认值设置为空白以及项目标记

时间:2016-12-29 06:26:23

标签: jsp

我希望在下拉列表中的页面加载时默认值为空白 这是我的jsp代码:

    <div class="ukjentFodselDato ${saksdetaljerForm.sak.personUnderVerge.ukjentFodselsDato ? '':'hidden'}">
        <label class="boldText"><spring:message code="common.label.fodsel.ar"/></label> 
        <input type="hidden" value="${sak.personUnderVerge.fodselAr}" />
       <form:select path="sak.personUnderVerge.fodselAr">
         <form:options items="${yearList}" />
       </form:select>
    </div>

java代码:

     public static List<Integer> getYears(int range) {
    int year = LocalDate.now().getYear();
    List<Integer> years = new ArrayList<>();
    for(int counter=0; counter < range; counter++) {
        years.add(year);
        year = year-1;
    }
    return years;
  }

1 个答案:

答案 0 :(得分:0)

   <div class="ukjentFodselDato ${saksdetaljerForm.sak.personUnderVerge.ukjentFodselsDato ? '':'hidden'}">
        <label class="boldText"><spring:message code="common.label.fodsel.ar"/></label> 
        <input type="hidden" value="${sak.personUnderVerge.fodselAr}" />
     <form:select path="sak.personUnderVerge.fodselAr">
       <form:option value="" selected="${sak.personUnderVerge.fodselAr == null ? 'selected' : ''}" />
       <c:forEach var="year" items="${yearList}">
        <form:option value="${year}" selected="${parorende.fodselAr == year ? 'selected' : ''}" >${year}</form:option>
       </c:forEach>
   </form:select>
 </div>