Full Calendar Struts2 JSON

风格不统一 提交于 2019-12-08 04:16:05

问题


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


回答1:


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 ENDparams 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>


来源:https://stackoverflow.com/questions/24001455/full-calendar-struts2-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!