我是 spring 框架的新手,我只是创建自己的项目来捕捉这个想法。流程是登录->客户列表->选择一位客户->宠物列表。 我在第一页上有一个带有表单的按钮,用于添加客户并且它工作正常。但是,与宠物页面中用于添加宠物的第二个按钮类似的代码不起作用。当我尝试保存新宠物时,它返回 HTTP 400 – 错误请求,控制台中没有任何内容
pet-form.jsp:
<form:form action="${pageContext.request.contextPath}/pet/savePet"
modelAttribute="pet" method="POST">
<!-- need to associate this data with customer id -->
<form:hidden path="owner.id" />
......
<td><input type="submit" value="Save" class="save" /></td>
</tr>
</tbody>
</table>
PetController.java
@Controller
@RequestMapping("/pet")
public class PetController {
.....
@PostMapping("/savePet")
public String savePet(@ModelAttribute("pet") Pet thePet) {
System.out.println("New Pet " + thePet.toString());
thePet.setOwner(customerService.getCustomer(thePet.getOwner().getId()));
System.out.println("Controller" + thePet);
// save the customer using our service
petService.savePet(thePet);
return "redirect:/pet/showListPets";
}
知道发生了什么吗?
提前致谢
最佳答案
我解决了我的问题,但我不明白为什么,所以,如果有人好心地解释我..更好:) 问题已解决,将 BindingResult 参数添加到 Controller 方法中:
@PostMapping("/savePets")
public String savePets(@ModelAttribute("pet") Pet thePet, BindingResult result) {
谢谢
关于java - Spring mvc 不调用 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61083842/