[Spring] 어려운 데이터 controller에서 받기, form data request로 받기
https://hello-bryan.tistory.com/347
- 어려운 데이터 controller에서 받기
(file은 그냥 보내고 나머지 자료들은 JSON.stringify 시켜주기)
https://howtolivelikehuman.tistory.com/169
- form tag에input 필드들 설정하고 컨트롤러 HttpServletRequest httpServletRequest
에서 값 뽑기
----------------위에껀 크게 도움 안됨--------------
스프링에서
{a:a1
,b:[{b1:b1,b2:b2},{b1:b1,b2:b2}}]
}
이 런 족같은 데이터는 쌩 request로 받으면 파싱이 안되고
객체로 받으려하면 타입 안맞는다고 지랄함
@RequestMapping(value = "/test_formdata", method = RequestMethod.POST)
@ResponseBody
public String testpost(TestFormData testFromData,HttpServletRequest request, Model model) throws URISyntaxException, JsonMappingException, JsonProcessingException {
System.out.println("## testFromData: "+testFromData.toString());
return "test_formdata";
}
public class TestFormData {
private String t1;
private List<TestFormData_T2> t2=new ArrayList<TestFormData_T2>();
}
- jsp
<form id="test_formdata_form">
</form>
var formdatatest=function(){
var html1=`
<input name="t1" value="t1"/>
<input name="t2[0].a" value="a1"/>
<input name="t2[0].b" value="b2"/>
<input name="t2[1].a" value="a2"/>
<input name="t2[1].b" value="b2"/>
`;
$("#test_formdata_form").append(html1);
$.ajax({
url:"/test_formdata",
method: "POST",
data: $("#test_formdata_form").serialize(),
//dataType: 'json',
//processData: false,
//contentType: false,
success: function () {
console.log("completed!");
},
error: function () {
alert("failed! ")
}
});
}
--- 스타일2
//그냥 컨트롤러(List<TestFormData2) testFormData2)
이렇게 받으려하면 list 객체 초기화 안됬다고 지랄남
@RequestMapping(value = "/test_formdata2", method = RequestMethod.POST)
@ResponseBody
public String testpost2(TestFormData2List testFromDataList,HttpServletRequest request, Model model) throws URISyntaxException, JsonMappingException, JsonProcessingException {
Enumeration params = request.getParameterNames();
System.out.println("----------------------------");
while (params.hasMoreElements()){
String name = (String)params.nextElement();
System.out.println(name + " : " +request.getParameter(name).toString());
}
System.out.println("----------------------------");
System.out.println("## testFromDataList: "+testFromDataList.toString());
return "test_formdata";
}
public class TestFormData2List {
List<TestFormData2> testFormData2=new ArrayList<TestFormData2>();
}
-jsp
var formdatatest2=function(){
var html1=`
<input name="testFormData2[0].a1" value="-1"/>
<input name="testFormData2[0].a2" value="a2"/>
<input name="testFormData2[1].a1" value="-1"/>
<input name="testFormData2[1].a2" value="a2"/>
`;
$("#test_formdata_form2").append(html1);
$.ajax({
url:"/test_formdata2",
method: "POST",
data: $("#test_formdata_form2").serialize(),
//dataType: 'json',
//processData: false,
//contentType: false,
success: function () {
console.log("completed!");
},
error: function () {
alert("failed! ")
}
});
}
출처: https://howtolivelikehuman.tistory.com/169 [howtolivelikeHUMAN]
댓글
댓글 쓰기