如何整合jQuery的日历JSP [英] how to integrate jquery calendar with jsp

查看:155
本文介绍了如何整合jQuery的日历JSP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,从MySQL加载事件数据给出的jQuery是在PHP fullcalendar..the例如,我不知道如何做到这一点的Java .. 这是样品code:

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:

 $year = date('Y');
 $month = date('m');

 echo json_encode(array(

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

 ));

?>

推荐答案

您需要创建一个的Servlet 为。创建一个类,延伸的HttpServlet 和写code。在的doGet()相应地,它写入所需的JSON字符串到响应。您可以使用谷歌GSON 的转换Java对象为JSON字符串。

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.

例如:

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

然后就映射这个servlet的的web.xml 所需的 URL模式

而不是一个地图,你甚至可以创建你的JavaBean类事件

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.
}

您甚至可以使用GSON将其转换:

You could even use Gson to convert it:

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

这种方式可以更容易收集他们都在一个名单,其中,事件&GT; 这是preferable以上名单,其中,地图&LT;字符串,字符串&GT;&GT;

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

这篇关于如何整合jQuery的日历JSP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆