특히 ajax 와 데이터를 주고 받을 때,
컨트롤러로 어떤 데이터들이 넘어오는지 확인하기 어려울 때가 있다.
넘어오는 값들을 어떻게 받아야 하는지 잘 되지 않을 때가 있다.
어떤 변수 이름으로 어떤 값들이 넘어올까?
모든 파라미터들을 하나씩 꺼내서 둘러볼 수 있다면
데이터를 주고 받기 편할 것이다.
다음은 Java Spring Framework의 controller에서 request로부터
parameter들을 하나씩 꺼내서 조회하는 코드다.
넘어오는 값들을 확인하기 어려울 때 사용하면 좋다.
특히 ajax로 array가 넘어올 때는
"testArray[]" 이런 식으로 넘어오는데,
이렇게 key 값들을 확인하기에 좋다
@RequestMapping(value="sendFruit", method={RequestMethod.POST})
@ResponseBody
public String sf(HttpServletRequest request) {
Enumeration<?> param = request.getParameterNames();
while (param.hasMoreElements()){
String name = (String)param.nextElement();
System.out.println(name + " : " +request.getParameter(name));
}
return "response success";
}
- 220408
반응형
'Java' 카테고리의 다른 글
[Java] lambda 에러 : Local variable doesntExistFlag defined in an enclosing scope must be final or effectively final (0) | 2022.04.18 |
---|---|
JSTL 에서 forEach 사용할 때 index값 받아오기 (0) | 2022.04.17 |
ajax로 배열 넘기기 / controller에서 arraylist 받기 (@RequestParam) (2) | 2022.04.14 |
파라미터 DTO를 만들어서 파라미터 여러 개를 한 번에 받기 (0) | 2022.04.14 |
Spring이 service 인터페이스와 serviceImpl 를 연결하는 법 (0) | 2022.04.02 |