The usage of Calendar taglib: Next/Prev buttons
This file contains a sample for Calendar taglib. Here we are demonstrating a simple use case for the Next/Prev buttons. For Calendar taglib you can describe buttons for Next/Prev month. Actually, it is a generic tool so you can provide your own links for getting to next (previous) month. And of course, you can have your own actions for these buttons. We are describing the simplest use case: you are going to use the same page for your calendar and hit for next (previous) button should simply output the calendar for the next (previous) month. Suppose, our main file is calendar.jsp and now is March 2004. By default tag setNextMonth (see Calendar taglib description) will create the following link for our Next button: calendar.jsp?month=4&year=2004 (next month is April). And for our Prev button tag setPrevMonth by default (with empty body) will create the following link: calendar.jsp?month=2&year=2004 (prev month is February). So by default both tag are using the exactly the same URI (it is calendar.jsp in our example).
So now in our calendar.jsp file we can simply check out parameter month (and year) for getting the actual value for calendar's
month (year). If this parameter is empty we will output our calendar for the current month (year). It means that user simply run
calendar.jsp file. Otherwise this parameter will keep a value for month (year) we need to use. It means user hits Prev (Next) button and
query string contains this parameter. And do not forget that Java counts month as 0-11. So it is our code:
<%@ taglib uri="taglib.tld" prefix="cal" %>
int thisMonth, thisYear;
java.util.Calendar c = java.util.Calendar.getInstance();
if ((s = request.getParameter("month"))==null)
thisMonth = c.get(java.util.Calendar.MONTH)+1;
thisMonth = Integer.parseInt(s);
if ((s = request.getParameter("year"))==null)
thisYear = c.get(java.util.Calendar.YEAR);
thisYear = Integer.parseInt(s);
<cal:Calendar month="<%=thisMonth%>" year="<%=thisYear%>" header="true">
© Coldbeans Comments?
See also Other tips from Coldbeans.