반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- yona
- Maven
- mybatis
- pwa
- FCM
- popup
- PM2
- git
- Java
- MariaDB
- mysql
- javascript
- spring3
- security
- rocky9
- SpringBoot
- Eclipse
- docker
- MSsql
- Next.js
- config
- jenkins
- NextJS
- node.js
- jquery
- Push
- Tomcat
- centos7
- ajax
- nodejs
Archives
- Today
- Total
ふたりで
JsonData를 VO model로 바로 받을때...참고 본문
728x90
반응형
SMALL
Array에 담긴 JsonData를 controller에서 VO model로 바로 받을 때 참고.
1. jsondata 생성
$(document).on('click','.js_authChangePopup',function(e){
//공통변수 클릭한 요소의 data에서 항목별 값 추출
var ordernum = parseInt($(this).data('ordernum'));
var arrayObj = new Array();//전송용 array 객체 선언
$('.js_check').each(function(){//체크된 요소 루프돌면서 data 값 추출
if($(this).is(':checked') == true){
var info = new Object();
info.ordernum=ordernum;
info.username=$(this).data('username');
info.age=parseInt($(this).data('age');
arrayObj.push(info);
}
});
//서버에전달
$.ajax({
type : "POST",
url : ctx + "/save",
data : JSON.stringify(arrayObj),//서버요청시 위에서 생성한 arrayObj를 json으로 전달
async: false,
contentType: "application/json",
beforeSend : function(xhr){
//ajax호출 중 처리
//글로벌 변수로 설정한 csrf token 셋팅
//xhr.setRequestHeader(header,token);
},
success : function(data) {
//ajax호출 성공시 후처리
},
error : function(request, status, error) {
//ajax호출 실패시 후처리
}
});
});
- 요청되는 결과 :
[
{"ordernum" : 1234567890, "username" : "graykang", "age" : 34},
{"ordernum" : 1234123456, "username" : "graykang2", "age" : 35},
{"ordernum" : 5432167890, "username" : "graykang3", "age" : 33}
]
2. controller 에서 처리 시 jsonData를 담을 VO model 생성
package com.grasykang.front.model;
public class RequestJsonVO {
private int ordernum;
private String username;
private int age;
public int getOrdernum() {
return ordernum;
}
public void setOrdernum(int ordernum) {
this.ordernum = ordernum;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
3. controller 에서 받는 부분.
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.json.simple.JSONObject;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.graykang.front.model.TestJsonVO;
/**
* @param request
* @param martCode
* @param empCode
* @param model
* @return
*/
@RequestMapping(value = "save", method = RequestMethod.POST)
public @ResponseBody JSONObject saveTest(HttpServletRequest request
,HttpSession session, @RequestBody List<TestJsonVO> jsondata, Model model) {
JSONObject jsonObj = testService.savePro(jsondata);
return jsonObj;
}
4. serviceImpl 에서 루프 돌면서 처리 이후 트랜잭션 설정 필요.
@SuppressWarnings("unchecked")
@Override
public JSONObject saveTest(List<TestJaonVO> jsondata) {
JSONObject jsonObj = new JSONObject();
TestJaonVO params = new TestJaonVO();
try {
for(int i=0; i<jsondata.size(); i++) {
params.setOrdernum(jsondata.get(i).getOrdernum());
params.setUsername(jsondata.get(i).getUsernamep());
params.setAge(jsondata.get(i).getAge());
testDao.saveInsert(params);
}
jsonObj.put("msg", "저장 하였습니다.");
} catch (Exception e) {
jsonObj.put("msg", "저장중 오류가 발생했습니다.");
}
return jsonObj;
}
참고: Spring3.x 버전에서는 위내용 관련 버그가 있다고 한다 아래 링크 참고.
java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to ~ 관련 정보.
spring 버전을 3.x 번대로 업그레이드를 한 프로젝트가 있는데 jsonArray를 List로 받아서 사용을 할 때 java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to ~ 요 딴 에러가 발생하면 아래와 같이 처
graykang.tistory.com
728x90
반응형
LIST
'Spring' 카테고리의 다른 글
mybatis INSERT 후 PK(자동생성된ID) 반환 받기(useGeneratedKeys) (0) | 2023.11.27 |
---|---|
mybatis+select count() resultType (0) | 2023.11.27 |
browser type check by JAVA (0) | 2023.06.08 |
Springboot2.x Web 프로젝트 설정 1(프로젝트 생성 및 JSP설정) (0) | 2023.05.08 |
springboot+security session timeout config (0) | 2022.06.23 |
Comments