Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用Spring/Thymeleaf表单将POJO添加到列表中?_Java_Spring_Thymeleaf - Fatal编程技术网

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

Java 如何使用Spring/Thymeleaf表单将POJO添加到列表中?,java,spring,thymeleaf,Java,Spring,Thymeleaf,我有一个非常简单的对象叫做移动: public class Move { private String move; public String getMove() { return move; } public void setMove(String move) { this.move = move; } } 我还有一个移动存储库(所有移动的列表): 从我的代码来看,一切都很好,但是当然,移动不会被添加到移动存储库中 我还将我的html(使用thymeleaf)代码发布在

我有一个非常简单的对象叫做移动:

public class Move {

private String move;

public String getMove() {
    return move;
}

public void setMove(String move) {
    this.move = 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>

添加移动
加把劲
移动:

欢迎来到SO

改变

@Autowired
private MoveRepository moveRepository = new MoveRepository();

其思想是Spring应该通过依赖项注入来处理对象的实例化。另外,确保通过组件扫描(在XML或Java配置中)获取注释

其他提示:

  • 将名为
    move
    的属性放在 同名。更具描述性的命名方式对用户来说会更好 下一个阅读代码的人

  • 如果您的团队允许,请尝试 去掉样板代码。然后你可以做:

    public class Move {
    
       @Getter
       @Setter
       private String move;
    
    } 
    
    或者更好:

    @Data
    public class Move {
    
       private String move;
    
    } 
    
  • 如果您计划持久化到数据库,请考虑使用
    @repository
    注释您的存储库。这还将通过Spring为您提供一些异常处理特性


我可以使用您的代码。请,您可以添加控制台输出吗?
@Autowired
private MoveRepository moveRepository = new MoveRepository();
@Autowired
private MoveRepository moveRepository;
public class Move {

   @Getter
   @Setter
   private String move;

} 
@Data
public class Move {

   private String move;

}