본문 바로가기

Java

Controller로 어떤 parameter들이 넘어오는지 확인하는 방법 (parameter 모두 출력하기)

특히 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

반응형