我有一个带有棋盘的jsp来玩tic tac toe。每次点击它之后,我将点击的字段发送给Spring控制器。然后,我找到了最好的计算机移动,并显示一个移动我和计算机的板。问题是,在第一次移动后,单击另一个字段不会执行任何操作,脚本只能运行一次。并且在URL中更改参数会手动更改我的移动位置,但不会再生成另一个。
带有董事会的表格代码(“game.jsp”):
%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<html>
<head>
<title>Game</title>
</head>
<body>
<script>
function redirect(x, y) {
window.location = "http://localhost:8080/taskNine/move?x=" + x + "&y=" + y;
}
</script>
<FORM NAME="tic" method="post" accept-charset=utf-8>
<INPUT TYPE="button" NAME="sqr1" class="tictac" value="${board[0][0]}" onClick="if(document.tic.sqr1.value == '') {document.tic.sqr1.value = 'X';redirect(0, 0); }">
<INPUT TYPE="button" NAME="sqr2" class="tictac" value="${board[0][1]}" onClick="if(document.tic.sqr2.value == '') {document.tic.sqr2.value = 'X';redirect(0, 1); }">
<INPUT TYPE="button" NAME="sqr3" class="tictac" value="${board[0][2]}" onClick="if(document.tic.sqr3.value == '') {document.tic.sqr3.value = 'X';redirect(0, 2); }"> <br />
<INPUT TYPE="button" NAME="sqr4" class="tictac" value="${board[1][0]}" onClick="if(document.tic.sqr4.value == '') {document.tic.sqr4.value = 'X';redirect(1, 0); }">
<INPUT TYPE="button" NAME="sqr5" class="tictac" value="${board[1][1]}" onClick="if(document.tic.sqr5.value == '') {document.tic.sqr5.value = 'X';redirect(1, 1); }">
<INPUT TYPE="button" NAME="sqr6" class="tictac" value="${board[1][2]}" onClick="if(document.tic.sqr6.value == '') {document.tic.sqr6.value = 'X';redirect(1, 2); }"> <br />
<INPUT TYPE="button" NAME="sqr7" class="tictac" value="${board[2][0]}" onClick="if(document.tic.sqr7.value == '') {document.tic.sqr7.value = 'X';redirect(2, 0); }">
<INPUT TYPE="button" NAME="sqr8" class="tictac" value="${board[2][1]}" onClick="if(document.tic.sqr8.value == '') {document.tic.sqr8.value = 'X';redirect(2, 1); }">
<INPUT TYPE="button" NAME="sqr9" class="tictac" value="${board[2][2]}" onClick="if(document.tic.sqr9.value == '') {document.tic.sqr9.value = 'X';redirect(2, 2); }"> <br />
</form>
</body>
</html>
这是一个控制器代码段:
@RequestMapping(value = "/move", method = RequestMethod.GET)
public String fieldMove(@RequestParam("x") int x, @RequestParam("y") int y, ModelMap model) {
gameService.move(x, y, model);
return "game";
}
答案 0 :(得分:0)
RequestParam的名称必须与输入的名称匹配:
@RequestMapping(value = "/move", method = RequestMethod.GET)
public String fieldMove(@RequestParam("sqr1") int sqr1, @RequestParam("sqr2") int sqr1, ..., ModelMap model) {
// Do something
return "game";
}