Persist radio checked value in Thymeleaf Spring -
when searching, if select given field search within , submit, our choice forgotten. how modify view template keep previous search field selected when displaying results?
i have read many other questions , thymeleaf documentation here have not found suitable answer yet.
one can hard code string (like employer) following:
search.html snippet
<span th:each="column : ${columns}"> <input type="radio" name="searchtype" th:id="${column.key}" th:value="${column.key}" th:checked="${column.key == 'employer'}"/> <label th:for="${column.key}" th:text="${column.value}"></label> </span> searchcontroller.html snippet
@requestmapping(value = "") public string search(model model) { model.addattribute("columns", columnchoices); return "search"; } how can persist user selected radio value upon post in thymeleaf spring? (and default first, value on get)
http://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.html#creating-a-form
command object.
public class search { // default "employer" private string type = "employer"; public string gettype() { return type; } public void settype(string type) { this.type = type; } } controller
@getmapping public string search(model model) { model.addattribute("columns", columnchoices); model.addattribute("search", new search()); return "search"; } @postmapping public string search(@modelattribute search search) { // search's attributes match form selections. } with search command object, radio buttons should this:
<form th:object="${search}"> <th:block th:each="column: ${columns}"> <input type="radio" th:value="${column.key}" th:id="${column.key}" th:field="*{type}" /> <label th:for="${column.key}" th:text="${column.value}" /> </th:block> </form>
Comments
Post a Comment