gpt4 book ai didi

javascript - 通过 servlet 将数据库中的数据显示到 fullcalendar - 未列出事件

转载 作者:太空宇宙 更新时间:2023-11-04 13:30:32 26 4
gpt4 key购买 nike

这是我第一次使用 .jsp 和 Netbeans。我正在尝试创建一个利用 fullcalendar 的系统,用户可以选择月/年对并显示谁在所选月份的哪一天休假。基本上,它不会列出事件,而是列出缺席的员工姓名。我将这些名称称为事件。

这里的问题是日历没有显示任何事件。我关注了this tutorialthis 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="${01}" ${param.month == 01 ? 'selected' : ''}>January</option>
<option value="${02}" ${param.month == 02 ? 'selected' : ''}>February</option>
<option value="${03}" ${param.month == 03 ? 'selected' : ''}>March</option>
<option value="${04}" ${param.month == 04 ? 'selected' : ''}>April</option>
<option value="${05}" ${param.month == 05 ? 'selected' : ''}>May</option>
<option value="${06}" ${param.month == 06 ? 'selected' : ''}>June</option>
<option value="${07}" ${param.month == 07 ? 'selected' : ''}>July</option>
<option value="${08}" ${param.month == 08 ? 'selected' : ''}>August</option>
<option value="${09}" ${param.month == 09 ? 'selected' : ''}>September</option>
<option value="${10}" ${param.month == 10 ? 'selected' : ''}>October</option>
<option value="${11}" ${param.month == 11 ? 'selected' : ''}>November</option>
<option value="${12}" ${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 类。它包含标题、开始、结束、颜色和文本颜色。所有变量都是字符串,并且有各自的 setter 和 getter。

servlet 在 web.xml 中正确映射,并且 Netbeans 不会输出任何错误。

但是,从 Chrome 控制台我可以看到 KServlet 返回了一个只有“[]”的空白页面。我猜我弄乱了 servlet 中的某些内容,但我不确定是什么。

我真的很感谢任何可以提供帮助的人。

最佳答案

多亏了 Arvind 和飓风,我现在设法让它工作了。除了错误的 SQL 语句之外,还有 javascript 导入和不合适的数据类型(String 而不是 int)的安排导致了问题。

我已经编辑了我的脚本,以防有人想用它作为指南

关于javascript - 通过 servlet 将数据库中的数据显示到 fullcalendar - 未列出事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32245898/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com