如何将jquery日历与jsp集成

发布于 2024-08-15 17:19:01 字数 258 浏览 2 评论 0 原文

我在将事件数据从 mysql 加载到 jquery fullcalendar 时遇到问题。给出的示例是在 php 中,我不知道如何在 java 中执行此操作。 这是示例代码:

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

i have a problem to load the event data from mysql to jquery fullcalendar..the example given is in php and i dont know how to do it java..
this is the sample code:

111,
'title' => "Event1",
'start' => "$year-$month-10",
'url' => "http://yahoo.com/"
)

));

?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暮年 2024-08-22 17:19:01

您需要为此创建一个 Servlet。创建一个扩展 HttpServlet 的类,并相应地在 doGet() 中编写代码,以便将所需的 JSON 字符串写入响应。您可以使用 Google Gson 将 Java 对象转换为 JSON 字符串。

例如:

// 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);

然后只需在 web.xml 中将此 servlet 映射到所需的 url-pattern 上即可。

您甚至可以创建 Javabean 类 Event,而不是 Map

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

您甚至可以使用 Gson 来转换它:

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

这样您可以更轻松地将它们全部收集在 中List 优于 List>

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

You need to create a Servlet for that. Create a class which extends HttpServlet and write code in doGet() accordingly that it writes the desired JSON string to the response. You can use Google Gson to convert Java objects to a JSON string.

For example:

// 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 map this servlet in web.xml on the desired url-pattern.

Instead of a Map you could even create your Javabean class Event:

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

You could even use Gson to convert it:

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

This way you can more easy collect them all in a List<Event> which is preferable above a List<Map<String, String>>:

List<Event> events = eventDAO.list();
String json = new Gson().toJson(events);
花间憩 2024-08-22 17:19:01

在您的 servlet 中放入以下脚本:

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);

In your servlet put 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);
冷月断魂刀 2024-08-22 17:19:01

首先,您需要从 jQuery 调用 servlet - 您可以使用 $.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]
                });
          }
        });

问候,
索林

First you need to invoke the servlet from jQuery - you do this with $.ajax(). Then you need to pass the result to the calendar. The following works fine:

$.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]
                });
          }
        });

Greetings,
Sorin

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文