Event calendar with Dynamic data from databse - using java, jsp and struts
Thanks
Raj
- Call from Jsp ( add js and css files required - avaialble in below mentioned link)
- struts-config.xml
- AjaxAction - actionservlet
- AjaxMoreUtil - Support java class for data fetch (For db table - create table with mentioned columns - screenshot attached)
- EventsBean to set the values
1.Call from jsp page with jquery and ajax
for js and css file, we can download from http://arshaw.com/fullcalendar/ ---- link
and we can place it our project folder
Jsp file create - eventCalendar.jsp
<!DOCTYPE html>
<html>
<head>
<link rel='stylesheet' href='<%=request.getContextPath()%>/cal/jquery-ui.min.css' />
<link rel='stylesheet' href='<%=request.getContextPath()%>/fullcalendar/fullcalendar.css' />
<link rel='stylesheet' href='<%=request.getContextPath()%>/fullcalendar/fullcalendar.print.css' media='print' />
<script type="text/javascript" src='<%=request.getContextPath()%>/lib/jquery.min.js'></script>
<script type="text/javascript" src='<%=request.getContextPath()%>/js/jquery-ui.custom.min.js'></script>
<script type="text/javascript" src='<%=request.getContextPath()%>/fullcalendar/fullcalendar.min.js'></script>
<script type="text/javascript">
$(document).ready(function() {
$.post('getCalendarEvents.html?method=allEvents', {action: ''},
function(data) {
var date = new Date();
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();
$('#calendar').fullCalendar({
theme: true,
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
editable: true,
events: data
});
}, "json"
);
});
</script>
<style>
body {
font-size: 13px;
font-family: "Lucida Grande",Helvetica,Arial,Verdana,sans-serif;
}
#calendar {
margin-left: 5px;
width: 850px !important;
}
</style>
</head>
<body>
<div id='calendar'></div>
</body>
</html>
<html>
<head>
<link rel='stylesheet' href='<%=request.getContextPath()%>/cal/jquery-ui.min.css' />
<link rel='stylesheet' href='<%=request.getContextPath()%>/fullcalendar/fullcalendar.css' />
<link rel='stylesheet' href='<%=request.getContextPath()%>/fullcalendar/fullcalendar.print.css' media='print' />
<script type="text/javascript" src='<%=request.getContextPath()%>/lib/jquery.min.js'></script>
<script type="text/javascript" src='<%=request.getContextPath()%>/js/jquery-ui.custom.min.js'></script>
<script type="text/javascript" src='<%=request.getContextPath()%>/fullcalendar/fullcalendar.min.js'></script>
<script type="text/javascript">
$(document).ready(function() {
$.post('getCalendarEvents.html?method=allEvents', {action: ''},
function(data) {
var date = new Date();
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();
$('#calendar').fullCalendar({
theme: true,
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
editable: true,
events: data
});
}, "json"
);
});
</script>
<style>
body {
font-size: 13px;
font-family: "Lucida Grande",Helvetica,Arial,Verdana,sans-serif;
}
#calendar {
margin-left: 5px;
width: 850px !important;
}
</style>
</head>
<body>
<div id='calendar'></div>
</body>
</html>
2.In struts-config.xml file
<action path="/getCalendarEvents" scope="request" type="com.naaraa.action.ajax.AjaxAction" validate="false"/>
3.In AjaxAction.java file
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
AjaxMoreUtil.getAllEvents(request, response, userId);
return null;
}
HttpServletRequest request, HttpServletResponse response)
throws Exception {
AjaxMoreUtil.getAllEvents(request, response, userId);
return null;
}
getting datas from database from the table "user_events"
4.In AjaxMoreUtil.java file
public static void getAllEvents(HttpServletRequest request, HttpServletResponse response) throws Exception {
Connection con = null;
Statement st = null;
ResultSet rs = null;
String query = "";
ArrayList<EventsBean> allEventList = new ArrayList<EventsBean>();
try {
if (userId != null && !"".equals(userId)) {
query = "select events_id,events_name,location,from_date,from_time,to_date,to_time from user_events";
}
//System.out.println(query);
con = DBCPoolSource.getConnection(request);
st = con.createStatement();
rs = st.executeQuery(query);
while (rs.next()) {
EventsBean bean = new EventsBean();
bean.setTitle(rs.getString("events_name"));
bean.setStart(rs.getString("from_date"));
bean.setEnd(rs.getString("to_date"));
bean.setAllDay("false");
bean.setTextColor("#fff");
allEventList.add(bean);
}
String json = new Gson().toJson(allEventList);
//System.out.println("json value " + json);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (con != null) {
con.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (st != null) {
st.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (rs != null) {
rs.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Connection con = null;
Statement st = null;
ResultSet rs = null;
String query = "";
ArrayList<EventsBean> allEventList = new ArrayList<EventsBean>();
try {
if (userId != null && !"".equals(userId)) {
query = "select events_id,events_name,location,from_date,from_time,to_date,to_time from user_events";
}
//System.out.println(query);
con = DBCPoolSource.getConnection(request);
st = con.createStatement();
rs = st.executeQuery(query);
while (rs.next()) {
EventsBean bean = new EventsBean();
bean.setTitle(rs.getString("events_name"));
bean.setStart(rs.getString("from_date"));
bean.setEnd(rs.getString("to_date"));
bean.setAllDay("false");
bean.setTextColor("#fff");
allEventList.add(bean);
}
String json = new Gson().toJson(allEventList);
//System.out.println("json value " + json);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (con != null) {
con.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (st != null) {
st.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (rs != null) {
rs.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
5.In EventsBean.java file
public class EventsBean extends org.apache.struts.action.ActionForm {
private String title;
private String start;
private String end;
private String textColor;
private String allDay;
private String start;
private String end;
private String textColor;
private String allDay;
public EventsBean(String title, String start, String end, String textColor, String allDay) {
this.setTitle(title);
this.setStart(start);
this.setEnd(end);
this.setTextColor(textColor);
this.setAllDay(allDay);
}
public EventsBean() {
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getStart() {
return start;
}
public void setStart(String start) {
this.start = start;
}
public String getEnd() {
return end;
}
public void setEnd(String end) {
this.end = end;
}
public String getTextColor() {
return textColor;
}
public void setTextColor(String textColor) {
this.textColor = textColor;
}
public String getAllDay() {
return allDay;
}
public void setAllDay(String allDay) {
this.allDay = allDay;
}
this.setTitle(title);
this.setStart(start);
this.setEnd(end);
this.setTextColor(textColor);
this.setAllDay(allDay);
}
public EventsBean() {
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getStart() {
return start;
}
public void setStart(String start) {
this.start = start;
}
public String getEnd() {
return end;
}
public void setEnd(String end) {
this.end = end;
}
public String getTextColor() {
return textColor;
}
public void setTextColor(String textColor) {
this.textColor = textColor;
}
public String getAllDay() {
return allDay;
}
public void setAllDay(String allDay) {
this.allDay = allDay;
}
}
output result view in browser
Raj