Thursday, November 22, 2012

Ajax Data Submit On Spring Applications


In this lesson My target is to demonstrate simple ajax data submitting to the backend in a Spring application. This will help you to develop more complex ajax data submitting as well.

In future I will tell you how to bring multiple data objects to the backend(Spring controller level) using Spring DTO Objects and Json.

Let’s begin with usual html

Customer Name: <input type=”textname=” customerNameTextid=” customerNameText” />

Now Let’s add some jquery to this,

var customerNameValue = $("#customerNameText").val();
        jQuery.ajaxSettings.traditional = true;
        $.post('customerDataSave.htm',{customerName:customerNameValue},function(data){
            if(data == "SUCCESS"){
                //do anything when success data submitting              
            }
        });


Now front end work is done. Let’s move on to Spring application. I hope you are familiar with Spring applications. It uses MVC architecture. Below code is a good example for a controller of a Spring web application.

        @RequestMapping(value = "/customerDataSave")
        public @ResponseBody String customerDataSave(@RequestParam("customerName") String customerName,
        HttpServletRequest request){
            
            //do anything you like using customerName 
        
        }


This is a very simple application. Hope you all can understand. Write me or comment on this for more details.. Wish you success… Happy ajax…!!!

No comments:

Post a Comment