Saturday, April 13, 2013

How to create gridview dynamically in jsp

How to generate gridview dynamically on runtime.We all waste lot of time ,to manage data in Grid view (Table Form ) Each Time when there is another Database table,we have to create a new Grid view for that.We can utilize this time while creating a single Grid view control that automatically generate Grid view.That Control accept Collection and bean object from us and generate the corresponding Grid View.This can be done by using reflection,Now there is no need to typecast the collection(ArrayList) and iterate it.While using reflection we can retrive the fields of any object. there is example
       for (Field field : bean.getClass().getDeclaredFields())
           {
             field.setAccessible(true); //set it to make Accessiable
             System.out.println( field.getName());
           }
Here bean is object and field object save the Fields retrive form bean object This code print the field name of your bean object(In Control this fieldname became the heading of grid view).Now how to retrive the value from the collection Object
       for (Field field : collection.getClass().getDeclaredFields())
            {
                field.setAccessible(true);
                Object value;
                value = field.get(collection);// Retrive the vale from the Collection object
                System.out.println(value);
            }
field.get(Object) return the value in field From this code we retrive the value's of collection.Now you got the basic idea how control works.I already created Grid view Control,You just have only to use it Download Code  .Here is Smaple view for the output of control
Pass the Collection and bean object,name of servlet .when you click on Select or Delete the form is submitted and control goes to servlet (which you specify).And you have to chosse one unique field accordingly which perform Select or Delete operation .Suppose I want to select or delete field  according to Id (As specified in Image),then I set index in my code to 0 and if I want to do operation according to Name then I set index to 1 and so on ,Start it from 0.Here is Code for Passing bean object,Collection Object ,Servlet Name and Index.
<%
       SignupDA sa = new SignupDA();
       Signups da = sa.Getall();//collection object
       Signup be = new Signup();//bean object
       int ind = 0; // It is index
       application.setAttribute("bean", be);
       application.setAttribute("coll", da);
       application.setAttribute("action", "ManageCategory"); servlet name 
       application.setAttribute("index", ind);
    %>
    <%@ include file="/Control/Gridview.jsp"%>
Collection is nothing new ,it is same as Arraylist<bean> you can use ArrayList<yourbean> instead of Singups,sa is object of DBoperation,be is object of bean class,ind is index,ManageCategory is Servlet.Now how to use the Control on your jsp page.Grid view control is also a jsp page.For this we have to include the Gridview.jsp file on current Jsp which is specifed in previous Code.This way you can use Gridview Control in your project 
If you click on Link select or delete it will go to the servlet, you can recieve the value in doPost section as shown below 
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException
        {
          String Id = request.getParameter("index");
          String Operation = request.getParameter("operation");
          System.out.println(Id + " " + Operation);
        }
Here you recieve the index and operation.if you want to delete then you can use"if(Operation.equals("Delete")) {your code}" by using Id (Index value)For Select "if (Operation.equals("Select")) {your code}"

Here is code for Gridview Control  Download Code 
If you have any confusion .Please Drop your Comment Below

0 comments :

Post a Comment