java - 一种表单中的多个提交按钮

标签 java spring thymeleaf

我有一个带有请求映射和 View 的 Controller ,其中两个按钮“保存”和“删除”位于何处,但它们不执行任何操作。我尝试调试它<但什么也没有

我使用了带有 thymleaf 的 Spring mvc,我尝试了很多变体,例如: Action 、单独的 Controller ,但什么都没有

<h:form method="post" data-th-action="@{/admin/edit-page/edit}" 
   th:object="${product}">
            <button type="submit" name="action" 
           value="save">save</button>
           <button type="submit" name="action" 
            value="cancel">cancel</button>

</h:form>
@RequestMapping(value="/admin/edit-page/edit", method=RequestMethod.POST, params="action=save")
    public ModelAndView save() {

        return null;
    }


    @RequestMapping(value="/admin/edit-page/edit", method=RequestMethod.POST, params="action=cancel")
    public ModelAndView cancel() {

        return null;   
}

我想根据按下的按钮执行操作

最佳答案

在 Controller 方法中,您的模型参数丢失。

尝试如下

@RequestMapping(value="/admin/edit-page/edit", method=RequestMethod.POST, params="action=save")
    public ModelAndView save(@ModelAttribute Product product) {
      //assuming Product is your class
        return null;
    }


// same for delete

关于java - 一种表单中的多个提交按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56409755/

相关文章:

java - 如何在java中的其他地方使用计数

java - 我如何才能从任何类访问这个数组?

java - Kubernetes:如何使用 K8 的 API 从代码中访问当前正在运行的 pod 的标签值?

xml - 使用 <import> 时出现 Spring/Eclipse 'referenced bean not found' 警告?

java - Thymeleaf 源 jar 中未定义空构造函数

java - Thymeleaf StringTemplateResolver 所有变量为 null

java - StringBuffer equals 方法是否比较内容?

java - 如何在已经使用另一个镜像的 docker 容器中设置并运行一个非常简单的 php-apache 服务器?

java - Spring 错误: only capable of generating responses with characteristics not acceptable according to the request "accept" headers

java - org.springframework.beans.NotReadablePropertyException :