【问题标题】:How to send a json file to a spring controller?如何将 json 文件发送到 spring 控制器?
【发布时间】:2018-02-15 04:22:27
【问题描述】:

验证表单的所有输入后,我尝试将 JSON 发送到控制器(Spring-MVC 框架)。但是,我一直在获取空数据对象。

这是我的.js 文件

/**
 * 
 */

$(document).ready(function() {
 alert('Running ... ');
});



function validateForm() {

        var name = document.forms.myform.elements.name.value;
        var email = document.forms.myform.elements.email.value;
        var description = document.forms.myform.elements.description.value;
        var error = 'has-error';
        var ok = 'has-success';

        if (!name || !email || !description) {

          // alert(description);
          if (!description) {
            $('#description_form').addClass(error);
          } else {
            $('#description_form').removeClass(error).addClass(ok);
          }
          if (!name) {
            $('#name_form').addClass(error);
          } else {
            $('#name_form').removeClass(error).addClass(ok);
          }
          if (!email) {
            $('#email_form').addClass(error);
          } else {
            $('#email_form').removeClass(error).addClass(ok);
          }
          return false;
        }

        var frm = $(document.forms.myform);


            $.ajax({
                type: 'POST',
                url: '/insert',
                data: {
                        name: "Name", 
                        email: "example@abc.com", 
                        desc: "Not a real person" 
                },
                success: function (data) {
                    console.log('Submission was successful.');
                    console.log(data);
                },
                error: function (data) {
                    console.log('An error occurred.');
                    console.log(data);
                },
                beforeSend: function (data) {
                    console.log('Before sending data ... ');
                    console.log(data);
                }
            });

            e.preventDefault();

        return true;

}

对于控制器部分,使用org.json.JSONObject库:

package gac.asr;

import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HomeController {


    @RequestMapping(path = "/", method = RequestMethod.GET)
    public String index() {
        return "index";
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public @ResponseBody void getData(JSONObject json) {
        System.out.println(json.toString());
    }
}

运行应用程序时,我从脚本中收到“正在运行...”消息。但是当我尝试打印也来自脚本的数据时,我得到一个空的 JSON 对象“{}”。

编辑:

添加调用验证脚本的表单。

<form:form name="myform" class="form-horizontal" onsubmit="return validateForm();" method="POST" action="/insert">
<!--MORE CODE-->
</form:form>

【问题讨论】:

    标签: javascript java jquery spring spring-mvc


    【解决方案1】:

    我会创建一个包装器来封装你的所有属性。我们称它为 MyEntity

    public class MyEntity implements Serializable{
        private String name;
        private String email;
        private String desc;
        //getters setters
    }
    

    在你的控制器中使用它

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public void getData(MyEntity person) {
        System.out.println(person.getName());
    }
    

    【讨论】:

      【解决方案2】:

      在你的控制器中,你应该使用:

      @RequestMapping(value = "/insert", method = RequestMethod.POST)
      public String getData(
        @RequestParam(value = "name", required = true,) String name,
        @RequestParam(value = "email", required = true,) String email,
      ){
        System.out.println(name);
        ...
      }
      

      【讨论】:

      • 我收到“HTTP 状态 400 - 所需的字符串参数 'name' 不存在”消息
      猜你喜欢
      • 2012-09-09
      • 2012-01-29
      • 2018-01-18
      • 1970-01-01
      • 2021-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多