Announcement Announcement Module
Collapse
No announcement yet.
Connect JavaScript to Java using spring-MCV Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Connect JavaScript to Java using spring-MCV

    I have the javascript on web page and Java Therad running. All is in spring-MVC framwork. My idea is that I have in jsp site javascript code. The controller make game therad wich be in java. But I need send data to my game thread. So I want to use controler for that.
    So I want from javascript script wich is in site call controler for update data and get responde 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:
    Code:
        $("#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);
                }
            });
        });
    http://stackoverflow.com/questions/1...mvc-controller

    or

    Code:
      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);
        }
        });
    http://stackoverflow.com/questions/1...rom-javascript

    So the controller need some think like this:

    Code:
      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;
            }
        }
    http://stackoverflow.com/questions/5...-to-javascript

    So I have a few questions.
    First. Javascript is a game code(graphic display in browser) wich is all time working. I want to calling controller and transfer data to it. And receive data from controller(physic engine). So I dont want any reload of page from controller action.
    Second. Is good idea making comunication by controller in this case?
Working...
X