如何在Spring Boot中将映射从本地存储传递到控制器?

时间:2018-12-15 08:26:54

标签: javascript java spring spring-boot

我正在尝试使用map在本地存储中保存复选框值。这样我就可以使用地图过滤结果。我可以使用javascript将地图键值存储在localstorage中。但是后来当我检索并将地图从localstorage传递到控制器时,我得到的地图具有空值。

这是我的代码

将地图存储在本地存储中

$(':checkbox[name=brand]').on('change', function() {
    var assignedTo = $(':checkbox[name=brand]:checked').map(function() {
        return this.id;
    }).get();
    localStorage.setItem('brands', JSON.stringify(assignedTo))
});

本地存储

enter image description here

传递给控制器​​

$.ajax({
    type : "POST",
    url : "filter",
    data : {
        txtMaxAge : localStorage.getItem('txtMaxAge'),
        brands : JSON.parse(localStorage.getItem("brands"))

    }, // parameters
    success : function(result) {
        // alert('changed');
        }
});

控制器

@RequestMapping("/filter")
@ResponseBody
public String filter(
        @RequestParam(value = "txtMaxAge", required = false) String txtMaxAge,
        @RequestParam(value = "brands", required = false) Map<String, String> brands,
        HttpServletRequest request, HttpServletResponse response) {

    System.out.println("max"+txtMaxAge);

    System.out.println("printing map----");

         for (Map.Entry<String,String> entry : brands.entrySet())  
            System.out.println("Key = " + entry.getKey() + 
                             ", Value = " + entry.getValue()); 
    return "send data";
}

我收到此错误

max10000000
printing map----
2018-12-15 13:37:59.142  WARN 13672 --- [nio-8080-exec-1] .m.m.a.ExceptionHandlerExceptionResolver :
Resolved [java.lang.NullPointerException] to ModelAndView: reference to view with name 'error';
model is {errorTitle=Contact Your Administrator!!, errorDescription=java.lang.NullPointerException

我在这里做错了什么?

3 个答案:

答案 0 :(得分:1)

问题在于品牌不是地图类型,它是一个字符串数组。相应地更改类型,它应该可以工作。

答案 1 :(得分:0)

尝试更改您的控制器代码:

@RequestMapping(value = "/filter")
@ResponseBody
public String filter(
        @RequestParam(value = "txtMaxAge", required = false) String txtMaxAge,
        @RequestParam(value = "brands[]", required = false) String[] brands,
        HttpServletRequest request, HttpServletResponse response) {

    System.out.println("max " + txtMaxAge);
    System.out.println("map " + brands);

    return "send data";
}

答案 2 :(得分:0)

如果您确实想要在地图中找到品牌,这是我的看法。

这是我的控制器

@RequestMapping(value = "/filter", method = RequestMethod.POST)
    public String submission(@RequestParam("brands")String brands) {

        try {

            JSONParser parser = new JSONParser();
            JSONObject ob = (JSONObject)parser.parse(brands);

            Map<String,String> mp = new HashMap<>();
            mp.put(ob.keySet().toString(), ob.values().toString());

            for(Map.Entry<String,String> entry : mp.entrySet()) {

                System.out.println("Key = " + entry.getKey() + 
                        ", Value = " + entry.getValue().toString()); 
            }

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        return "home";
    }

这是我的ajax电话,

$.ajax({
  type: "POST",
  url: "${pageContext.request.contextPath}/Submit",
  data: {

      brands: localStorage.getItem("brands")
  },
  success : function() {
       console.log('done');
  }

});