代码之家  ›  专栏  ›  技术社区  ›  wen tian

如何使用Spring mvc接收axios阵列

  •  0
  • wen tian  · 技术社区  · 6 年前

    在brand controller类中,我要执行以下操作:

    @RequestMapping(value = "brand",method = RequestMethod.GET)
    @ResponseBody
    public Object deleteByIds(int[] ids) {
        System.out.println(ids);
        goodsBrandService.deleteByIds(ids);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("msg","success to delete");
        return jsonObject;
    }
    

    当接收到数组时,它给我空值。

    <delete id="deleteByIds" parameterType="list">
        DELETE FROM
        goods_brand
        WHERE brand_id IN
      <foreach collection="array" item="item" open="(" separator="," close=")">
        ${item}
      </foreach>
    

    例如,我在Vue.js上使用axios向springmvc发送参数

     // Determine bulk delete
     multiDelete() {
      let checkArr = this.multipleSelection;
      let params = [];
      let self = this;
      checkArr.forEach(function (item) {
        params.push(item.brandId); 
      });
      console.log(params); 
      this.$http.get('http://localhost:9090/brand', params).then(function (res) {
        if (res) {
          self.$message({
            message: 'success to delete',
            type: 'success'
          });
        }
      }).then(error => {
        this.$message.error("failed to delete");
      })
      this.multiDeleteVisible = false; //close delete bullet box
    }
    

    console.log(params) ,例如:

    (2) [43, 41]
        0: 43
        1: 41
        length: 2
        __proto__: Array(0)
    

    1 回复  |  直到 6 年前
        1
  •  0
  •   Hadi Jeddizahed    6 年前

    应该是这样的

    @RequestMapping(value = "brand/{ids}",method = RequestMethod.GET)
    @ResponseBody
    public Object deleteByIds(@PathVariable Integer[] ids) {
     System.out.println(ids);
     goodsBrandService.deleteByIds(ids);
     JSONObject jsonObject = new JSONObject();
     jsonObject.put("msg","success to delete");
     return jsonObject;
    }
    

    @RequestMapping(value = "brand",method = RequestMethod.GET)
    @ResponseBody
    public Object deleteByIds(@RequestParam(value="ids[]") Integer[] ids) {
    
    }
    

    但我建议使用 POST GET ,因此您的请求映射更改为:

    @RequestMapping(value = "brand",method = RequestMethod.POST)
    @ResponseBody
    public Object deleteByIds(@RequestBody WarapperList ids) {
    
    }
    

    WrapperList 是这样一个类:

    class WrapperList{
        private List<Integer> ids;
    }