Creating a normal calendar

Creating a normal calendar is simple. However there are three main ways you may do it so I will go over them below.

Using specific values for the month, date and year.

<calendartag:calendar month="9" day="19" year="2004" />

This will create a calendar which begins in September of 2004 with the 19th day selected. By default it will provide the viewer to move forward and backwards in time in 1 month increments.

Using an object to define the calendar.

<calendartag:calendar date="$someDateObject}" />

This will create a calendar that will display the month, day and year of someDateObject. It is recommended you generate this object in your action and place it into the request scope. By default it will provide the viewer to move forward and backwards in time in 1 month increments.

Using date objects to define both the range and selected dates.

<calendartag:calendar date="$someDateObject}" startDate="${startDate}" endDate="${endDate}" />

This will create a calendar beginning at startDate and ending at endDate. Assuming the someDateObject is within the bounds it will be the date which is selected. The viewer will be able to move forward and backwards based on the interval of days visible. This is particularily useful if you want to show Monday-Sunday calendar's or any other custom date range. Try it out.

Creating a calendar with content (events)

CalendarTag provides a CalendarDecorator [code] interface and DefaultCalendarDecorator [code] for customizing the contents and design of your calendar.

If you want to populate the inside of your calendar you must either implement CalendarDecorator or extend DefaultCalendarDecorator. Here is how it might look.

Inside the jsp we need to override the decorator with our own custom implementation. We will also want to set width and height to avoid a very ugly looking calendar.

<calendartag:calendar decorator="YourCalendarDecorator" dayWidth="80" dayHeight="80" />

Inside your action (or whatever you prepare your view with) you will most likely build a collection of event objects of your liking and place it in the request. Later, in the decorator we will retrieve it and display them in their appropriate places.

...
Collection events = yourPersistence.getEvents();
request.setAttribute(events);
...

Inside YourCalendarDecorator you will want to override getDay(String) to provide your custom day contents to the calendar.

public class YourCalendarDecorator implements DefaultCalendarDecorator {
      
      public String getDay(String url) {
          StringBuffer content = new StringBuffer();
          content.append(super.getDay(url) + "<br />");
          
          // retrieve the collection of objects you put into the request in the action
          pageContext.getRequest().getAttribute("Events");
          
          // do some logic over the events to determine what happened on this day
          // you can use calendar or what setCalendar sends to determine the currently selected date
          content.append("5K Run at the HAWC<br />");
          
          return content.toString();
      }
      
}