Model layer needs to provide data options, set error message
Key Code
@NotNull (message = "Please select gender") Private String Gender;
Controller layer needs to view before displaying, by the data model is transmitted to the view option layer
Data code
final static Map<String,String>RADIO_GENDER = Collections.unmodifiableMap(new LinkedHashMap<String,String>(){ private static final long serialVersionUID = 1L; { put("男,","男"); put("女","女"); } });
Key Code
@GetMapping ( "/ the adduser") public String showAddUserForm (the Model Model) { the User User new new = the User (); user.setId (userDao.getMaxId ()); model.addAttribute ( "User", User); model.addAttribute ( "radioItems", RADIO_GENDER); // this line setting data to the view layer by return "the Add-User"; }
Loop display view layer
Key Code
<div th:each="item : ${radioItems}"> <input type="radio" th:value="${item.value}" th:field="*{gender}" /> <i th:for="${#ids.prev('gender')}" th:text="${item.key}"></i> </div>
Detail list shows the code
<td th:text="${user.gender}"></td>