I'm new using spring framework and I'm just creating my own project in order to catch the idea. The flow of this is loging -> customer list -> select one cusstomer -> Pet list. I have a button on first page with a form in order to add a customer and it is working properly. However, similar code to a second button in pet page to add a pet is not working. When I try to save the new pet, it is returning HTTP 400 – Bad Request and nothing in the console
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";
}
Any ideas what is happening?
Thanks in advance
I resolved my question, however I dont understand why, so, if someone is so kind to explain me.. better:) The problem was solved adding BindingResult paramenter to the controller method:
@PostMapping("/savePets")
public String savePets(@ModelAttribute("pet") Pet thePet, BindingResult result) {
Thanks
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.