gpt4 book ai didi

JQuery Full Calendar with Spring MVC ..无法调用 Controller 来获取JSON对象

转载 作者:行者123 更新时间:2023-12-01 01:17:47 27 4
gpt4 key购买 nike

我正在尝试使用 JQuery Full Calendar以及 Spring MVC + Freemarker。

我做了一个类似 that 的演示.

目标:我需要调用 Controller 来获取包含要在日历上呈现的事件的 JSON 对象。

问题:我有以下 freemarker,它应该转到 Controller 并获取要渲染的 JSON 对象,但它没有去?!!

自由标记:

[#ftl/]

<script type="text/javascript">
$(document).ready(function() {

var date = new Date();
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();

var calendar = $('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
selectable: true,
selectHelper: true,
select: function(start, end, allDay) {
var title = prompt('Event Title:');
if (title) {
calendar.fullCalendar('renderEvent',
{
title: title,
start: start,
end: end,
allDay: allDay
},
true // make the event "stick"
);
}
calendar.fullCalendar('unselect');
},
editable: true,
events: [
{
url: '[@spring.url '/vacation/getVacation'/]',
type: 'GET',

data: {
start: 'start',
id: 'id',
title: 'title'
}

}
]
});

});

body {
margin-top: 40px;
text-align: center;
font-size: 14px;
font-family: "Lucida Grande", Helvetica, Arial, Verdana, sans-serif;
}

#calendar {
width: 900px;
margin: 0 auto;
}

Controller :

@RequestMapping(value = "/vacation/getVacation", method = RequestMethod.GET)
public @ResponseBody void getVacation(HttpServletResponse response) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", 111);
map.put("title", "event1");
map.put("start", "2011-07-28");
map.put("url", "http://yahoo.com/");

// Convert to JSON string.
String json = new Gson().toJson(map);


// Write JSON string.
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
try {
response.getWriter().write(json);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Firebug 射击: enter image description here

最佳答案

终于,我成功了:)我使用 $.getJSON 来获取 json 对象。

自由标记:

   $(document).ready(function() {
var date = new Date();
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();
$.getJSON('[@spring.url '/vacation/getVacation'/]', function (data) {
var calendar = $('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
selectable: true,
selectHelper: true,
select: function(start, end, allDay) {
var title = prompt('Event Title:');
if (title) {
calendar.fullCalendar('renderEvent',
{
title: title,
start: start,
end: end,
allDay: allDay
},
true // make the event "stick"
);
}
calendar.fullCalendar('unselect');
},
editable: true,
events:[data]
});
});
});

Java Controller :

@RequestMapping(value = "/vacation/getVacation", method = RequestMethod.GET)
public
@ResponseBody
String getVacation(HttpServletResponse response) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", 111);
map.put("title", "event1");
map.put("start", "2012-4-15");
map.put("url", "http://yahoo.com/");

// Convert to JSON string.
String json = new Gson().toJson(map);

// Write JSON string.
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");

return json;
}

关于JQuery Full Calendar with Spring MVC ..无法调用 Controller 来获取JSON对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10154113/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com