I have the JavaScript on web page and Java Thread running. All is in spring-MVC framework. My idea is that I have in jsp site JavaScript code. The controller make a game thread which be in java. But I need send data to my game thread. So I want to use controller for that. So I want from JavaScript script which is in site to call the controller for updated data and get a response from it.
So I think to inputs code like this in my JavaScript code on site to send JSON data to controller.Example code to see idea:
$("#btnPostGlEntry").click(function () {
var glEntries = '{"glEntries":[{"generalLedgerId":"1"},{"accountId":"4"},{"amount":"344.44"},{"description":"Test Entry"},{"debit":"Yes"}]}';
$.ajax({
type: "POST",
contentType: "application/json",
dataType: "json",
url: contextPath + "/generalLedger/journalEntries/form",
data : JSON.stringify(glEntries),
success: function(data) {
alert("Success!!!");
},
error: function (jqXHR, textStatus, errorThrown) {
alert(jqXHR + " : " + textStatus + " : " + errorThrown);
}
});
});
JSON - Spring MVC : How to post json data to spring MVC controller
or
var PersonText = '["firstname":"John", "lastname":"Someone"]';
$.ajax({
type : 'POST',
url : "/addnewperson.do",
data : {'personModel' : personText},
datatype : 'json',
success : function(data) {
var obj = jQuery.parseJSON(data);
}
});
How to call Spring MVC from Javascript?
So the controller need some think like this:
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
public class TestController {
@RequestMapping(value="/test.htm")
public @ResponseBody Integer add(@RequestParam(value="inputNumber1", required=true) Integer inputNumber1,
Model model) {
System.out.println(inputNumber1);
return inputNumber1;
}
}
Unable to send data from controller to javascript
So I have a few questions. First. JavaScript is a game code(graphic display in browser) which is all time working. I want to calling controller and transfer data to it. And receive data from controller(physic engine). So I don't want any reload of page from controller action. Second. Is good idea making communication by controller in this case?