使用JSTL下载JSP的选定值 [英] Selected value for JSP drop down using JSTL

查看:112
本文介绍了使用JSTL下载JSP的选定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Servlet中有SortedMap来填充JSP中的下拉列表值,我有以下代码

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆