Ads Inside Post

Search in This Blog

Ajax in struts

How to use ajax in struts and hibernate.

Ajax is very important in website coding.
Here I am demonstrating how to use jquery ajax in struts and hibernate.


struts-config.xml
--------------------------
<action input="/" path="/amittest" scope="request" type="com.test.actions.AmitTestAction" parameter="method">
<forward name="success" path="/Webs/Input/amittest.jsp"/>
</action>
====================================================


query.hbm.xml
-----------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <sql-query cache-mode="ignore" cacheable="false" flush-mode="always" name="chkBook"> SELECT COUNT(*) FROM book_master WHERE code_id=:code AND serial_id>=:serial </sql-query>
</hibernate-mapping>
====================================================

MyUtilDao.java
------------------------------
package dao;
import org.hibernate.CacheMode;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import plugins.HibernateSessionFactory;
public class MyUtilDao {
    public int chkBook(int code,int serial){
        Session ssn = null;
        ssn = HibernateSessionFactory.getSession();
        ssn.setCacheMode(CacheMode.GET);
        Query qry = ssn.getNamedQuery("chkBook");
        qry.setParameter("code", code);
        qry.setParameter("serial", serial);
        Object tot = qry.uniqueResult();
        int total = 0;
        if (tot != null) {
            total = Integer.parseInt(tot.toString());
        } else {
            total = 0;
        }
        return total;
    }
 
}
====================================================

AmitTestAction.java
-------------------------
package mytest.actions;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import dao.MyUtilDao;
import plugins.HibernateSessionFactory;

public class AmitTestAction  extends org.apache.struts.actions.DispatchAction {
     private final static String SUCCESS = "success";
     public ActionForward showForm(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
       
         MyUtilDao myUtiDaoObj=new MyUtilDao();
       
         String ffor= request.getParameter("ffor");
         if(ffor.compareTo("chkBook")==0) {
            int code= Integer.parseInt(request.getParameter("code"));
            int erial= Integer.parseInt(request.getParameter("serial"));
            int total=myUtiDaoObj.chkBook(code,serial);
            request.setAttribute("total", total);
            request.setAttribute("ffor", "chkBook");
         }
         return mapping.findForward(SUCCESS);
      }
}
====================================================
amittest.jsp
<%
if(request.getAttribute("ffor")!=null) {
    if(request.getAttribute("ffor").toString().compareTo("chkBook")==0) {
        out.print(request.getAttribute("total"));
    }
}
%>
====================================================
BookAdd.jsp

<input type="button" name="btn1" value="Button" onclick="dotest();"/>
<html:button property="button" value="Save" onclick="do_save()"/>
<script>

 var code=222;
var serial=111;
function do_save() {
        var ajaxans="";
        $.ajax({
                    url: "amittest.do?method=showForm",
                    type: "POST",
                    async: false,
                    cache: false,
                    data:   { 'ffor':'chkBook',
                              'code':code,
                              'serial':serial},
                    success: function(html) {
                             ajaxans=html;
                            }
                }
             );
       if(ajaxans=="1" || parseInt(ajaxans)==1) {
           alert("This is already exists");
           return;
       }    
        var r=confirm("Do you want to save?");
        if (r==true) {
            document.forms[0].action="BookMasterAdd.do?method=processForm";
            document.forms[0].submit();
        }
    }

   function dotest() {
       /*$.post("http://localhost:8080/mytest/amittest.do?method=showForm",{'method':'showForm'},function(ans) {
alert(ans);
});*/
      /*// done
       *$.post("amittest.do?method=showForm",{'method':'showForm'},function(ans) {
alert(ans);
});*/
      /* done
       * $.post("amittest.do?method=showForm",function(ans) {
alert(ans);
});*/
      /* $.get("amittest.do?method=showForm",function(ans) {
alert(ans);
});*/
     
      /* $.get("amittest.do?method=showForm",{'val1':'Hi'},function(ans) {
alert(ans);
});*/  
   
      /*   $.post("amittest.do?method=showForm",{'val1':'Hi'},function(ans) {
alert(ans);
});*/
    /*  $.post("amittest.do?method=showForm",{'ffor':'chkExistReceiptBookMaster',
                                            'zone_code':'8004500',
                                            'frm_serial':'11'
                                           },function(ans) {
alert(ans);
});*/
}
</script>
====================================================