Friday, March 7, 2014

Event calendar - Fullcalendar - Event calendar using java, jsp and struts

Event calendar with Dynamic data from databse - using java, jsp and struts
 
  1. Call from Jsp  ( add js and css files required - avaialble in below mentioned link)
  2. struts-config.xml
  3. AjaxAction - actionservlet
  4. AjaxMoreUtil - Support java class for data fetch (For db table - create table with mentioned columns - screenshot attached)
  5. 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>

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;
    }


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();
        }
        }
    }

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;

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;
    }
}

output result view in browser

 
Thanks
Raj

10 comments:

  1. I was very happy when i saw your code written in this page.

    A big thank you for your article.Really thank you! Cool.

    ReplyDelete
  2. I'm trying this but I unable to resolve issues. Can I get compete code here...

    ReplyDelete
  3. Hi, I have a question...
    Where do I have to add the file struts-config.xml? And this file just going to have these code: ?

    ReplyDelete
  4. You can add your struts-config file in WEB-INF folder

    ReplyDelete
  5. If you create your struts project using Netbeans, you will have struts-config file in WEB-INF folder of your project. If you use Eclipse, you need to create manually in any of the folder and you can do configure in web.xml.

    ReplyDelete
  6. for any query, you may send to my email rajboon@gmail.com for immediate reply.

    ReplyDelete
  7. Replies
    1. Sorry.. i was caught up in my project. This week end will send you full code or i will upload in github

      Delete
    2. can you send me the full codes? i badly need this thank you very much.

      Delete
  8. please send me full code I badly need this

    ReplyDelete