简体   繁体   English

在Spring MVC中单击表单提交和调用提交按钮的JavaScript函数?

[英]Form submit and javascript function calling on submit button click in spring mvc?

On the submit button click event, I am calling a js function for getting the user's input and send to the server side for processing. 在提交按钮单击事件中,我正在调用js函数以获取用户的输入并将其发送到服务器端进行处理。 At the same time, I need to submit the form with all the filled data along. 同时,我需要提交包含所有填充数据的表单。 I put <button type="button"> instead <button type="submit"> as when I put 'type' as submit it gets error. 我放<button type="button">而不是<button type="submit">因为当我将'type'提交时会出错。 There, the js is functioning finely, but the field values are not fetched by controller method. 在那里,js运行正常,但是控制器方法未获取字段值。

How can I do this with only one button ? 我如何只用一个按钮就能做到这一点? Here is my handler method in controller: 这是我在控制器中的处理程序方法:

@RequestMapping(value = "/add_package")
    public
    @ResponseBody
    ModelAndView addPackage(@ModelAttribute("newPackage") Package newPackage,
                            HttpServletRequest request) {

        LOGGER.error("package objct {}",newPackage);// here I am getting objects with null values !!

        String s = request.getParameter("test");
        LOGGER.trace("test val = {}", s);

        ObjectMapper objectMapper = new ObjectMapper();
        List<Package> cats = null;
        String pkgName = newPackage.getPackName();
        try {

           ...
        } catch (Exception e) {
            ..
            LOGGER.error("error in package add {}", e.getMessage());
        }

        return new ModelAndView(new RedirectView("add"));
    }

Here is the jsp code for button: 这是按钮的jsp代码:

<form:button type="button" class="btn btn-success" id="btnAddNewPkg" onclick="contentPackge()"><span class="glyphicon glyphicon-plus"></span>

Add

I dont think you need to pass the url as https://localhost:8443/admin/packages/add_package instead only /add_package will be enough. 我不认为您需要将url作为https://localhost:8443/admin/packages/add_package而仅/add_package就足够了。

Beside you may also need to provide the url-pattern 在旁边,您可能还需要提供url-pattern

Secondly for ajax response I am not sure why you again need ModelAndView . 其次,对于ajax响应,我不确定为什么再次需要ModelAndView You can explore redirect property 您可以探索redirect属性

If you are using button="submit" , then you need to use event.preventDefault() to stop the default behavior. 如果使用button =“ submit”,则需要使用event.preventDefault()停止默认行为。 I guess using button="button" will work fine 我想使用button="button"可以正常工作

See you should submit form after the ajax has been successfuly called that is in the success function of ajax you should manually submit the form using jquery.submit() function .other wise it won't work.because it may disrupt the function of ajax .or you could have time delay function after clicking the submit button . 查看成功后,应该在ajax的成功函数中提交表单,您应该使用jquery.submit()函数手动提交表单,否则将无法正常工作,因为这可能会破坏ajax的功能。或单击“提交”按钮后可能具有延时功能。

this is a nasty way of doing it .i prefer you call the submit function in ajax success and keep button type as button 这是一种令人讨厌的方法。我更喜欢您在ajax成功中调用submit函数,并保持按钮类型为button

    $( "#target" ).submit(function( event ) {
    alert( "Handler for .submit() called." );
    event.preventDefault();showloadingif();
    setTimeout(function(){ $("#formId").submit(); }, 3000);         
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM