thymeleaf_form : multi checkbox
This commit is contained in:
@@ -9,7 +9,9 @@ import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
@Controller
|
||||
@@ -19,6 +21,15 @@ public class FormItemController {
|
||||
|
||||
private final ItemRepository itemRepository;
|
||||
|
||||
@ModelAttribute("regions")
|
||||
public Map<String, String> regions() {
|
||||
Map<String, String> regions = new LinkedHashMap<>();
|
||||
regions.put("SEOUL", "서울");
|
||||
regions.put("BUSAN", "부산");
|
||||
regions.put("JEJU", "제주");
|
||||
return regions;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public String items(Model model) {
|
||||
List<Item> items = itemRepository.findAll();
|
||||
@@ -30,6 +41,7 @@ public class FormItemController {
|
||||
public String item(@PathVariable long itemId, Model model) {
|
||||
Item item = itemRepository.findById(itemId);
|
||||
model.addAttribute("item", item);
|
||||
|
||||
return "form/item";
|
||||
}
|
||||
|
||||
@@ -42,6 +54,7 @@ public class FormItemController {
|
||||
@PostMapping("/add")
|
||||
public String addItem(@ModelAttribute Item item, RedirectAttributes redirectAttributes) {
|
||||
log.info("item.open = {}", item.getOpen());
|
||||
log.info("item.region = {}", item.getRegions());
|
||||
Item savedItem = itemRepository.save(item);
|
||||
redirectAttributes.addAttribute("itemId", savedItem.getId());
|
||||
redirectAttributes.addAttribute("status", true);
|
||||
|
||||
@@ -42,6 +42,15 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- multi checkbox -->
|
||||
<div>
|
||||
<div>등록 지역</div>
|
||||
<div th:each="region : ${regions}" class="form-check form-check-inline">
|
||||
<input type="checkbox" th:field="*{regions}" th:value="${region.key}" class="form-check-input">
|
||||
<label th:for="${#ids.prev('regions')}" th:text="${region.value}" class="form-check-label">서울</label>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<button class="w-100 btn btn-primary btn-lg" type="submit">상품 등록</button>
|
||||
|
||||
@@ -47,6 +47,15 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- multi checkbox -->
|
||||
<div>
|
||||
<div>등록 지역</div>
|
||||
<div th:each="region : ${regions}" class="form-check form-check-inline">
|
||||
<input type="checkbox" th:field="*{regions}" th:value="${region.key}" class="form-check-input">
|
||||
<label th:for="${#ids.prev('regions')}" th:text="${region.value}" class="form-check-label">서울</label>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<button class="w-100 btn btn-primary btn-lg" type="submit">저장</button>
|
||||
|
||||
@@ -49,6 +49,15 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- multi checkbox -->
|
||||
<div>
|
||||
<div>등록 지역</div>
|
||||
<div th:each="region : ${regions}" class="form-check form-check-inline">
|
||||
<input type="checkbox" th:field="${item.regions}" th:value="${region.key}" class="form-check-input" disabled>
|
||||
<label th:for="${#ids.prev('regions')}" th:text="${region.value}" class="form-check-label">서울</label>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<button class="w-100 btn btn-primary btn-lg"
|
||||
|
||||
Reference in New Issue
Block a user