第一种:在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>
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)