JAVA
선택된 체크박스 (list, json, 배열) 데이터 컨트롤러에서 받기
hhana
2023. 8. 1. 21:59
이처럼 list 혹은 json 혹은 배열 형식으로 데이터를 컨트롤러로 전달하는데에 무사히 성공했다면!
이 데이터는 어떻게 깔끔하게 정리 가공할 수 있을까?
List -> JSON 형식
@PostMapping("/받았다.do")
public void 받았다(@RequestParam Map<String, Object> reqParam) throws Exception {
String trgtList = HtmlUtils.htmlUnescape((String) reqParam.get("trgtList"));
JSONArray ja = new JSONArray(trgtList);
for (int i = 0 ; i < ja.length() ; i++) {
JSONObject jo = (JSONObject) ja.get(i);
String userId = (String) jo.get("userId");
UserVO userVO = new UserVO();
userVO.setUserId(userId);
userService.selectUserInfo(vo);
}
}
배열
@PostMapping("/배열받았다.do")
public void 배열받았다(UserVO vo) throws Exception {
//vo에 String[] userIds가 있고 html에서 String[] userIds를 넘겨야 vo에 담겨요
String[] userIds = vo.getUserIds();
for (int i = 0 ; i < userIds.length() ; i++) {
String userId = userIds[i];
UserVO userVO = new UserVO();
userVO.setUserId(userId);
userService.실행할메서드이름(vo);
}
}
const checkedLength = $('input[name=trgt]:checked').length;
var trgtList = new Array();
for (var i = 0; i < checkBoxList.length; i++) {
var chkBox = $(checkBoxList[i]);
const index = chkBox.attr('id').split('_')[1];
const userId = $('#USER_ID' + index).val();
trgtList[i] = {'userId' : userId};
}
const trgtListTxt = JSON.stringify(trgtList);
param['trgtList'] = trgtListTxt;
이 html 코드를 참고하여 param을 전달하면 JSON 형식으로 데이터를 전달받을 수 있다
const checkedLength = $('input[name=trgt]:checked').length;
var trgtList = new Array();
for (var i = 0; i < checkBoxList.length; i++) {
var chkBox = $(checkBoxList[i]);
const index = chkBox.attr('id').split('_')[1];
const userId = $('#USER_ID' + index).val();
trgtList[i] = {'userId' : userId};
}
param.userIds = trgtList;
이 html 코드를 참고하여 param을 전달하면 배열 형식(String[] userIds)으로 데이터를 전달받을 수 있다