Struts 2 CRUD Operation Using JQuery

Introduction

This article is using for create example of struct 2 crud operation using JQuery and Oracle.

Background

We using Oracle for data storing in backgroud process.

Using the code

Please follow below steps for create struds 2 crud operation application.

First we create XML file
  1. //  
  2. //  
  3.   
  4. <?xml version="1.0" encoding="UTF-8" ?>  
  5. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">  
  6.   
  7. <struts>  
  8.    <package name="default" extends="json-default">  
  9.   
  10.       <action name="product" class="com.javatpoint.Product" method="execute">  
  11.          <result name="success">welcome.jsp</result>  
  12.       </action>  
  13.   
  14.       <action name="save" class="com.javatpoint.DatabaseConnection" method="save">  
  15.          <result type="json" name="success">index.jsp</result>  
  16.       </action>  
  17.   
  18.       <action name="update" class="com.javatpoint.DatabaseConnection" method="update">  
  19.          <result type="json" name="success">index.jsp</result>  
  20.       </action>  
  21.   
  22.       <action name="view" class="com.javatpoint.DatabaseConnection" method="view">  
  23.          <!-- <interceptor-ref name="timer" /> -->  
  24.          <result type="json" name="success">index.jsp</result>  
  25.       </action>  
  26.   
  27.   
  28.       <action name="delete" class="com.javatpoint.DatabaseConnection" method="delete">  
  29.          <!-- <param name="time1">viewBookingDtls</param> -->  
  30.          <result type="json">/index.jsp</result>  
  31.       </action>  
  32.   
  33.    </package>  
  34. </struts>   
  35.   
  36. // 
Second step for create action class.
  1. import java.sql.Connection;  
  2. import java.sql.DriverManager;  
  3. import java.sql.PreparedStatement;  
  4. import java.sql.ResultSet;  
  5. import java.util.ArrayList;  
  6.   
  7. import javax.servlet.http.HttpServletRequest;  
  8.   
  9. import org.apache.struts2.convention.annotation.Action;  
  10.   
  11. import com.Bean.UserDetails;  
  12. import com.opensymphony.xwork2.ActionSupport;  
  13. import org.apache.struts2.convention.annotation.Result;  
  14. import org.apache.struts2.interceptor.ServletRequestAware;  
  15.   
  16.   
  17. public class DatabaseConnection extends ActionSupport {  
  18.   
  19.    private static final long serialVersionUID = 1L;  
  20.    ArrayList<UserDetails> list=new ArrayList<UserDetails>();  
  21.    //private javax.servlet.http.HttpServletRequest request;  
  22.   
  23.    Connection con = null;  
  24.   
  25.    private String mess=null;  
  26.   
  27.    public DatabaseConnection(){  
  28.       con = new GetConnectionClass().getConnection();  
  29.   
  30.    }  
  31.   
  32.    /*Here array list will directly accessible into jsp page*/  
  33.    //@Action(value="view",results={@Result(name="success",location="index.jsp")})  
  34.    public String view(){  
  35.       try{  
  36.   
  37.          PreparedStatement ps=con.prepareStatement("select * from ifsapp.user_roles where rownum<50");  
  38.          ResultSet rs=ps.executeQuery();  
  39.   
  40.          while(rs.next()){  
  41.             UserDetails user=new UserDetails();  
  42.             user.setUserID(rs.getString("USERID"));  
  43.             user.setUserName(rs.getString("NAME"));  
  44.             user.setLocation(rs.getString("LOCATION"));  
  45.   
  46.             list.add(user);  
  47.          }  
  48.          con.close();  
  49.       }catch(Exception e){e.printStackTrace();}  
  50.   
  51.          return "success";  
  52.       }  
  53.   
  54.       /*Here you can insert data into the database*/  
  55.       public String save(){  
  56.          mess = "successfully Save";  
  57.          return "success";  
  58.       }  
  59.   
  60.       /*Here you can Update data into the database*/  
  61.       public String update(){  
  62.       System.out.println("update");  
  63.       mess = "successfully Update";  
  64.       return "success";  
  65.    }  
  66.   
  67.    /*Here you can delete data into the database*/  
  68.    @Action(value="delete",results={@Result(name="success",location="index.jsp")})  
  69.    public String delete(){  
  70.       //System.out.println(request.getParameter("time"));  
  71.       System.out.println("delete");  
  72.       mess = "successfully deleted";  
  73.       return "success";  
  74.    }  
  75.   
  76.    public String getMess() {  
  77.       return mess;  
  78.    }  
  79.   
  80.    public void setMess(String mess) {  
  81.       this.mess = mess;  
  82.    }  
  83.   
  84.    public ArrayList<UserDetails> getList() {  
  85.       return list;  
  86.    }  
  87.   
  88.    public void setList(ArrayList<UserDetails> list) {  
  89.       this.list = list;  
  90.    }  
  91.   
  92.    /*@Override 
  93.    public void setServletRequest(HttpServletRequest request) { 
  94.    this.request = request; 
  95.  
  96.    }*/ 
  97.   

Now We create JSP Files
  1. <%@ taglib uri="/struts-tags" prefix="s" %>  
  2. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
  3. pageEncoding="ISO-8859-1"%>  
  4.   
  5.   
  6. <link rel="stylesheet" type="text/css" href="css/jquery-ui.custom.css" />  
  7. <link href="css/Style.css" rel="stylesheet" type="text/css" />  
  8. <link rel="stylesheet" type="text/css" href="css/ui.jqgrid.css" />  
  9.   
  10.   
  11. <script type='text/javascript' src="js/jquery-1.7.2.min.js"></script>  
  12. <script type='text/javascript' src="js/grid.locale-en.js"></script>  
  13. <script type="text/javascript">  
  14. $.jgrid.no_legacy_api = true;  
  15. $.jgrid.useJSON = true;  
  16. </script>  
  17. <script type='text/javascript' src="js/jquery-ui-1.8.14.custom.js"></script>  
  18. <script type='text/javascript' src="js/jquery.jqGrid.min.js"></script>  
  19. <script type='text/javascript' src="js/CRUD.js"></script>  
  20.   
  21. <script type="text/javascript">  
  22.   
  23.     $(document).ready(function() {  
  24.       view();  
  25.     });  
  26.   
  27. </script>  
  28.   
  29. <div style="float: left;" align="center">  
  30.     <div id="userDetailsDiv"></div>  
  31.     <table id="userDetailsTable"></table>  
  32. </div>  
  33.   
  34.   
  35. <script type="text/javascript">  
  36.   
  37.     jQuery("#userDetailsTable").jqGrid(  
  38.     {  
  39.   
  40.       datatype : "local",  
  41.   
  42.        colNames : [ 'UserID''Name''Location'],  
  43.         colModel : [  
  44.         {name : 'userID',index : 'userID',width : 150},  
  45.         {name : 'userName',index : 'userName',width : 300},  
  46.         {name : 'location',index : 'location',width : 150}  
  47.   
  48.   
  49.         ],  
  50.         rowNum : 30,  
  51.         rowList : [ 10, 20, 30 ],  
  52.         pager : jQuery('#userDetailsDiv'),  
  53.         sortname : 'id',  
  54.         viewrecords : true,  
  55.         sortorder : "asc",  
  56.         caption : "User Details ",  
  57.         width : 750,  
  58.         multiselect:true,  
  59.         shrinkToFit : false,  
  60.         height : 260,  
  61.   
  62.   
  63.     });  
  64.   
  65.     jQuery("#userDetailsTable").jqGrid('navGrid','#userDetailsDiv',{edit:false, add:false, del:false, search:false,refresh:false},{ }, { }, { }, { } );  
  66.     jQuery("#userDetailsTable").jqGrid('navButtonAdd''#userDetailsDiv', {  
  67.      caption : "ADD",  
  68.       buttonicon : "ui-icon-plus",  
  69.      title : "Add Row",  
  70.      onClickButton : function() {  
  71.             saveUser();  
  72.       }  
  73.     });  
  74.   
  75.     jQuery("#userDetailsTable").jqGrid('navButtonAdd''#userDetailsDiv', {  
  76.         caption : "Delete",  
  77.         buttonicon : "ui-icon-trash",  
  78.         title : "Delete Row",  
  79.         onClickButton : function() {  
  80.             deleteUser();  
  81.         }  
  82.     });  
  83.     jQuery("#userDetailsTable").jqGrid('navButtonAdd''#userDetailsDiv', {  
  84.         caption : "Update",  
  85.         buttonicon : "",  
  86.         title : "Update Row",  
  87.         onClickButton : function() {  
  88.             updateUser();  
  89.         }  
  90.     });  
  91.   
  92.   
  93. </script> 
History

Keep a running update of any changes or improvements you've made here.