通过jquery脚本将表单提交到多个url

时间:2014-07-16 10:15:10

标签: java javascript jquery forms spring

在我目前的春季项目中,我有一个包含两种类型字段的表单:普通文本字段和允许上传图像文件的文件输入。我想在我的spring控制器中将这些类型的字段中的每一个提交给不同的方法。文本字段应提交给以下方法之一:

@RequestMapping(value="insert", method=RequestMethod.POST)
@ResponseBody
public String insert(@ModelAttribute("object") E object, BindingResult result) {
    if(serv.cadastra(object)) {
        return "yes";
    }
    else {
        return "not";
    }
}

@RequestMapping(value="update", method=RequestMethod.POST)
@ResponseBody
public String update(@ModelAttribute("object") E object, BindingResult result) {
    if(serv.altera(object)) {
        return "yes";
    }
    else {
        return "not";
    }
}

应将filem upload字段提交给此方法:

@RequestMapping(value="upload", method=RequestMethod.POST)
@ResponseBody
public String setPicture(@ModelAttribute(value="object") E object, BindingResult result, @RequestParam("file") MultipartFile file) {
    if(serv.upload_picture(object, file))
        return "pic-yes";
    else
        return "pic-not";
}

到目前为止,我已尝试过这个jquery脚本,但它只适用于文本字段:

$(document).on('submit', '.form', function (event) {
    event.preventDefault();

    var $form = $( this ),  url = $form.attr( "action" );
    var posting = $.post( url, $(this).serialize() );
    posting.done(function( data ) {
        var alert = $("#"+data); 
        alert.css("display", "block");
        $(".form").each(function(){
            this.reset();
        });
        $(".upload").each(function(){
            var $upload = $(this), url = $upload.attr("action");
            var posting2 = $.post( url, $(this).serialize() );
            posting2.done(function(data){
                $("#"+data).css("display", "block");
                $("#"+data).fadeOut(2500);
            });
        });
        alert.fadeOut(2500);
    });
});

表单的html代码目前是这样的:

<form id="command" role="form" class="form" action="Usuario/cadastra" method="post">
    <fieldset>
    <div class="panel panel-default">
        <div class="panel-heading">
        </div>
        <div class="panel-body">
      <p>
      <label for="login" class="form-control">login</label>
      <input id="login" name="login" class="form-control" type="text" value=""/>
      </p>

      <p>
      <label for="senha" class="form-control">senha</label>
      <input id="senha" name="senha" class="form-control" type="password" value=""/>
      </p>

      <p>
      <label for="first_name" class="form-control">first_name</label>
      <input id="first_name" name="first_name" class="form-control" type="text" value=""/>
      </p>

      <p>
      <label for="last_name" class="form-control">last_name</label>
      <input id="last_name" name="last_name" class="form-control" type="text" value=""/>
      </p>

      <p>
      <label for="email" class="form-control">email</label>
      <input id="email" name="email" class="form-control" type="text" value=""/>
      </p>    
      </div>
  </div>


        <div class="panel panel-default include">
            <div class="panel-heading">
                Selecione um arquivo de imagem para <i>foto do perfil</i>
            </div>
            <div class="panel-body">

                <form id="command" class="form upload" action="/loja/Usuario/upload" method="POST">
                    <input type="file" name="file" class="form-control"/>
                </form>
            </div>
        </div>

    <p>
        <button type="submit" class="btn btn-lg btn-primary">Cadastrar</button>
    </p>
    </fieldset>
</form>

任何人都可以指明方向来实现这个目标吗?

1 个答案:

答案 0 :(得分:0)

对于此异常:org.springframework.web.multipart.MultipartException:当前请求不是多部分请求

您是否已将此行添加到spring xml或applicationContext文件中。

&lt; bean id =“multipartResolver”class =“org.springframework.web.multipart.commons.CommonsMultipartResolver”/&gt;

还在类路径中添加 spring-webmvc jar