Note that there are some explanatory texts on larger screens.

plurals
  1. POcomparing month,year parts of util.Date in dropdown list
    primarykey
    data
    text
    <p>In my web app,a customer object has fields related to credit card info</p> <pre><code>public class Customer { ... private String ccType; private Date ccExpirationDate; ... } </code></pre> <p>In the jsp page I am providing drop down lists to select credit card type,expiration month,expiration year. I want to check if a customer exists in session and if so his credit cardtype and month,year parts of expiration date matches any of the options in drop down list,then those options are shown as selected .</p> <p>In contextInitialized() method of a class implementing ServletContextListener ,I created maps as below..</p> <pre><code>Map&lt;String,String&gt; cardtypes = new TreeMap&lt;String,String&gt;(); cardtypes.put("M0", "MasterCard"); cardtypes.put("D0", "Discover"); ... Map&lt;String,String&gt; expiryMonths = new TreeMap&lt;String,String&gt;(); expiryMonths.put("01", "January"); expiryMonths.put("02", "February"); ... Map&lt;String,String&gt; expiryYears = new TreeMap&lt;String,String&gt;(); for(int i=2011;i&lt;2030;i++){ String year = Integer.toString(i+1); expiryYears.put(year,year); } ... sc.setAttribute("ccyears", expiryYears); sc.setAttribute("ccmonths", expiryMonths); sc.setAttribute("cctypes", cardtypes); </code></pre> <p>I tried this.</p> <pre><code>&lt;tr&gt; &lt;td&gt; &lt;select id="creditCardType" title="select card type" name="creditCardType"&gt; &lt;c:forEach var="cctype" items="${cctypes }"&gt; &lt;option ${not empty customer and customer.ccType == cctype.key ? 'selected':'' } value="${cctype.key }"&gt;${cctype.value }&lt;/option&gt; &lt;/c:forEach&gt; &lt;/select&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Expiration Date&lt;/td&gt; &lt;td&gt; &lt;select id="cardexpiryMonth" name="cardexpiryMonth"&gt; &lt;c:forEach var="ccmonth" items="${ccmonths }"&gt; &lt;option ${not empty customer and customer.ccExpirationDate.month == ccmonth.key ? 'selected':'' } value="${ccmonth.key }"&gt;${ccmonth.value }&lt;/option&gt; &lt;/c:forEach&gt; &lt;/select&gt; &lt;/td&gt; &lt;td&gt; &lt;select id="cardexpiryYear" name="cardexpiryYear"&gt; &lt;c:forEach var="ccyear" items="${ccyears }"&gt; &lt;option ${not empty customer and customer.ccExpirationDate.year == ccyear.key ? 'selected':'' } value="${ccyear.key }"&gt;${ccyear.value }&lt;/option&gt; &lt;/c:forEach&gt; &lt;/select&gt; &lt;/td&gt; &lt;/tr&gt; </code></pre> <p>This works for creditcard type ,which is shown as customer's card type selected.But the customer's expiration month and year are not shown selected..Is the failure due to int-string comparison?Is there a way to correct this problem?</p> <p>(I know util.Date's getMonth(),getYear() are deprecated..but could think of no other way to do this using EL) </p> <p>any help appreciated</p> <p>thanks</p> <p>mark</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload