完整日历Struts2 JSON [英] Full Calendar Struts2 JSON
问题描述
我无法弄清楚如何从JSON中获取数据到我的Fullcalendar jquery函数iive已经阅读过文档,但是他们展示的唯一一个例子对我来说并不明确
this是我的代码
< script>
$(document).ready(function(){
$('#calendar')。fullCalendar({
theme:true,
header:{
left :'prev,next today',
center:'title',
right:'month,agendaWeek'
},
editable:true,
events:' jsone.action'
});
});
只有3个值用于测试目的
@ParentPackage(json-default)
@Action(value =jsone ,results = {
@Result(type =json,name =success),})
public String title;
public String start;
public String end;
public String execute(){
title =Event;
start =2014-01-12T10:30:00-05:00;
end =2014-01-12T12:30:00-05:00;
System.out.println(execute);
返回SUCCESS;
}
// setter getters
当我调用jsone动作我得到这个,所以我认为这是正确的格式,即使当你加载日历我没有得到任何错误消息和jsone行动工程并正确执行
{ 端: 2014-01-12T12:30:00-05:00, 启动: 2014-01-12T10:30:00-05:00, 标题: 事件}
注意id与同样问题的人阅读这篇文章
< a href =https://stackoverflow.com/questions/17459184/implementing-fullcalendar-with-struts-2> https://stackoverflow.com/questions/17459184/implementing-fullcalendar-with-struts-2
现在我知道如何正确获取数据,所以我想发布解决方案。
在这个例子中,我将只使用一个事件。
首先在动作中使用注释
@ParentPackage(json-default)
@Action(value =jsone,results = {
@Result(type =json,name =success),})
现在里面的动作(java class)
public String title;
public String start;
public String end;
public String execute(){
title =Event;
start =2014-06-12T10:30:00-05:00;
end =2014-06-12T12:30:00-05:00;
返回SUCCESS;
}
// GETTERS SETTERS
你的事件应该看起来像这样就像在你的JSP中一样
$ p $
$ .ajax({
url:'jsone.action',
dataType:'json',
数据:{
start:start.unix(),
end:end.unix ()
},
success:function(doc){
var events = [];
events.push({
title:doc.title,
start:doc.start,
end:doc.end
});
callback(events);
}
});
现在.. START
和 END
CALLBACK
param用于返回事件数组,然后成功函数(doc)
doc
param是事件从Action获取,以便您可以轻松访问您的动作属性(在我的动作示例中,我使用'标题','开始'和'结束'名称,但您可以使用任何不同的名称)也很重要,您知道您的fullcalendar版本因为新的BETA版本(2.x)使用时刻而不是通常的1.x版本使用的数据。 现在您必须使用这种格式
start =2014-06-12T10:30:00-05: 00\" ;
end =2014-06-12T12:30:00-05:00;
重要提示:此示例使用Struts2 JSON-Pluginso @ParentPackage(json-default)需要使用这个插件,你可以使用maven
< dependency>
< groupId> org.apache.struts< / groupId>
< artifactId> struts2-json-plugin< / artifactId>
< version> 2.3.16.3< / version>
< /依赖关系>
I cant figure out how to fetch data from JSON to my Fullcalendar Jquery function ive had read the documentation but the only examnple they show is not clear for me
this is my code
<script>
$(document).ready(function() {
$('#calendar').fullCalendar({
theme: true,
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek'
},
editable: true,
events: 'jsone.action'
});
});
and in my action i return only 3 values for test purposes
@ParentPackage("json-default")
@Action(value = "jsone", results = {
@Result(type = "json", name = "success"),})
public String title;
public String start;
public String end;
public String execute() {
title="Event";
start="2014-01-12T10:30:00-05:00";
end="2014-01-12T12:30:00-05:00";
System.out.println("execute");
return SUCCESS;
}
//setters getters
when i call the "jsone" action i get this so i think this is the correct format even when y load the calendar i dont get any error message and the jsone action works and executes correctly
{"end":"2014-01-12T12:30:00-05:00","start":"2014-01-12T10:30:00-05:00","title":"event"}
Note id read this post with a guy with the same problem https://stackoverflow.com/questions/17459184/implementing-fullcalendar-with-struts-2
Now i know how to fetch data correctly so i want to post the solution. for this example i will use only one event.
First in the action (using annotations)
@ParentPackage("json-default")
@Action(value = "jsone", results = {
@Result(type = "json", name = "success"),})
Now inside the action (java class)
public String title;
public String start;
public String end;
public String execute() {
title="Event";
start="2014-06-12T10:30:00-05:00";
end="2014-06-12T12:30:00-05:00";
return SUCCESS;
}
//GETTERS SETTERS
Tis is the way your events should look like in your JSP
events: function(start, end, timezone, callback) {
$.ajax({
url: 'jsone.action',
dataType: 'json',
data: {
start: start.unix(),
end: end.unix()
},
success: function(doc) {
var events = [];
events.push({
title: doc.title,
start: doc.start,
end: doc.end
});
callback(events);
}
});
}
Now.. the START
and END
params are used to only return a certain amount of events and not the whole event list, the CALLBACK
param is used to return an array of events and then in the success function(doc)
the doc
param is the event fetched from the Action so you can easily access to your action attributes (in my action example ive used 'title','start' and 'end' names but you can use any different) also is important that you know what fullcalendar version you are using because the new BETA version (2.x) uses moment instead the usual Data that 1.x version uses.
Now you have to use this format
start="2014-06-12T10:30:00-05:00";
end="2014-06-12T12:30:00-05:00";
IMPORTANT: this example uses "Struts2 JSON-Plugin" so @ParentPackage("json-default") is needed in order to use this plugin, you can use it with maven
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-json-plugin</artifactId>
<version>2.3.16.3</version>
</dependency>
这篇关于完整日历Struts2 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!