This is my code:
<form>
<table>
<tr>
<td>Month</td>
<td>
<select name="">
<option value="">January</option>
<option value="">February</option>
<option value="">March</option>
<option value="">April</option>
<option value="">May</option>
<option value="">June</option>
<option value="">July</option>
<option value="">August</option>
<option value="">September</option>
<option value="">October</option>
<option value="">November</option>
<option value="">December</option>
</select>
</td>
</tr>
<tr>
<td>Year</td>
<td><input type="text" name="year"></td>
</tr>
<tr>
<td><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
I want to put the month values in the option value. Let say that 2013-07-16 is the date with the date format of yyyy-mm-dd in SimpleDateFormat. How can I put the value of July or "07" in my option value with this kind of format.
<fmt:format var="month" value="${theDate}" pattern="MM"/>
<select name="">
<option value="01" ${month == "01" ? "selected='selected'" : ""}>January</option>
<option value="02" ${month == "02" ? "selected='selected'" : ""}>February</option>
...
</select>
There is a lot of repetition, though, in this code, so you'd better prepare a List<SelectableMonth>
in your controller, and iterate over this list:
<select name="">
<c:forEach var="month" items="${selectableMonths}">
<option value="${month.number}" ${month.selected ? "selected='selected'" : ""}>${month.label}</option>
</c:forEach>
</select>
I haven't written any JSP in years but I think this should point you in the right direction:
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c-rt" %>
<%!
String[] months = { "January","February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
%>
<form>
<table>
<tr>
<td>Month</td>
<td>
<select name="">
<c-rt:forEach var="month" items="<%= months %>">
<option value="${i}">${months[i]}</option>
</c-rt:forEach>
</select>
</td>
</tr>
<tr>
<td>Year</td>
<td><input type="text" name="year"></td>
</tr>
<tr>
<td><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
Let say that 2013-07-16 is the date with the date format of yyyy-mm-dd in SimpleDateFormat. How can I put the value of July or "07" in my option value with this kind of format.
Your question is a little unclear, but it sounds like you're asking if you have a date value modelled by a String
, and a SimpleDateFormat
that matches the pattern of that value, how can you modify the format to output it in a new way? Is that what you're looking for?
If that is what you're asking, you'd need twwo more instances of SimpleDateFormat
to model the new formats you want to use — one to model the month name and other for its numeric value. You could then use the parse(String source)
method to turn your String
into a Date
& reformat it from there. If you want to take this approach, something like the below should work.
Be careful of throwing a ParseException
if taking this approach though. You'd be better to start from a Date
instead, if possible.
<%
String today = "2013-07-16";
SimpleDateFormat yearMonthDay = new SimpleDateFormat("yyyy-MM-dd");
Date todaysDate = yearMonthDay.parse(today);
String monthAsNumber = new SimpleDateFormat("MM").format(todaysDate);
String monthAsFullName = new SimpleDateFormat("MMMMM").format(todaysDate);
%>
<select>
<option value="<%= monthAsNumber %>"><%= monthAsFullName %></option>
</select>
Also, it would be best practice to remove this type of Java code from the JSP altogether for testability, re-use etc — You should look into using JSTL functions, that will allow you to move the data parsing out into a separate class & then use a bean to render out the two values you need to display per date.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.