Spring MVC 4 and Simple Ajax Call

To understand this tutorial, I assume that you are already aware with Spring MVC and how it works. I will demonstrate here a simple Ajax call in Spring MVC where form data are posted and sent to the controller. The output of the ajax call is plain string text.

Step 1: Create Form

Step 2: Add Ajax Script

Step 3: Create Spring MVC Controller

Step 4: Create Model

Step 1 & 2 : Create Form & Add Ajax Script

Screenshots:

Screen 1: Loading the form and filling information

1_springmvc_ajax

Screen 2: Displaying data after clicking on submit

2_springmvc_ajax

Create a jsp file with name studentForm.jsp and add the following code:

<%@taglib uri=”http://www.springframework.org/tags/form” prefix=”form”%>
<html>
<head>
<title>Spring MVC Form Handling</title>
</head>
<body>

<h2>Student Information</h2>
<form:form id=”form” method=”POST” action=”addStudent.htm” >
<table>
<tr>
<td><form:label path=”name”>Name</form:label></td>
<td><form:input path=”name” /></td>
</tr>
<tr>
<td><form:label path=”age”>Age</form:label></td>
<td><form:input path=”age” /></td>
</tr>
<tr>
<td><form:label path=”id”>id</form:label></td>
<td><form:input path=”id” /></td>
</tr>
<tr>
<td colspan=”2″>
<input type=”submit” value=”Submit” id=”submitButton” />
</td>
</tr>
</table>
</form:form>

<div id=”result”>
</div>

<script src=”https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js”> </script>
<script>
$(function(){

$(“#form”).submit(function(){

formData = $(“#form”).serialize();
alert(formData);
$.ajax({
type: “POST”,
url: “addStudent.htm”,
data : formData,
success : function(callback){
alert(callback);
$(“#result”).html(callback);
},
error : function(){
$(“#result”).html(“Error!”);
}
});

return false;
});
});
</script>

</body>
</html>

 

Step 3: Create Spring Controller

Create a java file with name studentController.java and add the following code:
package com.StudentController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
//import org.springframework.web.portlet.ModelAndView;
@Controller
public class StudentController {
@RequestMapping(value = “/student”, method = RequestMethod.GET)
public ModelAndView student() {
return new ModelAndView(“studentForm”, “command”, new Student());
}

@RequestMapping(value = “/addStudent”, method = RequestMethod.POST)
public @ResponseBody String addStudent(@ModelAttribute(“SpringWeb”)Student student) {

String studentStr = “{name:”+student.getName()+”,age:”+student.getAge()+”,id:”+student.getId()+”}”;

return studentStr;
}

}

 

The first method in the controller loads the form and the later one is executed when the submit button is clicked.

Step 4: Create Model

In this step data binding takes place which helps to bind the form data and transact via the controller. You must have already done this step while implementing spring custom controller. Create file Student.java and paste the following code:
package com.StudentController;

public class Student {
private Integer age;
private String name;
private Integer id;

public void setAge(Integer age) {
this.age = age;
}
public Integer getAge() {
return age;
}

public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}

public void setId(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
}

Finally, run the project and call the student form by typing localhost:8080/project_name/student at the address bar of your browser. Fill some data into the form fields and there your are.

 

 

How do I use Java with the Google Chrome browser?

 

NPAPI support by Chrome

The Java plug-in for web browsers relies on the cross platform plugin architecture NPAPI, which has long been, and currently is, supported by all major web browsers. Google announced in September 2013 plans to remove NPAPI support from Chrome by “the end of 2014”, thus effectively dropping support for Silverlight, Java, Facebook Video and other similar NPAPI based plugins.

[dfads params=’groups=-1′]

 

 

 

 Enabling NPAPI in Chrome Version 42 and later

As of Chrome Version 42, an additional configuration step is required to continue using NPAPI plugins.

  1. In your URL bar, enter:
    chrome://flags/#enable-npapi
  2. Click the Enable link for the Enable NPAPI configuration option.
  3. Click the Relaunch button that now appears at the bottom of the configuration page.

Java plug-in needs permission

 

If you see a message within Chrome that says Java(TM) needs your permission to run, you will need to respond to the Chrome options in order to run plug-ins on the page. Options vary according to the version of Chrome.

Chrome plug-in blocked icon (in address bar)

Look for the blocked plug-in icon in the Chrome address bar. Clicking on the icon will display Plug-ins were blocked on this page and plug-in options

  • Click Always allow plug-ins on [name of site] to allow plug-ins (including Java) to run on all this site’s pages.
  • Click Run all plug-ins this time to allow the plug-in (including Java) content on the page to run only until you leave this page.
  • Click Continue blocking plug-ins to prevent plug-ins from running on the page.

In addition, you can manage permissions on a site basis through the Manage plug-in blocking option.

Chrome message bar

  • Click Run this time to allow the Java content on the page to run only until you leave this page.
  • Click Always run on this site to allow Java to run on all this site’s pages. You will not see this message again for pages on this site.

Additional plug-in required

If you see a message bar within Chrome that says Java(TM) is required to display some elements on this page, click on Install plug-in in the Chrome message bar, or download from java.com.

[dfads params=’groups=-1′]

Plug-in out of date

If you see a message bar within Chrome that says Java(TM) was blocked because it is out of date, click on Update plug-in in the Chrome message bar to get the latest Java.

Reference:

https://java.com/en/download/faq/chrome.xml