【问题标题】:Spring MVC : Mapping an input button (non submit) with the controller requestSpring MVC:使用控制器请求映射输入按钮(非提交)
【发布时间】:2012-08-13 10:03:08
【问题描述】:

我正在尝试将按钮操作映射到控制器功能。 当我尝试使用时:

@RequestMapping(method = RequestMethod.POST)
public void processValidateGenerate 
....

<input type="button" id="continue" name="continue"...>

效果很好,但现在我想添加另一个按钮,所以为了区分它们,我在第一个操作中添加了:

@RequestMapping(method = RequestMethod.POST, params="continue")
public void processValidateGenerate 
....

但按下按钮时不再调用该函数,我认为这仅适用于提交输入。

是否有任何解决方案可以通过非提交输入来做到这一点?

谢谢

【问题讨论】:

    标签: spring model-view-controller controller request


    【解决方案1】:

    您可以使用 javascript/jquery 进行 ajax 调用并点击所需的控制器方法。

    function doSomething(){
    $.ajax({                                  //This is a jquery ajax call. You need to inlude jquery js files to do
    type: "POST",                              // this operation
    url: //URL,
    data: //JSON data
    }).done(function( msg ) {
    alert( "Operation done: " + msg );
    window.location.href=contextPath+"/newURL";      //If you want to go to a new page
    });
    }
    

    现在添加新按钮(即使它不是按钮也没关系,一个 div 就足够了)。使用 onclick 函数并给出 javscript 函数名称。

    <input type="button" id="newcontinuebutton" name="continue" onclick=doSomething()...>
    

    可以查看jquery ajax详情here

    我想这会对你有所帮助。

    【讨论】:

      猜你喜欢
      • 2012-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-22
      • 2010-12-21
      • 1970-01-01
      • 2014-03-13
      • 2020-01-16
      相关资源
      最近更新 更多