【问题标题】:Problem at sending a javascript object list to controller in Spring MVC在 Spring MVC 中向控制器发送 javascript 对象列表时出现问题
【发布时间】:2019-05-24 00:44:45
【问题描述】:

我正在尝试检索 Java Spring MVC 中的 javascript 对象列表,但当它到达控制器时为空。

我已经阅读了很多关于但我不知道它是如何工作的

这是我的 ajax 代码:

$.ajax({
                    url : 'ajax/abrir_cotizacion',
                    data : {
                        listaunidades: [{
                                idtipounidad: 1, 
                                modelo: 2013, 
                                cantidad: 1, 
                                tipopago: 1,
                                costounidad: 1500000,
                                ivaoperacion: 25000,
                                totaloperacion: 1504000,
                                enganche: 750000,
                                idplazo: 3
                            },
                            {
                                idtipounidad: 2, 
                                modelo: 2012, 
                                cantidad: 2, 
                                tipopago: 2,
                                costounidad: 1500000,
                                ivaoperacion: 25000,
                                totaloperacion: 1504000,
                                enganche: 750000,
                                idplazo: 6
                            }]
                    },
                    type : 'POST',
                    dataType : 'json',
                    success : function(data) {

                    },
                    error : function(xhr, status) {
                        alert('Disculpe, existió un problema');
                    },
                    complete : function(xhr, status) {
                        //alert('Petición realizada');
                    }
                });

在我的控制器和模型下面需要这个。

@ResponseBody
@RequestMapping(value = "/ajax/abrir_cotizacion", method = RequestMethod.POST)
public Object abrircotizacion(Model model, HttpServletRequest request, @ModelAttribute ArrayList<Unidades> listaunidades) {
    try {
        Injector inj = AppInjector.getInjector();
        return new MsgPojo(1, "Se abre la cotización");
    } catch (Exception ex) {
        LoggerUtils.printLog(this.getClass(), Level.SEVERE, ex, null, Thread.currentThread().getStackTrace());
        return new MsgPojo(-1, "Ocurrio un error al cargar los datos. " + ex.toString());
    }
}


public class Unidad {

    private int idtipounidad;
    private int modelo;
    private int cantidad;
    private int tipopago;
    private double costounidad;
    private double ivaoperacion;
    private double totaloperacion;
    private double enganche;
    private int idplazo;

   //getters and setters
}

【问题讨论】:

    标签: javascript java jquery ajax spring-mvc


    【解决方案1】:

    我正在使用 Spring Boot 2.1.4
    我已经尝试过了,它正在工作。

    HomeController.class

    @RestController
    public class HomeController {
    
        private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
    
        @PostMapping("/test")
        public void test(@RequestBody UserDTO[] userDTO) {
            logger.info(userDTO.length + ""); //expect 2
        }
    
    }
    

    UserDTO.class

    @Data
    public class UserDTO {
    
        private String email;
        private String nickname;
    
    }
    

    我正在使用 邮递员

    POST /test? HTTP/1.1
    Host: localhost
    Content-Type: application/json
    cache-control: no-cache
    [
        {
            "email": "test@test.co.kr",
            "nickname": "test1"
        },
        {
            "email": "test2@test.co.kr",
            "nickname": "test2"
        }
    ]
    

    输出:

    2019-05-24 11:04:40.871  INFO 1860 --- [p-nio-80-exec-5] k.c.k.w.w.s.controller.HomeController    : 2
    

    我还调试了检查我的 userDTO。 电子邮件和昵称设置为我在邮递员中发送的参数。

    像这样改变参数:

    [
      {
        "idtipounidad": 1,
        "modelo": 2013,
        "cantidad": 1,
        "tipopago": 1,
        "costounidad": 1500000,
        "ivaoperacion": 25000,
        "totaloperacion": 1504000,
        "enganche": 750000,
        "idplazo": 3
      },
      {
        "idtipounidad": 2,
        "modelo": 2012,
        "cantidad": 2,
        "tipopago": 2,
        "costounidad": 1500000,
        "ivaoperacion": 25000,
        "totaloperacion": 1504000,
        "enganche": 750000,
        "idplazo": 6
      }
    ]
    

    和你的控制器:

    @ResponseBody
    @RequestMapping(value = "/ajax/abrir_cotizacion", method = RequestMethod.POST)
    public Object abrircotizacion(Model model, HttpServletRequest request, @RequestBody Unidad[] unidad) {
       ....
    }
    

    我希望这项工作.. 并尝试邮递员。这很有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-31
      • 2020-10-11
      • 2013-11-13
      • 1970-01-01
      • 2020-04-17
      • 1970-01-01
      • 1970-01-01
      • 2015-11-21
      相关资源
      最近更新 更多