April 21, 2015

To call spring controller from ajax jquery in java spring

Ajax Jquery call to java spring controller

Description : The program will helps you to call java spring controller  from ajax jquery. To know spring configuration please refer this link. The link shows a  Hello world Model View Controller java spring example.


jquery api needed

<script type="text/javascript" src="jquery-1.3.2.js"></script>
<script type="text/javascript" src="jqueryApps.js"></script>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>

These jquery api should be paste in your WEB-INF folder.


function callJavaSpringController(){
var jqueryData = 'java spring mvc';

       type: "GET",
       url: "callcontroller.html",
       data: { message : jqueryData }
     }).done(function( msg ) {
       alert( "Data Saved: " + msg );


JavaSpringSampleController  class
package net.cfed.oms.controller;

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

import net.cfed.oms.model.SendMailModel;
import net.cfed.oms.service.EmployeeService;
import net.cfed.oms.serviceImpl.EmployeeServiceImpl;

import org.apache.catalina.connector.Request;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

 * @author free source codes
public class JavaSpringSampleController {

public ModelAndView helloWorld(HttpServletRequest request, HttpServletResponse response) {

String message = "Welcome  to";
                message = message + request.getParameter("message");
return new ModelAndView("index", "message", message);

Here is a sample for ajax jquery call to servlet , click on the link


Facebook comments