I am trying to save checkbox values in localstorage using map. so that i can filter result using map. I am able to store map key-values in localstorage using javascript. but later when i retreive and pass map from localstorage to controller, I am getting map with null values.
this is my code
store map in 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))
});
localstorage
passing to controller
$.ajax({
type : "POST",
url : "filter",
data : {
txtMaxAge : localStorage.getItem('txtMaxAge'),
brands : JSON.parse(localStorage.getItem("brands"))
}, // parameters
success : function(result) {
// alert('changed');
}
});
controller
@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";
}
I am getting this error
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
What i am doing wrong here?
I am trying to save checkbox values in localstorage using map. so that i can filter result using map. I am able to store map key-values in localstorage using javascript. but later when i retreive and pass map from localstorage to controller, I am getting map with null values.
this is my code
store map in 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))
});
localstorage
passing to controller
$.ajax({
type : "POST",
url : "filter",
data : {
txtMaxAge : localStorage.getItem('txtMaxAge'),
brands : JSON.parse(localStorage.getItem("brands"))
}, // parameters
success : function(result) {
// alert('changed');
}
});
controller
@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";
}
I am getting this error
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
What i am doing wrong here?
Share Improve this question edited Dec 15, 2018 at 8:37 vidy asked Dec 15, 2018 at 8:26 vidyvidy 1,6406 gold badges27 silver badges43 bronze badges 6-
Change
@RequestParam(value = "brands", required = false) Map<String, String> brands
,to@RequestParam(value = "brands", required = false) ArrayList<String> brands
, – Shubham Dixit Commented Dec 15, 2018 at 9:13 -
@Jai Dixit
value = "brands"
will give us a null in brands ifbrands : ['aaa','bbb']
in JS. – Anatoly Samoylenko Commented Dec 15, 2018 at 9:24 - @Jai Dixit @RequestParam(value = "brands", required = false) ArrayList<String> brands dosent work giving same error. – vidy Commented Dec 15, 2018 at 11:54
-
@vidy
JSON.parse(localStorage.getItem("brands"))
remove this donot parse send the stringified response and use this@RequestParam(value = "brands", required = false) List<String> brands
– Shubham Dixit Commented Dec 15, 2018 at 12:47 - @Jai Dixit giving same error. – vidy Commented Dec 15, 2018 at 12:56
3 Answers
Reset to default 1The problem is that brands is not map type,it is an array of strings. Change the type accordingly and it should work.
Try to change your controller code:
@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";
}
if you really want brands in a Map ; here's my take on it.
Here my controller
@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";
}
Here's my ajax call,
$.ajax({
type: "POST",
url: "${pageContext.request.contextPath}/Submit",
data: {
brands: localStorage.getItem("brands")
},
success : function() {
console.log('done');
}
});