关于fullcalendar里显示json格式的events(java)

cyper的java代碼(spring mvc controller):

@RequestMapping(value = "calendarEvents")
@ResponseBody
public String calendarEvents(){
	List<LecturerAgendaVo> agendas = lecturerAgendaService.findAllMatchedAgenda();
	StringBuilder sb = new StringBuilder();
	sb.append("[");
	for(LecturerAgendaVo agenda: agendas){
		sb.append("{\"id\":" + agenda.getId());
		sb.append(",\"title\":\"\\n" + agenda.getLecterName()+"\\n");
		sb.append(agenda.getSchoolName()+"\\n");
		sb.append(agenda.getStartTime() + "-"+ agenda.getEndTime() + "\\n");
		sb.append("\"");
		sb.append(",\"start\":\""  + agenda.getLectDate() + " " + agenda.getStartTime() + ":00\"");
		sb.append(",\"end\":\""  + agenda.getLectDate() + " " + agenda.getEndTime() + ":00\"");
		sb.append("},");
	}
	sb.setLength(sb.length()-1);
	sb.append("]");
	return sb.toString();
}

cyper的javascript:

$(document).ready(function() {
	$('#calendar').fullCalendar({
		header: {
			left: 'prev,next today',
			center: 'title',
			right: 'month,basicWeek,basicDay'
		},
		editable: false,
		events:  '${ctx}/mgr/lect/apply/calendarEvents.json'
	});
	
});

以下為轉載部分:

fullcalendar 的帮助文档 可见地址:http://arshaw.com/fullcalendar/docs/


jQuery日历FullCalendar插件是一个非常不错的日历工具,可用于制作日程表或计划安排

Event Data

里有3种显示events的方式

events (as an array) 这种课见帮助
events (as a json feed) 此时
<pre class="javascript" name="code"> 
          $('#calendar').fullCalendar({
            events: {
                url: '<%=request.getContextPath()%>/displayAction.do?method=testJson' //你的controller的地址
                type: 'POST',
                error: function() {
                    alert('there was an error while fetching events!');
                },
                color:'yellow',// 背景色
                textColor:'black'// 文字颜色
            }
        });</pre>
<pre></pre>


java代码

 public ModelAndView testJson(HttpServletRequest request, HttpServletResponse response) {
        String strvalue = "[{\"id\":111,\"title\":\"Event1\",\"start\":\"2012-03-10\",\"url\":\"http:\\/\\/yahoo.com\\/\"},{\"id\":222,\"title\":\"Event2\",\"start\":\"2012-03-20\",\"end\":\"2012-03-22\",\"url\":\"http:\\/\\/yahoo.com\\/\"}]";
        response.setCharacterEncoding("UTF-8");
        System.out.println("strvalue="+strvalue);
        try {
            response.getWriter().print(strvalue);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
3 events (as a function)
              $('#calendar').fullCalendar({
                events:function(start, end, timezone, callback) {
                    $.ajax({
                        url:"<%=request.getContextPath()%>/displayAction.do?method=getTitle",
                        cache:false,
                        success:function(doc) {
                            eval("var j=" + doc);
                            var events = [];
                            var info = j.eventinfo;
                            for (var i = 0; i < info.length; i++) {
                                var ev = info[i];
                                var title = ev.title;
                                var evtstart = new Date(Date.parse(ev.start));
                                var evtend = new Date(Date.parse(ev.end));
                                events.push({
                                    title:title,
                                    start:evtstart,
                                    end:evtend,
                                    id:1
                                });
                            }
                            callback(events);
                        },
                        error:function() {
                            alert('sdf')
                        }
                    })
                }
            })


java代码

public ModelAndView getTitle(HttpServletRequest request, HttpServletResponse response) {
        String strvalue = "{ 'eventinfo':[{day: '3/3/2012',eventtitle:'test1'},{day: '3/8/2012',eventtitle:'test2'} ]} ";
        response.setCharacterEncoding("UTF-8");
        System.out.println("strvalue="+strvalue);
        try {
            response.getWriter().print(strvalue);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

你可能感兴趣的:(jquery,fullCalendar)