Connect JavaScript to Java -
i have javascript on web page , java therad running. in spring-mvc framwork. idea have in jsp site javascript code. controller make game therad wich in java. need send data game thread. want use controler that. want javascript script wich in site call controler update data , responde it.
so think inputs code in javascript code on site send json data controller.example code 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 post json data 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 call spring mvc javascript?
so controller need think 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 send data controller javascript
so have few questions. first. javascript game code(graphic display in browser) wich time working. want calling controller , transfer data it. , receive data controller(physic engine). dont want reload of page controller action. second. idea making comunication controller in case?
it's fine use transfer data between client , controller without reloading page. i.e. using ajax , @responsebody tag in controller.
alternatively, can bypass controller altogether , go straight defined remote methods (e.g. in service layer) direct transfer of data using dwr (direct web remoting. direct transfer using ajax bypass mvc framework.
Comments
Post a Comment