简体   繁体   English

如何使用Spring / Thymeleaf表单将POJO添加到列表中?

[英]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.

相关问题 如何使用百里香叶和弹簧用列表填充下拉列表 - How do I populate a drop down with a list using thymeleaf and spring 使用Thymeleaf的表单不适用于POJO - Form using Thymeleaf doesn't work with POJO SPRING / THYMELEAF:如何在表单中填写列表并将另一行追加到表单? - SPRING/THYMELEAF: How to fill a list in form and append another line to the form? 如何从HTML / thymeleaf到Spring控制器访问表单输入? - How do I access form input from HTML/thymeleaf to Spring controller? Thymeleaf + Spring MVC - 如何从表单上的列表或枚举中保留值? - Thymeleaf + Spring MVC - How to persist values from a list or enum on a form? 我如何使用angular js将模型属性POJO对象发布到spring控制器 - How do i post model attribute POJO object to spring controller using angular js 在Spring中,如何从配置中创建具有默认值的POJO? - In Spring, how do I create a POJO with a default value from configuration? 在将Java和Spring与thymeleaf结合使用时,如何包含css文件? - How do I include my css file, while using Java and Spring with thymeleaf? 我如何获得清单 <Pojo1> 它存在于其根Pojo中,该Pojo也作为列表返回 <Pojo> - How do I get List<Pojo1> which is present in its root Pojo, which also returns as a List<Pojo> 如何使用Spring MVC和Thymeleaf添加静态文件 - How to add static files using Spring MVC and Thymeleaf
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM