-2

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.

2 Answers2

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;
}