custom paging with strut and hibernate


STEP : 1 Database Structure

— phpMyAdmin SQL Dump
— version 3.3.9
http://www.phpmyadmin.net

— Host: localhost
— Generation Time: Jun 16, 2011 at 06:39 PM
— Server version: 5.5.8
— PHP Version: 5.3.5

SET SQL_MODE=”NO_AUTO_VALUE_ON_ZERO”;


— Database: `college`

— ——————————————————–


— Table structure for table `courses`

CREATE TABLE IF NOT EXISTS `courses` (
`course_id` int(11) NOT NULL AUTO_INCREMENT,
`course_name` varchar(50) NOT NULL,
PRIMARY KEY (`course_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=67 ;


— Dumping data for table `courses`

INSERT INTO `courses` (`course_id`, `course_name`) VALUES
(1, ‘MnBBS’),
(2, ‘MBA’),
(3, ‘BSC’),
(14, ‘MBA’),
(13, ‘MnBBS’),
(12, ‘BSC’),
(11, ‘MBA’),
(10, ‘MnBBS’),
(15, ‘BSC’),
(16, ‘MnBBS’),
(17, ‘MBA’),
(18, ‘BSC’),
(19, ‘MnBBS’),
(20, ‘MBA’),
(21, ‘BSC’),
(22, ‘MnBBS’),
(23, ‘MBA’),
(24, ‘BSC’),
(25, ‘MBA’),
(26, ‘MnBBS’),
(27, ‘BSC’),
(28, ‘MBA’),
(29, ‘MnBBS’),
(30, ‘BSC’),
(31, ‘MnBBS’),
(32, ‘MBA’),
(33, ‘BSC’),
(34, ‘MnBBS’),
(35, ‘MBA’),
(36, ‘BSC’),
(37, ‘MnBBS’),
(38, ‘MBA’),
(39, ‘BSC’),
(40, ‘MBA’),
(41, ‘MnBBS’),
(42, ‘BSC’),
(43, ‘MBA’),
(44, ‘MnBBS’),
(45, ‘BSC’),
(46, ‘MnBBS’),
(47, ‘MBA’),
(48, ‘BSC’),
(49, ‘MnBBS’),
(50, ‘MBA’),
(51, ‘BSC’),
(52, ‘MnBBS’),
(53, ‘MBA’),
(54, ‘BSC’),
(55, ‘MBA’),
(56, ‘MnBBS’),
(57, ‘BSC’),
(58, ‘MBA’),
(59, ‘MnBBS’),
(60, ‘BSC’),
(61, ‘MnBBS’),
(62, ‘MBA’),
(63, ‘BSC’),
(64, ‘MnBBS’),
(65, ‘MBA’),
(66, ‘BSC’);

STEP : 2 customPaging.java

package customPaging;

public class customPaging {

public String getpaging(String url,String form_name,int total_records, int current_page_no,int current_page_size)
{

String left_arrow=””;
String textbox=””;
String right_arrow=””;
String page_status=””;
String dropdown=””;
String output=””;
int total_pages;
int lower_record_limit;
int upper_record_limit;
String selected_page_size;

total_pages=(int)(total_records/current_page_size);
if((total_records%current_page_size)>0)
total_pages=total_pages+1;

System.out.println(“Total Records = “+total_records+” Page Size =”+ current_page_size+ ” Total Pages :”+(total_records%current_page_size));
if(current_page_no==1)
{
lower_record_limit=0;
upper_record_limit=current_page_no*current_page_size;
}
else
{
lower_record_limit=(current_page_no-1)*current_page_size;
upper_record_limit=current_page_no*current_page_size;
}

if(current_page_no==1)
left_arrow=”<img src=’page_images/left_arrow.png’ />”;
else
left_arrow=”<a href=’javascript:void(0);’ onclick=’submit_paging(“+(current_page_no-1)+”);’><img src=’page_images/left_arrow.png’ /></a>”;

if(current_page_no==total_pages || total_records <= current_page_size )
right_arrow=”<img src=’page_images/right_arrow.png’ />”;
else
right_arrow=”<a href=’javascript:void(0);’ onclick=’submit_paging(“+(current_page_no+1)+”);’><img src=’page_images/right_arrow.png’ /></a>”;

textbox=”<input type=’text’ name=’txt_current_page’ id=’txt_current_page’ value='”+current_page_no+”‘ style=’width:30px;text-align:center;’  onchange=’submit_textbox_paging(“+(current_page_no+1)+”);’      />”;

page_status=” “+(lower_record_limit+1)+” – “+upper_record_limit+” of “+total_records;
dropdown=”<select name=’current_page_size’ style=’width:50px;text-align:center;’ onChange=’select_paging();’>”;

for(int i=10;i<=100;i=i+10)
{
selected_page_size=””;
if(i==current_page_size)
selected_page_size=”selected=’selected'”;
dropdown=dropdown+”<option value='”+i+”‘ “+selected_page_size+”>”+i+”</option>”;
}

dropdown=dropdown+”</select>”;

output=”<div>”+left_arrow+”&nbsp;”+textbox+”&nbsp;”+right_arrow+”&nbsp;|&nbsp;”+page_status+”&nbsp;|&nbsp; Total Pages : “+total_pages+”&nbsp;|&nbsp; Page Size : “+dropdown+”</div>”;

output=output+”<input type=’hidden’ id=’current_page’ name=’current_page’ value='”+current_page_no+”‘ />”;

output=output+”<script type=’text/javascript’>”;
output=output+”function submit_paging(current_page_no)”;
output=output+”{“;
output=output+”    document.getElementById(‘current_page’).value=current_page_no; “;
output=output+” document.”+form_name+”.submit(); “;

output=output+”}”;
output=output+”function select_paging()”;
output=output+”{“;
// output=output+” document.getElementById(‘current_page’).value=current_page_no; “;
output=output+” document.”+form_name+”.submit(); “;
output=output+”}”;

output=output+”function submit_textbox_paging()”;
output=output+”{“;
output=output+” document.getElementById(‘current_page’).value=document.getElementById(‘txt_current_page’).value; “;
output=output+” document.”+form_name+”.submit(); “;
output=output+”}”;

output=output+” </script>”;

return output;
}

}

STEP :3 SearchcourseAction.java

/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package courses;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import MyHibernet.HibernateSessionFactory;
import courses.SearchcourseForm;
import courses.Courses;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import customPaging.customPaging;

/**
* MyEclipse Struts
* Creation date: 06-07-2011
*
* XDoclet definition:
* @struts.action path=”/searchcourse” name=”searchcourseForm” input=”searchcourse.jsp” scope=”request” validate=”true”
* @struts.action-forward name=”sucess” path=”searchresult.jsp”
* @struts.action-forward name=”fail” path=”searchcourse.jsp”
*/
public class SearchcourseAction extends DispatchAction  {
/*
* Generated Methods
*/

private String search_result_paging=””;

/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward search(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
SearchcourseForm searchcourseForm = (SearchcourseForm) form;// TODO Auto-generated method stub
System.out.println(“called”);

int current_page_no=(request.getParameter(“current_page”)!=null)? Integer.parseInt(request.getParameter(“current_page”)):1 ;
int current_page_size=(request.getParameter(“current_page_size”)!=null)? Integer.parseInt(request.getParameter(“current_page_size”)):2;

System.out.println(“Current Page No :”+current_page_no+” Current Page Size :”+current_page_size);

String keyword=searchcourseForm.getKeyword();
List courses=getSearchResult(keyword,current_page_no,current_page_size);
request.setAttribute(“searchresult”, courses);
request.setAttribute(“keyword”, keyword);
request.setAttribute(“search_result_paging”, search_result_paging);

return mapping.findForward(“sucess”);
}

public List getSearchResult(String keyword,int current_page_no,int current_page_size)
{

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session=sessionFactory.openSession();

Transaction transaction = null;
List courses =null;
String SqlStr=””;

//String SqlStr=”select courses.courseId,courses.courseName,count(students.name) as no_of_students from Courses as courses ,Students as students where students.courseId=courses.courseId and courses.courseName like ‘%”+keyword+”%’ group by courses.courseName order by courses.courseId”;

//START : Paging
transaction = session.beginTransaction();
SqlStr=”select courses.courseId,courses.courseName,courses.courseId as no_of_students from Courses as courses”;
courses = session.createQuery(SqlStr).list();
transaction.commit();
int total_records=courses.size();
int records_start=(current_page_size*(current_page_no-1));
System.out.println(“Total Rerocds=”+total_records+” Current Page No=”+current_page_no+” Current Page Size=”+current_page_size+ ” Records Start=”+records_start);
customPaging obj_paging=new customPaging();
search_result_paging=obj_paging.getpaging(“”, “searchcourseForm”,total_records,current_page_no,current_page_size);
//END : Paging

transaction = session.beginTransaction();
SqlStr=”select courses.courseId,courses.courseName,courses.courseId as no_of_students from Courses as courses”;//limit “+records_start+”,”+current_page_size;
System.out.println(SqlStr);

Query query= session.createQuery(SqlStr);
query.setFirstResult(records_start);
query.setMaxResults(current_page_size);
courses = query.list();
transaction.commit();

session.close();
return courses;
}

}

STEP:4 searchresult.jsp

<%@ page language=”java” pageEncoding=”UTF-8″%>
<%@ taglib uri=”http://struts.apache.org/tags-bean&#8221; prefix=”bean”%>
<%@ taglib uri=”http://struts.apache.org/tags-html&#8221; prefix=”html”%>
<%@page language=”java” import=”java.util.*”%>

<html>
<head>
<title>JSP for SearchcourseForm form</title>

<script type=”text/javascript”>
function checkuncheck(field)
{
var checkall=document.searchcourseForm.chkall;
if(checkall.checked==true)
checkAll(field);
else
uncheckAll(field);
}
function checkAll(field)
{
for (i = 0; i < field.length; i++)
field[i].checked = true ;
}

function uncheckAll(field)
{
for (i = 0; i < field.length; i++)
field[i].checked = false ;
}
function doOper(studentsid,method)
{

var answer = confirm(“Do you really want to delete this record ?”);
if (answer){
document.getElementById(“coursesid”).value=studentsid;
document.getElementById(“method”).value=method;
document.forms[“searchcourseForm”].submit();
return true;
}
else{
return false;
}

}
function doMultiOper(param,field,method)
{
if(param!=-1)
{
var counter=0;
for (i = 0; i < field.length; i++)
{
if(field[i].checked == true)
{
counter=1
break;
}
}
if(counter==1)
{
var answer = confirm(“Do you really want to delete this record ?”);
if (answer){

document.getElementById(“method”).value=method;
document.forms[“searchcourseForm”].submit();
return true;
}
else
{
return false;
}
}
else
{
alert(“Please select records atleat one!”);
return false;
}
}
}
</script>
</head>
<body>
<html:form action=”searchcourse.do”>

<table width=”700px”>
<tr>
<td width=”5%”>&nbsp;</td><td width=”20%”>&nbsp;</td><td width=”20%”>&nbsp;</td><td width=”20%”>Action :</td>
<td width=”35%”>
<select name=”oper” id=”oper” onchange=”doMultiOper(this.value,document.searchcourseForm.chk,’multidelete’);”>
<option value=”-1″>Select</option>
<option value=”delete”>Delete</option>
</select>

</td>
</tr>
<tr><td width=”5%”><input type=”checkbox” name=”chkall” onClick=”checkuncheck(document.searchcourseForm.chk)” /></td><td width=”20%”>Course Id</td><td width=”20%”>Course</td><td width=”20%”>Student</td><td width=”35%”>Action</td></tr>
<%
List searchresult = (List) request.getAttribute(“searchresult”);
%>
<%
for(Iterator itr=searchresult.iterator(); itr.hasNext(); )
{
Object[] obj_list =(Object[])itr.next();
%>

<tr>
<td><input type=”checkbox” name=”chk” value=”<%=obj_list[0] %>” /></td>

<td><%=obj_list[0] %></td>
<td><%=obj_list[1] %></td>
<td><%=obj_list[2] %></td>
<td>
<a href=”javascript:void(0);”  onClick=window.open(“editcourse.do?coursesid=<%=obj_list[0] %>&method=edit”,”Course”,”width=550,height=170,0,status=0,”);  >Edit</a>&nbsp;|&nbsp;
<a href=”javascript:void(0);” onclick=”doOper(‘<%=obj_list[0] %>’,’delete’);” >Delete</a>
</td>
</tr>

<%
}
%>
<tr>
<td colspan=”5″><%=request.getAttribute(“search_result_paging”) %></td>
</tr>
</table>
<html:hidden property=”keyword” styleId=”keyword” value=”<%=(String)request.getAttribute(“keyword”) %>” />
<html:hidden property=”method” styleId=”method” value=”search” />
<input type=”hidden” name=”coursesid” id=”coursesid” value=”” />

</html:form>
</body>
</html>

STEP 6: searchcourse.jsp

<%@ page language=”java” pageEncoding=”UTF-8″%>
<%@ taglib uri=”http://struts.apache.org/tags-bean&#8221; prefix=”bean”%>
<%@ taglib uri=”http://struts.apache.org/tags-html&#8221; prefix=”html”%>

<html>
<head>
<title>JSP for SearchcourseForm form</title>
</head>
<body>
<html:form action=”/searchcourse”>
keyword : <html:text property=”keyword”/><html:errors property=”keyword”/><br/>

<html:submit property=”method”>search</html:submit><br/>
<a href=”addcourse.jsp” >Add Course</a>

</html:form>
</body>
</html>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s