개발/Spring Boot
Thymeleaf controller에서 값 받아오기 및 th:each 등 사용법 값 받기 값 담아오기
멋진놈
2022. 7. 28. 18:54
728x90
각 부분에 값 가져오기
message.fileList[0].storedFilePath
'images/20220728/1365666336304400.png'
controller에서 배열로 보냄
@RequestMapping(value="/jpa/board", method=RequestMethod.GET)
public ModelAndView openBoardList() throws Exception {
ModelAndView mv = new ModelAndView("thymeleaf/board/jpaBoardList");
List<Board> list = jpaBoardService.selectBoardList();
mv.addObject("list", list);
return mv;
}
배열로 보냈으니 배열로 받아야함.
jpaBoardList.html에서
<td th:each="list : ${list}">
배열에 있는 객체 값 담아옴.
이 list 안에 배열 값을 담아오고 싶으면
<td th:each="list : ${list.fileList}">
하면 값이 담기며
그 안에 값은
<td th:text="${list.fileList[0]}"></td>
이렇게 list.fileList[0].~값 이렇게 가져와야함.
<script th:inline="javascript">
/*<![CDATA[*/
var message = /*[[${list}]]*/ 'default';
var message2 = /*[[${list.fileList}]]*/ 'default';
/*]]>*/
console.log(message);
console.log(message2);
console.log(message4);
</script>
</tr>
하면 콘솔 찍기 가능
controller
model.addAttribute("userId",1);
html
<input type="hidden" th:value="${userId}"/> 값 담기
[[${list}]]
도 사용 가능
@PostMapping("/permit")
public String permit_policy_page(PolicyDto dto,String userId){ // userId => firstPageId
Long firstPageId = Long.parseLong(userId);
....
}
다만, 이런 방식은 컨트롤러에서 Long 타입이 아닌, String으로밖에 받지 못합니다.
조금 더 세련된 방식으로는 @RequestParam을 이용하는 방법이 있습니다.
@PostMapping("/permit")
public String permit_policy_page(PolicyDto dto,@RequestParam("userId") Long firstPageId){ // userId => firstPageId
// Long firstPageId = Long.parseLong(userId);
...
}
th:name값을 받아서 바로 Long 타입으로 변환할 수 있습니다.