JSP自定义标记,用于显示大型事件类型日历

时间:2010-11-02 20:59:34

标签: java spring jsp spring-mvc jodatime

这可能是一个简单的问题,但是每个人都使用什么来在jsp中显示简单的事件日历。我正在寻找一个现有的taglib但除了calendarTag之外什么都没找到。

我正在使用Spring MVC 3.0,JodaTime。并且基本上只是想在jsp上有一个大日历,我可以根据某些逻辑包含指向另一个寻呼机的链接。没什么特别的。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我会在控制器中创建日历,例如:

@Controller
public class CalendarController{

    private static final int START_OF_WEEK = DateTimeConstants.MONDAY;

    @RequestMapping("drawCalendar")
    public ModelAndView drawCalendar(@RequestParam("month") final int month,
        @RequestParam("year") final int year){
        final LocalDate monthStart = new LocalDate(year, month, 1);
        boolean endReached = false;
        LocalDate today = null;
        final List<List<LocalDate>> weeks1 = new ArrayList<List<LocalDate>>();
        while(!endReached){
            final List<LocalDate> thisWeek = new ArrayList<LocalDate>();
            for(int weekOffset = 0; weekOffset < 7; weekOffset++){
                if(today == null){
                    if(!endReached
                        && weekOffset == monthStart.getDayOfWeek()
                            - START_OF_WEEK){
                        today = monthStart;
                    }
                } else{
                    today = today.plusDays(1);
                    if(today.getMonthOfYear() != month){
                        today = null;
                        endReached = true;
                    }
                }
                thisWeek.add(today);
            }
            weeks1.add(thisWeek);
        }
        final List<List<LocalDate>> weeks = weeks1;

        final Map<String, Object> model = new HashMap<String, Object>();
        model.put("calendar", weeks);
        final ModelAndView modelAndView = new ModelAndView("calendar", model);
        return modelAndView;
    }
}

在JSP中我会迭代几周:

<c:forEach items="${calendar}" var="week">
    <tr>
        <td>Mon</td>
        <td>Tue</td>
        <td>Wed</td>
        <td>Thu</td>
        <td>Fri</td>
        <td>Sat</td>
        <td>Sun</td>
    </tr>
    <tr>
        <c:forEach items="${week}" var="date">
            <td><c:out value="${date==null ? '' : date.dayOfWeek}"</td>
        </c:forEach>
    </tr>
</c:forEach>

(当然我在大约3年内没有编写任何JSP代码,如果出现问题就很抱歉)