【问题标题】:how to integrate jquery fullcalendar with jsp如何将 jquery fullcalendar 与 jsp 集成
【发布时间】:2010-02-14 14:17:43
【问题描述】:
events: [
    {
     title: 'All Day Event',
     start: new Date(2010,2,13)
    },
    {
     title: 'Long Event',
     start: new Date(y, m, d-5),
     end: new Date(y, m, d-2)
    }
]

从数据库中获取数据后如何在JSP中制作这个数组?

【问题讨论】:

    标签: jquery ajax json jsp servlets


    【解决方案1】:

    好吧,您的问题含糊不清,但通常最简单的方法是将您的数据作为 Bean 或 Map 绑定到请求中,作为属性或其他东西;这取决于您使用的框架。完成后,您只需通过 JSTL EL 表达式引用字段:

    events: [
      <c:forEach var='event' items='${myData.events}'>
        { title: '${event.title}', start: new Date(${event.timestamp}) },
      </c:forEach>
      null
    ];
    

    后面的“null”是为IE放一些东西;您还可以编写循环代码以避免杂散的尾随逗号。

    在我的应用程序中,我总是编写一个“jsQuote”EL 函数,该函数对 Javascript 字符串常量语法执行类似于“fn:escapeXml”的操作。像这样直接将事件标题之类的内容转储到 Javascript 源中是很危险的,但是标准 JSP 库中没有函数可以为 Javascript 执行字符串转义。然而,写一个很容易。因此,在我的代码中,该行如下所示:

      {title: '${xyz:jsQuote(event.title)}', start: new Date(${event.timestamp}) },
    

    您不必担心数据中的“时间戳”是“长”还是“长”。

    同样,这一切都完全取决于您的数据是什么样的。

    【讨论】:

      【解决方案2】:
      1. 创建一个 Javabean 类 Event,如下所示

        public class Event {
            private String title;
            private Date start;
            private Date end;
            // Add/generate getters and setters.
        }
        
      2. 创建一个Servlet,它使用Google Gson 将其转换为JSON

        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOexception {
            List<Event> events = eventDAO.list();
            String json = new Gson().toJson(events);
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(json);
        }
        

        将此映射到url-pattern 上,例如/jsonGetEvents

      3. 在 jQuery 中使用 $.getJSON 函数:

        $.getJSON('jsonGetEvents', function(events) {
            // `events` is a JSON string. Do your thing with it. This examples loops over it.
            $.each(events, function(index, event) {
                var title = event.title;
                var start = event.start;
                var end = event.end;
            });
        });
        

      【讨论】:

        猜你喜欢
        • 2014-09-22
        • 2010-12-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-12
        • 1970-01-01
        相关资源
        最近更新 更多