My HTML table data is converted to JSON format and sent via AJAX to a Spring controller. In the controller, I used
@RequestParam Map<String, String>
to retrieve the values, but the entire JSON string was received as the only key. I cannot use a model class due to different scenarios, so I need the column headers along with their values.
<table>
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>A1</td>
<td>A2</td>
<td></td>
</tr>
<tr>
<td>B1</td>
<td>B2</td>
<td>B3</td>
</tr>
<tr>
<td>C1</td>
<td></td>
<td>C3</td>
</tr>
</tbody>
The HTML table data is transposed into JSON format and transmitted through AJAX.
[
{
"Column 1": "A1",
"Column 2": "A2",
"Column 3": ""
},
{
"Column 1": "B1",
"Column 2": "B2",
"Column 3": "B3"
},
{
"Column 1": "C1",
"Column 2": "",
"Column 3": "C3"
}
]
AJAX snippet -
$.ajax({
type: "POST",
//contentType : 'application/json; charset=utf-8',
//dataType : 'json',
url: "/gDirecotry/" + id,
data: JSON.stringify(rows),
success: function (result) {
console.log(result);
}
});
Spring controller logic -
@RequestMapping(value = "/gDirecotry/{id}", method = RequestMethod.POST)
public @ResponseBody ModelAndView
getSearchUserProfiles(@PathVariable("id") String id,
@RequestParam Map<String, String>
attributeMap) throws IOException {
return new ModelAndView("redirect:/home");
}
I aim to map the JSON data to map<string,string>
, how can this be achieved?
OUTPUT : I received the whole string as a single key without any associated value.
[{"Column 1":"A1","Column 2":"A2","Column 3":""},{"Column 1":"B1","Column
2":"B2","Column 3":"B3"},{"Column 1":"C1","Column 2":"","Column 3":"C3"}]