通过 servlet 将数据库中的数据显示到 fullcalendar - 未列出事件
Displaying data from database into fullcalendar through servlet - No events listed
这是我第一次使用 .jsp 和 Netbeans。我正在尝试创建一个利用全日历的系统,用户可以在其中 select month/year 对并显示谁在 selected 月份的哪一天休假。基本上,它不会列出事件,而是会列出缺席的员工姓名。我将这些名称称为事件。
这里的问题是日历没有显示任何事件。我已经按照 this tutorial and this question(指的是我链接的教程)进行操作,但似乎无法正常工作。日历在那里,但没有事件。我可以通过单击日历或编辑 .jsp 中的数组来手动添加事件,但我希望它从数据库生成事件。
这是我的代码:
main.jsp
<form name="choose" method="POST">
<table class="centerTable">
<tr>
<td>Month</td>
<td>:</td>
<td>
<select name="month" style="width:130px" required>
<option value="">Select a month:</option>
<option value="" ${param.month == 01 ? 'selected' : ''}>January</option>
<option value="" ${param.month == 02 ? 'selected' : ''}>February</option>
<option value="" ${param.month == 03 ? 'selected' : ''}>March</option>
<option value="" ${param.month == 04 ? 'selected' : ''}>April</option>
<option value="" ${param.month == 05 ? 'selected' : ''}>May</option>
<option value="" ${param.month == 06 ? 'selected' : ''}>June</option>
<option value="" ${param.month == 07 ? 'selected' : ''}>July</option>
<option value="" ${param.month == 08 ? 'selected' : ''}>August</option>
<option value="" ${param.month == 09 ? 'selected' : ''}>September</option>
<option value="" ${param.month == 10 ? 'selected' : ''}>October</option>
<option value="" ${param.month == 11 ? 'selected' : ''}>November</option>
<option value="" ${param.month == 12 ? 'selected' : ''}>December</option>
</select>
</td>
</tr>
<tr>
<td>Year</td>
<td>:</td>
<td>
<select name="year" style="width:130px" required>
<option value="">Select a year:</option>
<option value="${2016}" ${param.year == '2016' ? 'selected' : ''}>2016</option>
<option value="${2015}" ${param.year == '2015' ? 'selected' : ''}>2015</option>
<option value="${2014}" ${param.year == '2014' ? 'selected' : ''}>2014</option>
<option value="${2013}" ${param.year == '2013' ? 'selected' : ''}>2013</option>
<option value="${2012}" ${param.year == '2012' ? 'selected' : ''}>2012</option>
</select>
</td>
</tr>
<tr>
<td colspan=3 align=center>
<input type="submit" name="submit" value="Submit" onChange="submit();return false;">
</td>
</tr>
</table>
</form>
<script>
var y = "${param.year}";
var m = "${param.month}";
$(document).ready(function() {
$('#calendar').fullCalendar({
selectable: true,
selectHelper: true,
select: function(start, end) {
var title = prompt('Event Title:');
var eventData;
if (title) {
eventData = {
title: title,
start: start,
end: end
};
$('#calendar').fullCalendar('renderEvent', eventData, true);
}
$('#calendar').fullCalendar('unselect');
},
editable: true,
eventLimit: true,
events: "/Leave/KServlet"
});
if (y === "" || m === "")
$('#calendar').fullCalendar('today');
else
$('#calendar').fullCalendar('gotoDate', (y+'-'+m));
});
</script>
<div id="calendar"></div>
KServlet.java
public class KServlet extends HttpServlet
{
Calendar now = Calendar.getInstance();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
List l = new ArrayList();
try {
Connection con = null;
con = DriverManager.getConnection("jdbc:derby://localhost:1527/C:\Users\Acer\Desktop\livedb?username=username&password=password");
int month, year;
if (request.getParameter("month").equals(""))
month = now.get(Calendar.MONTH) + 1;
else
month = Integer.parseInt(request.getParameter("month"));
if (request.getParameter("year").equals(""))
year = now.get(Calendar.YEAR);
else
year = Integer.parseInt(request.getParameter("year"));
Statement stmt = null;
String query = "select PERSON_NAME, TYPE_CODE, FROM_DATE, TO_DATE " +
"from LV_INFO_VIEW " +
"where YEAR(FROM_DATE) = " + year + " and MONTH(FROM_DATE) = " + month;
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
CalendarDTO c = new CalendarDTO();
c.setTitle(rs.getString("PERSON_NAME"));
c.setStart(rs.getString("FROM_DATE"));
c.setEnd(rs.getString("TO_DATE"));
String t = rs.getString("TYPE_CODE");
if (t.equalsIgnoreCase("ANNUAL") || t.equalsIgnoreCase("AL - CT"))
{
c.setColor("green");
c.setTextColor("white");
}
else if (t.equalsIgnoreCase("MEDICAL") || t.equalsIgnoreCase("ML-CT"))
{
c.setColor("red");
c.setTextColor("white");
}
else if (t.equalsIgnoreCase("MARRIAGE"))
{
c.setColor("#ff6699");
c.setTextColor("black");
}
else if (t.equalsIgnoreCase("MATERNITY") || t.equalsIgnoreCase("PATERNITY"))
{
c.setColor("#b2e7d3");
c.setTextColor("black");
}
else if (t.equalsIgnoreCase("UNPAID"))
{
c.setColor("blue");
c.setTextColor("white");
}
else if (t.equalsIgnoreCase("COMPASSIONATE") || t.equalsIgnoreCase("DOF"))
{
c.setColor("black");
c.setTextColor("white");
}
else if (t.equalsIgnoreCase("REPLACEMENT"))
{
c.setColor("#720045");
c.setTextColor("white");
}
else if (t.equalsIgnoreCase("HOSPITAL") || t.equalsIgnoreCase("SICK"))
{
c.setColor("#7a0000");
c.setTextColor("white");
}
else if (t.equalsIgnoreCase("EMERGENCY"))
{
c.setColor("yellow");
c.setTextColor("black");
}
else
{
c.setColor("#ffd4b1");
c.setTextColor("black");
}
l.add(c);
}
}
catch (SQLException ex) { Logger.getLogger(KServlet.class.getName()).log(Level.SEVERE, null, ex); }
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.write(new Gson().toJson(l));
}
}
还有 CalendarDTO class。它包含标题、开始、结束、颜色和文本颜色。所有变量都是字符串,并且有各自的 setter 和 getter。
servlet 已正确映射到 web.xml 并且 Netbeans 不会输出任何错误。
但是,从 Chrome 控制台我可以看到 KServlet 返回了一个只有“[]”的空白页面。我猜我在 servlet 中搞砸了一些东西,但我不确定是什么。
我真的很感谢任何能提供帮助的人。
多亏了 Arvind 和 hurricane,我现在才设法让它运行起来。除了导致问题的错误 SQL 语句外,javascript 导入和不合适的数据类型(String 而不是 int)的安排也是如此。
我已经编辑了我的脚本以防有人想用它作为指南
这是我第一次使用 .jsp 和 Netbeans。我正在尝试创建一个利用全日历的系统,用户可以在其中 select month/year 对并显示谁在 selected 月份的哪一天休假。基本上,它不会列出事件,而是会列出缺席的员工姓名。我将这些名称称为事件。
这里的问题是日历没有显示任何事件。我已经按照 this tutorial and this question(指的是我链接的教程)进行操作,但似乎无法正常工作。日历在那里,但没有事件。我可以通过单击日历或编辑 .jsp 中的数组来手动添加事件,但我希望它从数据库生成事件。
这是我的代码:
main.jsp
<form name="choose" method="POST">
<table class="centerTable">
<tr>
<td>Month</td>
<td>:</td>
<td>
<select name="month" style="width:130px" required>
<option value="">Select a month:</option>
<option value="" ${param.month == 01 ? 'selected' : ''}>January</option>
<option value="" ${param.month == 02 ? 'selected' : ''}>February</option>
<option value="" ${param.month == 03 ? 'selected' : ''}>March</option>
<option value="" ${param.month == 04 ? 'selected' : ''}>April</option>
<option value="" ${param.month == 05 ? 'selected' : ''}>May</option>
<option value="" ${param.month == 06 ? 'selected' : ''}>June</option>
<option value="" ${param.month == 07 ? 'selected' : ''}>July</option>
<option value="" ${param.month == 08 ? 'selected' : ''}>August</option>
<option value="" ${param.month == 09 ? 'selected' : ''}>September</option>
<option value="" ${param.month == 10 ? 'selected' : ''}>October</option>
<option value="" ${param.month == 11 ? 'selected' : ''}>November</option>
<option value="" ${param.month == 12 ? 'selected' : ''}>December</option>
</select>
</td>
</tr>
<tr>
<td>Year</td>
<td>:</td>
<td>
<select name="year" style="width:130px" required>
<option value="">Select a year:</option>
<option value="${2016}" ${param.year == '2016' ? 'selected' : ''}>2016</option>
<option value="${2015}" ${param.year == '2015' ? 'selected' : ''}>2015</option>
<option value="${2014}" ${param.year == '2014' ? 'selected' : ''}>2014</option>
<option value="${2013}" ${param.year == '2013' ? 'selected' : ''}>2013</option>
<option value="${2012}" ${param.year == '2012' ? 'selected' : ''}>2012</option>
</select>
</td>
</tr>
<tr>
<td colspan=3 align=center>
<input type="submit" name="submit" value="Submit" onChange="submit();return false;">
</td>
</tr>
</table>
</form>
<script>
var y = "${param.year}";
var m = "${param.month}";
$(document).ready(function() {
$('#calendar').fullCalendar({
selectable: true,
selectHelper: true,
select: function(start, end) {
var title = prompt('Event Title:');
var eventData;
if (title) {
eventData = {
title: title,
start: start,
end: end
};
$('#calendar').fullCalendar('renderEvent', eventData, true);
}
$('#calendar').fullCalendar('unselect');
},
editable: true,
eventLimit: true,
events: "/Leave/KServlet"
});
if (y === "" || m === "")
$('#calendar').fullCalendar('today');
else
$('#calendar').fullCalendar('gotoDate', (y+'-'+m));
});
</script>
<div id="calendar"></div>
KServlet.java
public class KServlet extends HttpServlet
{
Calendar now = Calendar.getInstance();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
List l = new ArrayList();
try {
Connection con = null;
con = DriverManager.getConnection("jdbc:derby://localhost:1527/C:\Users\Acer\Desktop\livedb?username=username&password=password");
int month, year;
if (request.getParameter("month").equals(""))
month = now.get(Calendar.MONTH) + 1;
else
month = Integer.parseInt(request.getParameter("month"));
if (request.getParameter("year").equals(""))
year = now.get(Calendar.YEAR);
else
year = Integer.parseInt(request.getParameter("year"));
Statement stmt = null;
String query = "select PERSON_NAME, TYPE_CODE, FROM_DATE, TO_DATE " +
"from LV_INFO_VIEW " +
"where YEAR(FROM_DATE) = " + year + " and MONTH(FROM_DATE) = " + month;
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
CalendarDTO c = new CalendarDTO();
c.setTitle(rs.getString("PERSON_NAME"));
c.setStart(rs.getString("FROM_DATE"));
c.setEnd(rs.getString("TO_DATE"));
String t = rs.getString("TYPE_CODE");
if (t.equalsIgnoreCase("ANNUAL") || t.equalsIgnoreCase("AL - CT"))
{
c.setColor("green");
c.setTextColor("white");
}
else if (t.equalsIgnoreCase("MEDICAL") || t.equalsIgnoreCase("ML-CT"))
{
c.setColor("red");
c.setTextColor("white");
}
else if (t.equalsIgnoreCase("MARRIAGE"))
{
c.setColor("#ff6699");
c.setTextColor("black");
}
else if (t.equalsIgnoreCase("MATERNITY") || t.equalsIgnoreCase("PATERNITY"))
{
c.setColor("#b2e7d3");
c.setTextColor("black");
}
else if (t.equalsIgnoreCase("UNPAID"))
{
c.setColor("blue");
c.setTextColor("white");
}
else if (t.equalsIgnoreCase("COMPASSIONATE") || t.equalsIgnoreCase("DOF"))
{
c.setColor("black");
c.setTextColor("white");
}
else if (t.equalsIgnoreCase("REPLACEMENT"))
{
c.setColor("#720045");
c.setTextColor("white");
}
else if (t.equalsIgnoreCase("HOSPITAL") || t.equalsIgnoreCase("SICK"))
{
c.setColor("#7a0000");
c.setTextColor("white");
}
else if (t.equalsIgnoreCase("EMERGENCY"))
{
c.setColor("yellow");
c.setTextColor("black");
}
else
{
c.setColor("#ffd4b1");
c.setTextColor("black");
}
l.add(c);
}
}
catch (SQLException ex) { Logger.getLogger(KServlet.class.getName()).log(Level.SEVERE, null, ex); }
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.write(new Gson().toJson(l));
}
}
还有 CalendarDTO class。它包含标题、开始、结束、颜色和文本颜色。所有变量都是字符串,并且有各自的 setter 和 getter。
servlet 已正确映射到 web.xml 并且 Netbeans 不会输出任何错误。
但是,从 Chrome 控制台我可以看到 KServlet 返回了一个只有“[]”的空白页面。我猜我在 servlet 中搞砸了一些东西,但我不确定是什么。
我真的很感谢任何能提供帮助的人。
多亏了 Arvind 和 hurricane,我现在才设法让它运行起来。除了导致问题的错误 SQL 语句外,javascript 导入和不合适的数据类型(String 而不是 int)的安排也是如此。
我已经编辑了我的脚本以防有人想用它作为指南