6/05/2011

Laguna Beach 드라이브

오늘 오후에 잠깐 집 근처의 라구나 비치 구경을 다녀왔습니다. 기록도 할 겸 사진 몇장 올려봅니다. DSC 0651 DSC 0656 DSC 0661 DSC 0671 DSC 0677 DSC 0678 DSC 0693 DSC 0698 DSC 0709 DSC 0720 DSC 0721 DSC 0726 DSC 0731 DSC 0755 DSC 0760

6/03/2011

Spring3에서 @RequestMapping시 URI tempate이 제대로 먹히지 않는 경우

@RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
public String findOwner(@PathVariable String ownerId, Model model) {
  Owner owner = ownerService.findOwner(ownerId);  
  model.addAttribute("owner", owner);  
  return "displayOwner"; 
}
The URI Template "/owners/{ownerId}" specifies the variable name ownerId. When the controller handles this request, the value of ownerId is set to the value in the request URI. For example, when a request comes in for /owners/fred, the value fred is bound to the method parameter String ownerId.

The matching of method parameter names to URI Template variable names can only be done if your code is compiled with debugging enabled. If you do not have debugging enabled, you must specify the name of the URI Template variable name in the @PathVariable annotation in order to bind the resolved value of the variable name to a method parameter.

http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html




요약 : Spring3에서 아래와 같이 작성을 해주면 /owners/1234 와 같이 URL이 GET으로 호출되는 경우 String ownerId 에 1234 라는 값이 자동으로 저장된다. 하지만 이건 debug enabled로 컴파일 되었을 때의 경우이고(디버깅용으로 생성된 정보를 이용해서 매칭해 준다는 거지), 제대로 하려면 @PathVariable("ownerId") 처럼 해줘야 한다는 것. 이 경우 실제 변수명은 꼭 ownerId가 아니어도 상관없다.(당연)  즉, String ownerId 이든 String theOwner이든 앞에 @PathVariable("ownerId") 만 URI tempate의 것과 맞으면 값을 할당됨.

은근히 처음 방법으로 코딩해 놓은 것들을 많이 찾아볼 수 있는데, 항상 디버깅 정보를 넣은 채로 컴파일이 되는 건 아니니까 항상 이름을 명시해주는 방식으로 코딩하자.

-P.S-
그러고보니 이 블로그에 이런 내용을 쓰는건 진짜 백만년만인듯...

요즘 포스팅을 모두 medium쪽으로 하고 있습니다

안녕하세요? 어쩌다보니 그냥 한번 써보기로 한 medium.com 에다가 죄다 최근 포스팅을 하고 있습니다. 현재 도메인인 potatosoft.com 은 제가 대학때인가 사회 취업한지 얼마 안됐을때부터 유지해 온 도메인이고, 블로그 시스템은 그...