使用JSTL填充选择标签中的选定项目? [英] Selected item populating in Select tag using JSTL?

查看:149
本文介绍了使用JSTL填充选择标签中的选定项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < select name =birthday_monthid = birthday_month > 
< option value = - 1> Month< / option>
< option value =1> Jan< / option>
< option value =2> Feb< / option>
...
< / select>

JSP中的输出代码使用我正在使用的JSTL来显示先前选择的项目(这是不正确的)

 < select name =birthday_monthid =birthday_month> 
< c:forEach var =valueitems =$ {birthdaymonth}>
< option value =$ {birthdaymonth}> $ {birthdaymonth}< / option>
< option value =1> Jan< / option>
< option value =2> Feb< / option>
...
< / c:forEach>
< / select>

我从这段代码中得到的值是select 1或2标签



其他信息:


  1. 我存储生日月份为1,2,3 ..为1月,2月,3月...在数据库中

  2. 我在Servlet中的请求范围内使用

    request.setAttribute(birthdaymonth,user.getBirthdayMonth());

我期望的是


  1. 当我显示以后的JSP时,应该将以前存储的生日月份显示为Jan,Feb,Mar,而不是1,2,3并显示其他选项值,包括所选项目为高亮显示。


解决方案

要动态迭代几个月的集合,您想要存储几个月在 Map< Integer,String> 中,键是月份号,值是月份名称。要默认选择HTML < option> 元素,您需要设置选定的属性。



所以,假设你有一个 Map< Integer,String>月份和一个整数s​​electMonth ,那么以下应该做到:

 < select name =birthday_month> 
< c:forEach items =$ {months}var =month>
< option value =$ {month.key}$ {month.key == selectedMonth? 'selected':''}> $ {month.value}< / option>
< / c:forEach>
< / select>

条件运算符?:将打印选择 selectedMonth 等于当前迭代的月份号。


I store Birthday Month in database as value using following code in JSP.

<select name="birthday_month" id="birthday_month">
  <option value="-1">Month</option>
  <option value="1">Jan</option>
  <option value="2">Feb</option>
  ...
</select>

Output code in JSP to show previously selected item using JSTL that I am using (which is not correct)

<select name="birthday_month" id="birthday_month">
  <c:forEach var="value" items="${birthdaymonth}">
    <option value="${birthdaymonth}">${birthdaymonth}</option>
    <option value="1">Jan</option>
    <option value="2">Feb</option>
    ...
  </c:forEach>
</select>

What I am getting from this code is value like 1 or 2 in select tag

Other Information:

  1. I store birthday month as value like 1,2,3.. for Jan,Feb,Mar... in Database
  2. I bring value of birthday month in request scope in Servlet using
    request.setAttribute("birthdaymonth", user.getBirthdayMonth());

What i was expecting

  1. When i show later JSP it should show previously stored birthday month as Jan,Feb, Mar and not 1,2,3 and also show other Option values including selected item as highlighted.

解决方案

To dynamically iterate over a collection of months, you'd like to store the months in a Map<Integer, String> where the key is the month number and the value is the month name. To make a HTML <option> element by default selected, you need to set the selected attribute.

So, assuming that you have a Map<Integer, String> months and a Integer selectedMonth in the scope, then the following should do:

<select name="birthday_month">
    <c:forEach items="${months}" var="month">
        <option value="${month.key}" ${month.key == selectedMonth ? 'selected' : ''}>${month.value}</option>
    </c:forEach>
</select>

The conditional operator ?: will print selected when the selectedMonth is equal to the currently iterated month number.

这篇关于使用JSTL填充选择标签中的选定项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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