java - Spring mvc 不调用 Controller

标签 java spring spring-mvc controller

我是 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/

相关文章:

java - 使用带有 OpenID Connect 提供程序的 spring-security-oauth2 客户端时如何访问 "id_token"和 "refresh_token"?

java - 在 application.properties 中使用 pom.xml 的属性值

java - 使用 ${#authentication.name} 的 Spring boot + Security + Thymeleaf 错误

java - 线程 "main"org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException中出现异常

Java WebStart : How to fix deployJava. js 忽略 Chrome 42 并假设安装了 java

java - Json数据输出不更新 fragment View

java - 你好,springboot项目我想通过spring data JPA为我的crud存储库实现搜索栏操作(按关键字返回所需数据),但是,

Java 继承和 super() 未按预期工作

Spring security,如何基于动态角色限制用户访问某些资源?

将war部署到tomcat时的Spring MVC Servlet