【发布时间】:2017-04-18 19:05:07
【问题描述】:
我有一个非常简单的对象,叫做 Move:
public class Move {
private String move;
public String getMove() {
return move;
}
public void setMove(String move) {
this.move = move;
}
}
我还有一个移动存储库(所有移动的列表):
@Component
public class MoveRepository {
private List<Move>allMoves;
public void addMove(Move move){
allMoves.add(move);
}
public MoveRepository() {
this.allMoves = new ArrayList<>();
}
public void setAllMoves(List<Move> allMoves) {
this.allMoves = allMoves;
}
public List<Move> getAllMoves(){
return allMoves;
}
}
这是我的控制器:
@Controller
public class MoveController {
@Autowired
private MoveRepository moveRepository = new MoveRepository();
@GetMapping("/moveList")
public String listMoves (ModelMap modelMap){
List<Move> allMoves = moveRepository.getAllMoves();
modelMap.put("moves", allMoves);
return "moveList";
}
@GetMapping("/addMove")
public String addMoveForm(Model model) {
model.addAttribute("move", new Move());
return "addMove";
}
@PostMapping("/addMove")
public String addMoveSubmit(@ModelAttribute Move move) {
moveRepository.addMove(move); //Producing an error
return "moveAdded";
}
}
基本上,我想将使用网页“/addMove”上的表单提交的移动添加到移动存储库中的所有移动列表中。但是,每当我单击网页上的提交按钮时,它都会产生 500 服务器错误。如果我删除了
moveRepository.addMove(move);
根据我的代码,一切正常,但当然移动不会添加到移动存储库中。
我的 html(使用 thymeleaf)代码也贴在下面以供参考:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Add Move</title>
</head>
<body>
<h1>Add a move</h1>
<form action = "#" th:action="@{/addMove}" th:object="${move}" method = "post">
<p>Move: <input type = "text" th:field="*{move}"/></p>
<p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>
</body>
</html>
【问题讨论】:
-
它适用于我的代码。请问,你能添加控制台输出吗?