Thursday, 17 March 2011

Crud with JQuery Master-Detail 1

This is a Simple CRUD application using JQuery. The detail screen has
CRUD & prev/next, List buttons. You can select a person from the
master list and work on the detail screen.

Both the detail and list JSP's use JQuery and AJAX calls.
The server side is implemented via Servlets and JSPs.
The data is used from a MySQL table.

CREATE TABLE `persontab` (
`firstname` varchar(20) DEFAULT NULL,
`lastname` varchar(20) DEFAULT NULL,
`hobby` varchar(30) DEFAULT NULL,

You can populate it with:
INSERT INTO `persondao`.`persontab` (`firstname`, `lastname`, `hobby`) VALUES ("name1", "surname", "hobby");
INSERT INTO `persondao`.`persontab` (`firstname`, `lastname`, `hobby`) VALUES ("name2", "surname", "hobby");

Master list JSP uses the JQuery DT abilities fully, including buffering.

ANALYSIS and the source code:
The application opens with index.jsp which is indicated as welcome file at
<display-name>Simple CRUD of ARS</display-name>
(I will provide the complete web.xml when I come to servlet definitions).

Index.jsp has a form named 'theDetail'. This is used for input fields name, lastname
and hobby. At the bottom of this for there are navigation buttons next, prev,
new, save, delete. These buttons are linked to corresponding servlets via AJAX calls
which are included via
<script type="text/javascript" src="js/JQmultiAJAXCalls.js"></script>
at the top of the index.jsp.
A personDAO is defined used to access the MySQL database. personDAO is also
used to keep track of the currentId and the NextAvailableId.

Index.jsp shows the first record in the MySQL DB when it is first opened.
This is done via an AJAX call to the init servlet.

This is the end of an overall outline of the The complete cource follows.

Document : index
Created on : 24.Sub.2011, 18:23:21
Author : Ali Riza SARAL

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<%@ page import="mainPackage.*" %>
<%@ page import="daoPackage.*" %>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ARS's Simple JQ CRUD</title>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.form.js"></script>
<script type="text/javascript" src="js/JQmultiAJAXCalls.js"></script>
System.out.println("index.jsp JSP script began here.");
PersonDAOMySQLDBImpl personDAO;

if (session.getAttribute("personDAOsess") == null){
System.out.println("index.jsp personDAOsess == null ");
personDAO = new PersonDAOMySQLDBImpl();
session.setAttribute("personDAOsess", personDAO);
else {
System.out.println("index.jsp personDAOsess!!!!=null");
personDAO = (PersonDAOMySQLDBImpl) session.getAttribute("personDAOsess");
<script type="text/javascript" charset="utf-8">
function InitScr() {
//alert ("Merhaba from Ali R+ with best wishes! ");
$(document).ready(function() {
url: "./init",
contentType: "application/x-www-form-urlencoded",
dataType: "JSON",
success: function(data) {
var jsonData = $.parseJSON(data);
} );
<body style="background-color:yellow" onLoad="InitScr()">

<h1>The Simplest JQuery CRUD Application</h1>

<br />

<form name="theDetail" action="" method="get">
<input type="hidden" id="id" name="id"/><br />
First name: <input type="text" id ="name" name="name"/><br />
Last name : <input type="text" id="last" name="last"/><br />
Hobby : <input type="text" id="hobby" name="hobby"/><br />
<br />
<INPUT TYPE="button" id="New" VALUE="New">
<INPUT TYPE="button" id="Save" VALUE="Save">
<INPUT TYPE="button" id="Delete" VALUE="Delete">
<INPUT TYPE="button" id="Next" VALUE="Next">
<INPUT TYPE="button" id="Prev" VALUE="Prev">
<INPUT TYPE="button" id="List" VALUE="List">
<!--INPUT TYPE="button" id="List" VALUE="List"
onclick="document.forms[0].action = '/list'; return true;" /-->