spring - How to POST to database through URL -


I am writing a web application in Spring / Hibernate that handles basic voting functionality / vote / {gameId} There is a link that will add that vote to the database for that specific ID. I'm actually in the form of a loss, though how to complete it. Here I have tried my controller: @RequestMapping (value = "/ vote / {id}", method = RequestMethod.POST) public string addVote (@PathVariable ("gameId") integer game ID) Vote vote = new vote (); Vote.setGameId (GameID); VoteService.addVote (vote); Return "redirect: /games/wanted.html"; }

This is where the link appears in a jsp:

  & lt; C: If test = "$ {! Empty games}" & gt; & Lt; Table & gt; & Lt; TR & gt; & Lt; Th & gt; Game title & lt; / Th & gt; & Lt; Th & gt; Votes & lt; / Th & gt; & Lt; Th & gt; & Nbsp; & Lt; / Th & gt; & Lt; / TR & gt; & Lt; C: forEach item = "$ {games}" var = "game" & gt; & Lt; TR & gt; & Lt; Td> & Lt; C: Out value = "$ {game.title}" /> gt; & Lt; / Td> & Lt; TD & gt; Placeholder & lt; / TD & gt; & Lt; Td> & Lt; A href = "vote / $ {game.id}" & gt; Vote! & Lt; / A & gt; & Lt; / Td> & Lt; / TR & gt; & Lt; / C: foreach & gt; & Lt; / Table & gt; & Lt; / C: If & gt;   

When I try this, I get a 404 error. No insight would be great.

In this way you can with plain javascript:

  var url = "Vote"; Var parameters = "id = 1"; Http.open ("post", url, true); // Request the appropriate header information with http.setRequestHeader ("content-type", "app / x-www-form-urxended"); Http.setRequestHeader ("Content-Length", Parameters.Lamp); Http.setRequestHeader ("Connection", "Closed"); Http.onreadystatechange = function () {// Call a function when the status changes (http.readyState == 4 & amp; http.status == 200) {Warning (http.responseText); }} Http.send (params);   

You need to click it on your link.

On the other hand, it is very easy, for example, you use it:

Something like this will happen for your special case:

  $ Post ("vote", {id: "1"});   

or the entire jQuery reply (Remember to change #linkid with your own ID):

  $ (document) .ready (function () {// make this page load / run for handlers .ready () to $ ('# linkid'). Click (function) (// it looks for your & lt; a & gt; And onclicks, you can also search the CSS class by type of tag $ .post ("vote", {id: "1"}); Return return; // It is important to follow the link Not done}};});    

Comments

Popular posts from this blog

java - NullPointerException for a 2d Array -

python - Assemble mpeg file unable to play in mediaplayer -

c# - NameSpace Manager or XsltContent to parse aspx page -