Spring uses the check option button

Model layer needs to provide data options, set error message

Key Code

	@NotNull 
	@Size (= min. 1, max =. 5, Message = "Select Course") 
	Private String [] Course;

  

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>CHECK_COURSE = Collections.unmodifiableMap(new LinkedHashMap<String,String>(){
		private static final long serialVersionUID = 1L;
		{
			put("Java程序设计","java");
			put("Spring","spring");
			put("MySQL","mysql");
			put("HTML","html");
		}
	});

Key Code

     @GetMapping("/adduser")
	public String showAddUserForm(Model model) {
		User user = new User();
		user.setId(userDao.getMaxId());
		model.addAttribute("user", user);
		model.addAttribute("checkItems", CHECK_COURSE);//通过此行设置数据到视图层
		return "add-user";
	}

  

  Loop display view layer

Key Code

	<div th:each="item : ${checkItems}">
		<input type="checkbox" th:value="${item.value}" th:field="*{course}" /> 
		<i th:for="${#ids.prev('course')}" th:text="${item.key}"></i>
	</div>

  Detail list shows the code

<td><p th:each="c:*{user.course}" th:text="${c}"></p></td>

  

Guess you like

Origin www.cnblogs.com/max-hou/p/11111297.html