使用JSTL下载JSP的选定值 [英] Selected value for JSP drop down using JSTL
问题描述
SortedMap< String,String> ; dept = findDepartment();
request.setAttribute(dept,dept);
和JSP
< select name =department>
< c:forEach var =itemitems =$ {dept}>
< option value =$ {item.key}> $ {item.value}< / option>
< / c:forEach>
< / select>
我正在使用一个JSP页面进行插入和更新。当我编辑页面时,如何设置选定的值以将数据库中的所选值设置为下拉。
任何帮助都非常可观。
谢谢
在HTML中,选择的选项由选择
属性< option>
元素如下:
< option ... selected> ...< / option>
或者如果你是HTML / XHTML strict:
< option ... selected =selected> ...< / option>
因此,您只需要让JSP / EL有条件地打印。如果您已准备好所选部门,请按如下所示:
request.setAttribute(selectedDept,selectedDept);
那么这应该做:
< select name =department>
< c:forEach var =itemitems =$ {dept}>
< option value =$ {item.key}$ {item.key == selectedDept? 'selected =selected':''}> $ {item.value}< / option>
< / c:forEach>
< / select>
另请参见:
I have SortedMap in Servlet to populate drop down values in JSP and I have the following code
SortedMap<String, String> dept = findDepartment();
request.setAttribute("dept ", dept);
and in JSP
<select name="department">
<c:forEach var="item" items="${dept}">
<option value="${item.key}">${item.value}</option>
</c:forEach>
</select>
I am using one JSP page for insert and update. When I am editing the page how can I set selected value to drop down where selected value will come from database.
Any help is highly appreciable.
Thanks
In HTML, the selected option is represented by the presence of the selected
attribute on the <option>
element like so:
<option ... selected>...</option>
Or if you're HTML/XHTML strict:
<option ... selected="selected">...</option>
Thus, you just have to let JSP/EL print it conditionally. Provided that you've prepared the selected department as follows:
request.setAttribute("selectedDept", selectedDept);
then this should do:
<select name="department">
<c:forEach var="item" items="${dept}">
<option value="${item.key}" ${item.key == selectedDept ? 'selected="selected"' : ''}>${item.value}</option>
</c:forEach>
</select>
See also:
这篇关于使用JSTL下载JSP的选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!