[英]Thymeleaf form passing all but one inputs to java controller
My thymeleaf form has three inputs, when I submit it my controller is receiving the second and the third, but not the first one (first input is null) 我的百里香叶形式有三个输入,当我提交它时,我的控制器正在接收第二个和第三个,但没有第一个(第一个输入为空)
I have similar forms and method to add records to a MySQL database which work with no problems. 我有类似的形式和方法可以将记录添加到MySQL数据库中,而不会出现问题。 I have diplayed the two first inputs in my controller, first (Id of the entity) is null, second is well informed
我已经在控制器中显示了前两个输入,第一个(实体的ID)为null,第二个消息灵通
Here is my form: addDepartment.html 这是我的表格:addDepartment.html
<br/>
<form th:action="@{/university/addDepartment}"
th:object="${department}" method="POST">
Department Name
<input type="text" th:field="*{deptName}" />
<br/>
Department building
<input type="text" th:field="*{building}" />
<br/>
Budget
<input type="number" th:field="*{budget}" th:value=0/>
<br/>
<input type="submit" value="Add" />
</form>
<!-- Check if errorMessage is not null and not empty -->
<div th:if="${errorMessage}" th:utext="${errorMessage}"
style="color:red;font-style:italic;">
...
</div>
And my controller 还有我的控制器
// ADD a department to the database
// Show the addDepartment page
@RequestMapping(value= { "university/addDepartment" }, method = RequestMethod.GET)
public String showAddDepartment(Model model) {
Department department = new Department();
model.addAttribute("department", department);
return "addDepartment";
}
// Add the department to the database
@RequestMapping(value = { "university/addDepartment" }, method = RequestMethod.POST)
public String saveDepartment(Model model, //
@ModelAttribute("department")
@Validated Department department) {
System.out.println("DEPARTMENT: " + department.toString());
String deptName = department.getDeptName();
System.out.println("DEPARTMENT NAME: " + deptName);
String building = department.getBuilding();
System.out.println("BUILDING: " + building);
int budget = department.getBudget();
if (deptName != null && deptName.length() > 0 //
&& building != null && building.length() > 0 //
&& budget >= 0) {
Department newDepartment = new Department(deptName, building, budget);
departmentrepo.save(newDepartment);
return "redirect:/university/departmentList";
}
Object errorMessage = "All fields are mantatory";
model.addAttribute("errorMessage", errorMessage );
return "addDepartment";
}
Here is the Department class: 这是部门课程:
@Entity
@Table(name="department") // This annotation is needed only if the
table has a different name to this class
@EntityListeners(AuditingEntityListener.class)
public class Department {
@Id
private String deptName;
private String building;
private int budget;
//Constructors
public Department() {
}
public Department(String deptName, String building, int budget) {
this.deptName = deptName;
this.building = building;
this.budget = budget;
}
...GETTERS and SETTERS
The deptName is always null, but the other two inputs are ok deptName始终为null,但其他两个输入都可以
DEPARTMENT NAME: null BUILDING: Main 部门名称:null建筑物:主要
UPDATE: I think I found the solution, but if someone can explain the reason... I just passed the deptName in a @RequestParam annotation. 更新:我想我找到了解决方案,但是如果有人可以解释原因...我只是在@RequestParam批注中传递了deptName。
@RequestMapping(value = { "university/addDepartment" }, method =
RequestMethod.POST)
public String saveDepartment(Model model, //
@ModelAttribute("department") Department department,
@RequestParam("deptName") String deptName) {
//String deptName = department.getDeptName();
System.out.println("DEPARTMENT NAME: " + deptName);
String building = department.getBuilding();
System.out.println("BUILDING: " + building);
int budget = department.getBudget();
if (deptName != null && deptName.length() > 0 //
&& building != null && building.length() > 0 //
&& budget >= 0) {
Department newDepartment = new Department(deptName, building, budget);
departmentrepo.save(newDepartment);
return "redirect:/university/departmentList";
}
Object errorMessage = "All fields are mantatory";
model.addAttribute("errorMessage", errorMessage );
return "addDepartment";
}x`
There are a few issues with this code, but the first thing to try is removing the @Id
annotation from your deptName
property. 这段代码有一些问题,但是首先要尝试的是从
deptName
属性中删除@Id
注释。 Create a Long
(or Integer
) id
property and annotate this instead. 创建一个
Long
(或Integer
) id
属性,并对其进行注释。 You can more simply call the department name property name
too. 您也可以更简单地调用部门名称属性
name
。
You will also want to make sure that you have the getters and setters done correctly, or more advisable - just use Project Lombok to get rid of extra text and potential for mistakes. 您还需要确保正确完成吸气器和设置器,或者更可取-只需使用Project Lombok消除多余的文本和潜在的错误。
@Entity
@Table
@Data //see Project Lombok
public class Department {
@Id
private Long id;
private String name;
private String building;
private int budget; //I'd consider making this more precise by using BigDecimal
}
For your controller, let's more simply try this to see whether it is still breaking. 对于您的控制器,让我们更简单地尝试一下,看看它是否仍在中断。
@GetMapping("/university/addDepartment")
public String showAddDepartment(Model model) {
model.addAttribute("department", new Department());
return "addDepartment";
}
@PostMapping("/university/addDepartment")
public String saveDepartment(@ModelAttribute("department") Department department) {
System.out.println("name="+department.getName()); //use a logger if you have one available
return "addDepartment";
}
In your form, you only need the th:
notation if you want Thymeleaf to evaluate the logic. 在您的表单中,如果您希望Thymeleaf评估逻辑,则仅需要
th:
表示法。 So you can change th:value=0
to value=0
if you really intend this. 因此,如果您确实打算这样做,可以将
th:value=0
更改为value=0
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.