我在我的 JSP 应用程序中使用 DHXScheduler 来安排考试时间表。我添加了一个完全自定义的灯箱,其中包含我自己的字段以及有关考试的信息。
这是我的 EventsManager 类,它保存、创建和获取事件。它扩展了 DHXEventsManager 类。
public class EventsManager extends DHXEventsManager {
public EventsManager(HttpServletRequest request) {
super(request);
}
public Iterable getEvents() {
Connection conn = null;
DHXEventsManager.date_format = "yyyy-MM-dd HH:mm:ss";
List<Event> events = new ArrayList<Event>();
try {
conn = SQLHelper.getConnection();
Statement statement = conn.createStatement();
StringBuffer query = new StringBuffer();
query.append("SELECT * FROM ");
query.append(TimetableEvent.TBL_EVENTS);
query.append(" e JOIN ");
query.append(StudyUnit.TBL_STUDYUNITS);
query.append(" s ON e.");
query.append(TimetableEvent.FLD_UNITCODE);
query.append(" = s.");
query.append(StudyUnit.FLD_UNITCODE);
ResultSet rs = statement.executeQuery(query.toString());
while (rs.next()) {
Event e = new Event(); // extends DHXEvent and contains additional fields
e.setId(Integer.parseInt(rs.getString(TimetableEvent.FLD_ID)));
e.setText(rs.getString(TimetableEvent.FLD_UNITCODE));
e.setStart_date(rs.getString(TimetableEvent.FLD_STARTDATE));
e.setEnd_date(rs.getString(TimetableEvent.FLD_ENDDATE));
e.setUnitCode(rs.getString(StudyUnit.FLD_UNITCODE));
e.setTitle(rs.getString(StudyUnit.FLD_TITLE));
e.setYear(rs.getString(StudyUnit.FLD_YEAR));
e.setSemester(rs.getShort(StudyUnit.FLD_SEMESTER));
e.setExamLength(rs.getFloat(StudyUnit.FLD_EXAMLENGTH));
e.setNoOfStudents(rs.getShort(StudyUnit.FLD_NOOFSTUDENTS));
e.setDepartment(rs.getString(StudyUnit.FLD_DEPARTMENT));
e.setCredits(rs.getShort(StudyUnit.FLD_CREDITS));
e.setEvening(rs.getBoolean(StudyUnit.FLD_EVENING));
events.add(e);
}
} catch (SQLException e) {
System.out.println("[EventsManager.getEvents()]: " + e.getMessage());
} finally {
SQLHelper.closeConnection(conn);
}
DHXEventsManager.date_format = "MM/dd/yyyy HH:mm";
return events;
}
@Override
public DHXStatus saveEvent(DHXEv event, DHXStatus status) {
Connection conn = SQLHelper.getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;
Event ev = (Event) event;
try {
if (status == DHXStatus.UPDATE) {
pstmt = TimetableEvent.updateEvent(conn, ev);
} else if (status == DHXStatus.INSERT) {
System.out.println("SAVE");
pstmt = TimetableEvent.insertEvent(conn, ev);
} else if (status == DHXStatus.DELETE) {
pstmt = TimetableEvent.deleteEvent(conn, event);
}
if (pstmt != null) {
pstmt.executeUpdate();
rs = pstmt.getGeneratedKeys();
if (rs.next()) {
event.setId(rs.getInt(1));
ev.setId(rs.getInt(1));
}
}
} catch (SQLException e) {
System.out.println("[EventsManager.saveEvent() - " + status.name() + "]: " + e.getMessage());
e.printStackTrace();
} finally {
if (rs != null) SQLHelper.closeResultSet(rs);
if (pstmt != null) SQLHelper.closePreparedStatement(pstmt);
if (conn != null) SQLHelper.closeConnection(conn);
}
return status;
}
@Override
public DHXEv createEvent(String id, DHXStatus status) {
return new Event();
}
}
该类Event
扩展DHXEvent
并包含附加字段的 getter 和 setter。问题是,当我从DHXEv
to转换时Event
,我将其他字段丢失为 null 但 saveEvent 方法只接受 DHXEv 作为参数。
这是events.jsp
调用 EventsManager 中的 getEvents() 方法以获取带有事件的 JSON 对象的文件。
<%@ page contentType="application/json"
import="com.dhtmlx.planner.*,servlets.*,events.EventsManager"
%>
<%= getEvents(request) %>
<%!
String getEvents(HttpServletRequest request) throws Exception {
EventsManager evs = new EventsManager(request);
return evs.run();
}
%>
这是它返回的样本。如您所见,它仅返回来自DHXEv
对象的字段,即id, start_date, end_date and text
。
{id:7, text:CIS3087, end_date:03/27/2014 13:00, start_date:03/27/2014 10:00}
如何将这些附加字段添加到 JSON 对象,即有一个带有 studyunit 标题、部门等的对象?
这是调度程序的一些相关客户端代码。它的配置以及 save_form() 和 show_lightbox 方法:
scheduler.config.api_date = "%Y-%m-%d %H:%i";
scheduler.config.details_on_dblclick = true;
scheduler.config.details_on_create = true;
scheduler.config.first_hour = "8";
scheduler.config.last_hour = "22";
scheduler.config.drag_lightbox = true;
scheduler.config.show_loading = true;
scheduler.config.mark_now = true;
scheduler.init('scheduler_here', new Date(), "week");
scheduler.load("events.jsp", "json");
var dp = new dataProcessor("events.jsp");
dp.init(scheduler);
function save_form() {
var ev = scheduler.getEvent(scheduler.getState().lightbox_id);
ev.text = html("studyunit_code").value;
ev.start_date = setDate("start_date", "starttime");
ev.end_date = setDate("start_date", "endtime");
scheduler.endLightbox(true, html("custom_form"));
$("#form1").submit(); // submits all the other fields and saves them to a db table
}
scheduler.showLightbox = function(id) {
var ev = scheduler.getEvent(id);
scheduler.startLightbox(id, html("custom_form"));
html("studyunit_code").value = ev.text;
html("studyunit_code").focus();
html("studyunit_title").value = ev.studyunit_title;
var startDate = ev.start_date;
getDate(startDate, "start_date", "starttime");
var endDate = ev.end_date;
getDate(endDate, "start_date", "endtime");
};
我猜这只是我必须进行的一个小修改才能获取 JSON 对象中的字段并将它们显示在灯箱中。
你知道我该如何解决这个问题吗?谢谢!