Spring Boot
controller와 dto, html 에서 배열을 주고 받을때 성공 실패
제뉴어리맨
2021. 3. 18. 18:56
- 보낼때 HTML (type을 아래와 같은 형태로 하면 아래쪽 자바에서 받을수 있다. (
<input type="hidden" name="page" th:value="${requestDTO.page}">
<input type="hidden" name="type[0]" th:value="${requestDTO.getType(0)}">
<input type="hidden" name="type[1]" th:value="${requestDTO.getType(1)}">
<input type="hidden" name="type[2]" th:value="${requestDTO.getType(2)}">
<input type="hidden" name="typeKeyword" th:value="${requestDTO.typeKeyword}">
<input type="hidden" name="region" th:value="${requestDTO.region}">
<input type="hidden" name="regionKeyword" th:value="${requestDTO.regionKeyword}">
<input type="hidden" name="minCost" th:value="${requestDTO.minCost}">
<input type="hidden" name="maxCost" th:value="${requestDTO.maxCost}">
- 받을때 JAVA
@PostMapping("modify")
public String modify(BoardDTO dto, @ModelAttribute("requestDTO")
PageRequestDTO pageRequestDTO, RedirectAttributes redirectAttributes)
{
boardService.modify(dto);
redirectAttributes.addAttribute("page", pageRequestDTO.getPage());
redirectAttributes.addAttribute("size", pageRequestDTO.getSize());
redirectAttributes.addAttribute("type[0]", pageRequestDTO.getType(0));
redirectAttributes.addAttribute("type[1]", pageRequestDTO.getType(1));
redirectAttributes.addAttribute("type[2]", pageRequestDTO.getType(2));
redirectAttributes.addAttribute("typeKeyword", pageRequestDTO.getTypeKeyword());
redirectAttributes.addAttribute("region", pageRequestDTO.isRegion());
redirectAttributes.addAttribute("regionKeyword", pageRequestDTO.getRegionKeyword());
redirectAttributes.addAttribute("minCost", pageRequestDTO.getMinCost());
redirectAttributes.addAttribute("maxCost", pageRequestDTO.getMaxCost());
redirectAttributes.addAttribute("bno", dto.getBno());
return "redirect:/board/read";
}
- 받을때 pageRequestDTO 내부
public class PageRequestDTO { //현재 페이지에 대한 정보를 담고 있는것 같은 DTO
private int page;
private int size;
private boolean[] type;
private String typeKeyword;
private boolean region;
private String regionKeyword;
private Long minCost;
private Long maxCost;
:
:
생략
그리고 " - 받을때 JAVA " 소스처럼 redirect시 type을 보내주면 redierect 되는 read에서도 또 배열을 정상적으로 받을 수있다.
되도록 파라미터는 배열로 하지말자...