使用SpringMVC的JQuery完整日历。。Can';t调用控制器以获取JSON对象
我试图将JQuery与SpringMVC+Freemarker一起使用 我做了一个类似的演示 目标:我需要调用控制器来获取包含要在日历上呈现的事件的JSON对象 问题: 我有下面的freemarker,它应该转到控制器并获取要渲染的JSON对象,但它没有去 免费标记: [#ftl/] 控制员:使用SpringMVC的JQuery完整日历。。Can';t调用控制器以获取JSON对象,jquery,json,spring-mvc,fullcalendar,Jquery,Json,Spring Mvc,Fullcalendar,我试图将JQuery与SpringMVC+Freemarker一起使用 我做了一个类似的演示 目标:我需要调用控制器来获取包含要在日历上呈现的事件的JSON对象 问题: 我有下面的freemarker,它应该转到控制器并获取要渲染的JSON对象,但它没有去 免费标记: [#ftl/] 控制员: @RequestMapping(value = "/vacation/getVacation", method = RequestMethod.GET) public @ResponseBo
@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();
}
}
@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;
}
@RequestMapping(value=“/vacation/getVacation”,method=RequestMethod.GET)
public@ResponseBody void getVacation(HttpServletResponse){
Map Map=newhashmap();
地图放置(“id”,111);
地图放置(“标题”、“事件1”);
地图放置(“开始”,“2011-07-28”);
map.put(“url”http://yahoo.com/");
//转换为JSON字符串。
字符串json=new Gson().toJson(map);
//编写JSON字符串。
setContentType(“应用程序/json”);
响应。setCharacterEncoding(“UTF-8”);
试一试{
response.getWriter().write(json);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
萤火虫射击:
我不确定这是否与:
response.getWriter().write(json);
这篇文章讨论了两者的区别,其中一个解决方案提到了与freemarker的冲突:
还可以尝试指定ajax调用中预期的数据类型:
$(document).ready(function () {
var calendar = $('#calendar').fullCalendar({
editable: true,
eventSources: [{
// your event source
url: '[@spring.url '/vacation/getVacation'/]', // I was expecting here to call the controller,but nothing is happened !!
type: 'GET',
data: {
start: 'start',
id: 'id',
title: 'title,'
},
error: function () {
alert('there was an error while fetching events!');
},
color: 'yellow',
textColor: 'black',
dataType: 'json'
}])
};)
};
正如建议的那样,我还将在调试模式下运行应用程序,在控制器中设置调试点,以查看代码是否被命中。如果不是,请使用firebug分析请求中使用的url
检查spring用于设置dispatcher的配置文件。通常,在此配置中注册InternalResourceViewResolver。以下是我的一个例子:
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
致:
我不熟悉@spring.url,但在我看来,目前它弊大于利 最后,我成功了:)
我已经使用$.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控制器:
@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();
}
}
@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;
}
@RequestMapping(value=“/vacation/getVacation”,method=RequestMethod.GET)
公众的
@应答器
字符串getVacation(HttpServletResponse){
Map Map=newhashmap();
地图放置(“id”,111);
地图放置(“标题”、“事件1”);
地图放置(“开始”,“2012-4-15”);
map.put(“url”http://yahoo.com/");
//转换为JSON字符串。
字符串json=new Gson().toJson(map);
//编写JSON字符串。
setContentType(“应用程序/json”);
响应。setCharacterEncoding(“UTF-8”);
返回json;
}
您可以使用firebug查看请求使用的url吗?另外,如果在控制器中设置调试点,代码是否会被命中?您好,谢谢您的回复。我已经尝试了您告诉我的提示。但是,我仍然有相同的问题。控制器上的断点没有被调用。当我使用firebug时,url看起来很好,这里是firbug视图:eventSources:[{//您的事件源url:'/springway/vacation/getVacation',键入:'GET',数据:{start:'start',id:'id',title:'title',}所以我在这里遗漏了什么!!当我在控制器上更改请求类型并将其设置为'GET时,然后浏览到“”,我得到:{“id”:111,“title”:“event1”,“start”:“2011-07-28”,“url”:“}。所以现在我确信url是好的,但为什么它没有被调用在第一个地方!!你有控制台(Eclipse)吗当你调试它时打开它?它应该说像resource not found之类的东西或者类似的东西。复制它并把它放在你的答案中,同时在中截取目录结构的屏幕截图(我假设)eclipse或sts可以帮助我们帮助您。您的spring配置中是否有InternalViewResolver设置?关于firebug控制台,我没有错误。关于控制器,我没有在IDE的控制台上报告,因为操作尚未发送到控制器。是的,我已经在我的配置中安装了freemarker视图resolver。太棒了,非常高兴你明白了。
url: '[@spring.url '/vacation/getVacation'/]',
url: '/vacation/getVacation',
$(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]
});
});
});
@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;
}