只选择年份的下拉菜单

只选择年份的下拉菜单

第一种:在jsp页面中写入java代码,这是网上搜到的,不过感觉不是很好。代码如下:

     <select name="year1" style="width: 90px;">
     <option value="">--- 请选择 ---</option> 
              <% 
                     StringBuffer bufYear = new StringBuffer(); 
                     //下拉列表的年数 
                     for(int i=0;i<11;i++){ 
                     //最小年 
                     int iYear = 2005+i; 
                     bufYear.append("<option value = '"+iYear+"'"); 
                     Date date = new Date(); 
                     int sYear = date.getYear(); 
                     //系统时间从1900年开始 
                     int sYearc = sYear+1900;  
                     if(iYear == sYearc){  
                          bufYear.append(" selected");  
                      } 
                     bufYear.append(" >"+iYear+"</option>\n"); 
                     } 
                     out.println(bufYear.toString()); 
               %> 
      </select>- 
     <select name='year2' style="width: 90px;"> 
       <option value="">--- 请选择 ---</option>
              <% 
                     StringBuffer bufYear2 = new StringBuffer(); 
                     //下拉列表的年数 
                     for(int i=0;i<11;i++){ 
                     //最小年 
                     int iYear2 = 2005+i; 
                     bufYear2.append("<option value = '"+iYear2+"'"); 
                     Date date = new Date(); 
                     int sYear2 = date.getYear(); 
                     //系统时间从1900年开始 
                     int sYearc2 = sYear2+1900;  
                      if(iYear2 == sYearc2){  
                          bufYear2.append(" selected");  
                      } 
                     bufYear2.append(" >"+iYear2+"</option>\n"); 
                     } 
                     out.println(bufYear2.toString()); 
               %> 
      </select>  

第二种,本人采用过,就是借用jsp自带的foreach标签。代码如下:

其中集合years需要自己定义,本人是在action中定义赋值后放到actioncontent中的。

Action代码:ActionContent.getContent.put("years",years);

jsp代码:

     <select id="year1" name="year1" style="width:90px;">
     <option value="">--- 请选择 ---</option>
               <c:forEach var="per" items="${years}">
       <option <c:if test="${per==year1}">selected="selected"</c:if>>${per}</option>
      </c:forEach>
     </select>-   
     <select id="year2" name="year2" style="width:90px;">
     <option value="">--- 请选择 ---</option>
               <c:forEach var="per" items="${years}" >
       <option <c:if test="${per==year2}">selected="selected"</c:if>>${per}</option>
      </c:forEach>
     </select>

第三种:本人采用的s标签中的iterater标签。以选取办公室地点为例:

action代码:ActionContent.getContent.put("roomList",roomList);

jsp代码:

<select id="place" name="place" style="width:172px" class="required">
   <option value="">-- 请选择 --</option>
   <s:iterator id="tc" value="#roomList">
     <option value='<s:property value="#tc.name" />'
      <s:if test="%{#tc.name == model.place}">selected="selected"</s:if>>
      <s:property value="#tc.name" />
     </option>
      </s:iterator>
  </select>

posted on 2014-08-15 14:51  凯撒帝  阅读(677)  评论(0编辑  收藏  举报

导航