스프링

[ajax / Spring] @RequestBody TestVO paramsJson

배고파요 2024. 2. 23. 16:48
728x90
// ajax 내용

var params = $(“#listForm”).serialize();
var paramsJson = JSON.parse(‘{“‘ + (params.replace(/&/g, “\”, \””).replace(/=/g, “\”:\””)) + ‘“}’);

for(var key in paramsJson){
	paramsJson[key] = decodeURIComponent(paramsJson[key]);
}

var testId = “0001”;
var detailIdList = [];
for(var i=0; i<10; i++){
	var $dtl = $(“input[name=dtl”+i+”]:checked”);
	var $dtlDataId = $dtl.attr(“data-dtl”);
	var $dtlVal = $dtl.val();

	var dtlMap = new Map();
	dtlMap.set(“dtlId”, $dtlDataId);
	dtlMap.set(“testId”, testId);
	dtlMap.set(“questionAnswer”, $dtlVal);
	detailIdList.push(Object.fromEntries(dtlMap)); 
}

paramsJson[“detailIdList”] = detailIdList;


$.ajax({
	type : “POST”,
	async : false,
	traditional : true,
	contentType : “application/json”,
	url : “<c:url value=“/test/modifyProc.do”/>’,
	data : JSON.stringfy(paramsJson),
	dataType : “json”,
	success : function(data){
		console.log(data);
	},
	error : function(xhr, ajaxSettings, thrownError){
		console.log(“error——“);
	}
});
// controller
@RequestMapping(value=“/test/modifyProc.do”, produces=“application.json; charset=UTF-8”)
@ResponseBody
@Transactional
public HashMap<String,String> modifyProc(HttpServletRequest req, ModelMap model
												, @RequestBody TestVO paramsJson
												) throws Exception{

	String testId = paramsJson.getTestId();
	
	List<testDetailVO> dtlList = paramsJson.getDtlList();
	for(testDetailVO dtlVO : dtlList){
		int updateDetailCnt = testService.updateTestDetail(dtlVO);
		// 한 테스트에 대해서 문제별 점수를 업데이트 하는 과정.
	}
	
	int updateTestCnt = testService.updateTest(paramsJson);
	
	HashMap<String, String> rtnMap = new HashMap<String, String>();
	rtnMap.put(“updateTestCnt”, updateTestCnt);
	rtnMap.put(“updateDetailCnt”, updateDetailCnt);
	
	return rtnMap;

}
// VO

// 문제지 vo
public class TestVO{

	String testId;
	String score;
	
	private List<testDetailVO> dtlList;
	
	~~ getter ~ setter ~~
}


// 문제지 내의 문제별 vo
public class TestDetailVO{
	String testId; // 문제지 id
	String testDetailId; // 문제별 id 
	String questionAnswer; // 문제별 선택한 번호.
}





출처 ::

https://amy-it.tistory.com/112

https://leo-history.tistory.com/m/23

https://gent.tistory.com/295

https://gogo-jjm.tistory.com/m/6

https://developer0513.tistory.com/163

https://okky.kr/questions/953421

https://cloud.tencent.com/developer/ask/sof/100394660/answer/102564987

https://choiyeonho903.tistory.com/m/25

https://mingggu.tistory.com/m/66







728x90