我正在使用thymeleaf:
创建一个输入页面insertText.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Handing Form Submission</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Form</h1>
<form action="#" th:action="@{/insertText}" th:object="${text}" method="post">
<p th:text="'id: ' + ${text.id}" />
<p th:text="'parent_id' + ${text.parent_id} ?: 'original'" />
<p th:text="'id: ' + ${text.name}" />
<p th:text="'content: ' + ${text.operation}" />
<p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>
</body>
</html>
在parent_it字段中,如果它是原始文本,我想输入null但我得到一个错误:
Field error in object 'textTable' on field 'parent_id': rejected value [null];
codes [typeMismatch.textTable.parent_id,typeMismatch.parent_id,typeMismatch.java.lang.Integer,typeMismatch];
arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [textTable.parent_id,parent_id]; arguments []; default message [parent_id]];
default message [Failed to convert property value of type 'java.lang.String' to required type 'java.lang.Integer' for property 'parent_id';
nested exception is java.lang.NumberFormatException: For input string: "null"]]
虽然在post页面我添加了零值处理:
result.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Handing Form Submission</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Result</h1>
<p th:text="'id: ' + ${text.id}" />
<p th:text="'content: ' + ${text?.parent_id}" />
<p th:text="'id: ' + ${text.name}" />
<p th:text="'content: ' + ${text.operation}" />
<a href="/greeting">Submit another message</a>
</body>
</html>
也许有办法让这成为可能?
我的实体textTablt
package com.example.HiberTest.Entities;
@Entity
@Table(name = "textTable")
public class textTable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@Column(name = "parent_id")
private Integer parent_id;
@NotBlank
@Size(min=1, max=128)
@Column(name = "name")
private String name;
@NotBlank
@Size(min=1, max=128)
@Column(name = "operation")
private String operation;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getParent_id() {
return parent_id;
}
public void setParent_id(Integer parent_id) {
this.parent_id = parent_id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getOperation() {
return operation;
}
public void setOperation(String operation) {
this.operation = operation;
}
@Override
public String toString(){
return String.format("id = %o" +
", parent_id = %o, " +
"name = %s, operation = %s ",
id,parent_id,name,operation);
}
}
我的控制方法:
package com.example.HiberTest.Controller;
import java.util.List;
@Controller
public class TextController {
@Autowired
daoRepository dao;
@GetMapping("/insertText")
public String insertText(Model model){
//List<textTable> list =dao.findAll();
//model.addAttribute("texts",list);
model.addAttribute("text", new textTable());
return "insertText";
}
@PostMapping("/insertText")
public String getAllText(@ModelAttribute textTable texts, Model model){
//List<textTable> list =dao.findAll();
model.addAttribute("text", texts);
return "result";
}
}
我很乐意帮助弄清楚如何输入空值,以便正确处理
你的变量parent_id在类textttable的类型是整数,但你正试图插入字符串在它。你试图在字符串'parent_id'中添加整数id,然后存储在数据库中,这就是为什么你得到错误。