【问题标题】:Processing two different submit button requests with one Spring MVC controller class使用一个 Spring MVC 控制器类处理两个不同的提交按钮请求
【发布时间】:2018-10-04 10:36:27
【问题描述】:

我在 thymeleaf 模板中有两个提交按钮。我希望它在同一个控制器中将其提交给 2 个不同的操作。我做了这样的请求映射:

@RequestMapping(value="/directBind",  params={"send"})
public String send(Model model, @ModelAttribute(value="directBind") DirectBind directBind){

@RequestMapping(value="/directBind", params={"addPolicy"})
public String addPolicy(final DirectBind directBind, Model model){

html中的两个输入按钮:

 <input id="send"  type="submit" value="send" name="send" class="btn btn-success finish" data-loading-text="Sent!"/>

 <input type="submit"  name="addPolicy" class="btn btn-default" style="margin-left: 1rem; margin-bottom: 1rem;"><span class="fa fa-plus"></span> </input>

表格:

<form enctype="multipart/form-data" class="ui form" th:object="${directBind}" th:action="@{/send}" method="post"  style="padding:0 10px;">

它提交表单只是为了发送动作。

【问题讨论】:

    标签: spring-mvc spring-boot thymeleaf


    【解决方案1】:

    我推断,您的意图是使用一个弹簧控制器来处理来自两个提交按钮的提交操作。

    • 您可以为两个 html 输入按钮使用相同的名称和不同的值属性。按钮名称和值属性作为请求参数发送到控制器。这将允许控制器识别单击了哪个按钮并采取适当的措施。

        <input id="send_id"  type="submit" name="action" value="send"/>
        <input id="addPolicy_id"  type="submit" name="action" value="addPolicy"/>
        @RequestMapping(value="/directBind")
        public String doExecute(@RequestParam("action") String action, Model model, @ModelAttribute(value="directBind") DirectBind directBind){
        if (action=="send"){
        //do something
        send();
        }
        if (action=="addPolicy"){
        //do something
        addPolicy();
        }
    }
    

    【讨论】:

      【解决方案2】:

      我设法解决了这个问题。我在表单动作属性中使用了“directBind”,并在请求映射中使用了参数鉴别器。有效!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-06
        • 2012-05-07
        • 2011-06-18
        • 1970-01-01
        • 1970-01-01
        • 2017-03-22
        • 2015-05-06
        • 1970-01-01
        相关资源
        最近更新 更多