How to integrate jquery calendar with jsp

I have a problem loading event data from mysql to jquery fullcalendar. An example is given in php and I don't know how to do it java .. this is an example code:

 111, 'title' => "Event1", 'start' => "$ year- $ month-10", 'url' => "http://yahoo.com/"))); ? >
+1
source share
3 answers

For this you need to create Servlet. Create a class that you extends HttpServletwrite code in doGet()accordingly so that it writes the required JSON string to the response. You can use Google Gson to convert Java objects to a JSON string.

For instance:

// Gather data.
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", 111);
map.put("title", "event1");
map.put("start", new SimpleDateFormat("yyyy-MM-10").format(new Date()));
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");
response.getWriter().write(json);

Then just hover this servlet in web.xmlto the desired one url-pattern.

Instead, Mapyou can even create your own Javabean class Event:

public class Event {
    private Long id;
    private String title;
    private Date start;
    private URL url;
    // Add/generate getters/setters.
}

You can even use Gson to convert it:

Event event = eventDAO.find(request.getParameter("id"));
String json = new Gson().toJson(event);

This way you can easily collect all of them in List<Event>, which is preferable to List<Map<String, String>>:

List<Event> events = eventDAO.list();
String json = new Gson().toJson(events);
+4
source

In your servlet install this script:

map.put("id", 111);
map.put("title", "event1");
map.put("start", new SimpleDateFormat("yyyy-MM-10").format(new Date()));
map.put("url", "http://yahoo.com/");

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

// Put json between [] to be formatted by Fullcalendar
json = "[" + json + "]";

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

jQuery - $.ajax(). . :

$.ajax({
          url: 'app',
          dataType: "json",
          success: function(response) {
              $('#calendar').fullCalendar({
                  header: {
                        left: 'prev,next today',
                        center: 'title',
                        right: 'month,agendaWeek,agendaDay'
                    },
                    editable: true,
                    events: [response]
                });
          }
        });

,   

0

Source: https://habr.com/ru/post/1736235/


All Articles