How to send data from radio buttons to Controller with Thymeleaf
15,029
According to 6.4.1 Setting and getting basic and nested properties, you should have <input>
names such as priorities[camera]
in generated HTML to make it work.
Try the following:
<input type="radio" th:name="${'priorities[' + item + ']'}" value="a" />
Comments
-
ruhungry almost 2 years
I have got an easy
HTML
page where I am displaying fewradio buttons
:<form action="#" th:action="@{/processForm}" th:object="${priorities}" method="post"> <fieldset> <table style="width: 500px"> <tr th:each="item : ${chosen}"> <td> <div> <div> <label th:text="${item}">example</label> <input type="radio" th:name="${'priorities[' + item + ']'}" value="a" />A <input type="radio" th:name="${'priorities[' + item + ']'}" value="b" />B <input type="radio" th:name="${'priorities[' + item + ']'}" value="c" />C </div> </div> </td> </tr> <tr> <td></td> <td></td> </tr> <tr> <td> <button type="submit" class="btn btn-xs btn-primary margin10-right paddingNew" name="save">Calculate!</button> </td> <td></td> </tr> </table> </fieldset> </form>
Here you got what I can see:
I would like to send this data to my controller, so I created one:
@RequestMapping(value = "/processForm", method = RequestMethod.POST) public String save(@ModelAttribute(value = "foo") ClusterParams foo, @ModelAttribute(value = "priorities") HashMap<String, String> priorities, final ModelMap m) throws ClassNotFoundException, IOException, InterruptedException { for (String s : priorities.keySet()) { System.out.println(s); } }
Here you can see my class Weights (removed):
public class Weights { Map<String, String> priorities = new HashMap<String, String>(); public Map<String, String> getPriorities() { return priorities; } public void setPriorities(Map<String, String> priorities) { this.priorities = priorities; } }
but I have no idea how to set the values with thymeleaf.
I would like to get a mapping:
camera -> B video -> C
Could you help me to do that? Of course it is my idea with creating a HashMap, if there is any other solution I will change it.
Thank you in advance