1

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?

1
  • you need to access database and making ajax calls to controller actions is a way to do it. make the call and process the data in javascript lines, that won't cause page reload. and please delete this question. Commented May 9, 2013 at 21:47

1 Answer 1

1

It's fine to use this to transfer data between the client and the controller without reloading the page. i.e. using AJAX and the @ResponseBody tag in your Controller.

Alternatively, you can bypass the Controller altogether and go straight to defined remote methods (e.g. in the Service layer) for direct transfer of data using DWR (Direct Web Remoting. This is good for direct transfer using AJAX but it does bypass the MVC framework.

http://directwebremoting.org/dwr/index.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.