How to pass Hasmap from Html to Spring controller in java.And also I would like to know how to get it from the controller.
Asked
Active
Viewed 99 times
-2
-
what you have tried? – soorapadman Oct 14 '15 at 08:31
-
Post your code/ configuration for people to help you – Bilbo Baggins Oct 14 '15 at 08:37
-
@Soorapadman I just tried to pass it as a normal variable and I could see the map values in the url. but in the controller i couldnt get it. – dona devasia Oct 14 '15 at 08:37
-
http://viralpatel.net/blogs/spring-mvc-hashmap-form-example/ try this :) :) – Bilbo Baggins Oct 14 '15 at 08:38
-
and this too ... http://stackoverflow.com/questions/18280395/passing-a-mapstring-string-to-a-springmvc-controller – Bilbo Baggins Oct 14 '15 at 08:38
2 Answers
0
First of all there is nothing like passing hashmap from html to Spring controller. What you want to accomplish is actually making a http request to web application.
Create a form model which has Map<String,String>
as class variable with appropriate setter and getter.
Then in your action;
public @ResponseBody String formPost(@ModelAttribute YourFormClass form, HttpServletRequest request) {
//Rest of your business logic
}

Fatih Donmez
- 4,319
- 3
- 33
- 45
0
I guess this will help you.
HTML:
<html><body>
<input type="hidden" name="Hashmap" id="mymap">
</body>
<script>
var Map = new Map(); //push some values into this
var obj = Object.fromEntries(keyvalue);
var StringKeyvalue = JSON.stringify(obj);
console.log("entra babu nak idhi: ",StringKeyvalue)
document.getElementById("mymap").value = StringKeyvalue;
</script>
</html>
Controller:
public String passingHashmap(HttpServletRequest request,@RequestParam("Hashmap")
String mapValues) {
ObjectMapper mapper = new ObjectMapper();
try {
Map<String, String> myHashMap = mapper.readValue(mapValues, new
TypeReference<HashMap<String, String>>(){});
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}

user21252893
- 1
- 1