[英]How do I add a POJO to a list using Spring/Thymeleaf form?
I have a very simply object called Move: 我有一个非常简单的对象,称为Move:
public class Move {
private String move;
public String getMove() {
return move;
}
public void setMove(String move) {
this.move = move;
}
}
I also have a move repository(list of all moves): 我也有一个移动存储库(所有移动的列表):
@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;
}
}
Here is my controller: 这是我的控制器:
@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";
}
}
Basically, I want to add the move submitted using the form on the webpage "/addMove" to the list of moves allMoves in the Move Repository. 基本上,我想将使用网页“ / addMove”上的表单提交的移动添加到“移动存储库”中的allMoves移动列表中。 However, it produces a 500 server error whenever I click the submit button on the webpage.
但是,每当我单击网页上的“提交”按钮时,就会产生500个服务器错误。 If I delete the
如果我删除
moveRepository.addMove(move);
from my code then everything works fine, but then of course the move does not get added to the move repository. 从我的代码开始,一切都可以正常工作,但是当然,该移动不会添加到移动存储库中。
I also have my html(using thymeleaf) code posted below for reference: 我也有下面发布的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>
Welcome to SO. 欢迎来到SO。
Change 更改
@Autowired
private MoveRepository moveRepository = new MoveRepository();
to 至
@Autowired
private MoveRepository moveRepository;
The idea is that Spring should handle instantiation of objects through dependency injection. 这个想法是Spring应该通过依赖注入来处理对象的实例化。 Also, make sure that your annotations are picked up with component scanning (either in your XML or Java config).
另外,请确保通过组件扫描(在XML或Java配置中)拾取注释。
Other tips: 其他提示:
It isn't so helpful to have the property named move
in a class of the same name. 将名称为
move
的属性放在同名的类中并不是很有帮助。 More descriptive naming would be much better for the next person reading your code. 对于下一个阅读您的代码的人来说,更具描述性的命名会更好。
If your team allows it, try Project Lombok to get rid of the boilerplate code. 如果您的团队允许,请尝试Project Lombok摆脱样板代码。 Then you can just do:
然后,您可以执行以下操作:
public class Move { @Getter @Setter private String move; }
or even better: 甚至更好:
@Data public class Move { private String move; }
Consider annotating your repository with @Repository
if you're planning to persist to a database. 如果您打算保留到数据库,请考虑使用
@Repository
注释存储库。 This will also give you some exception handling features through Spring. 这还将通过Spring为您提供一些异常处理功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.