캠핑과 개발


Spring 2.5에서는 annotation이 강화 되었다. 특히 SpringMVC는 상당히 많은 변화가 있다.
(POJO 기반의 Controller작성이 가능한)  아래 예제처럼 말이다.

Controller

@Controller
public class ChargeTableController  {

 @Autowired
 public ChargeFacade chargeFacade;

 @RequestMapping("/charge/chargeTableList.do")
 public String chargeTableList(ModelMap model, HttpServletRequest request){
  int currentPage = ServletRequestUtils.getIntParameter(request, "currentPage", 0);
  int countPerPage = ServletRequestUtils.getIntParameter(request, "countPerPage", 10);
 
  ChargeTableResult result = chargeFacade.chargeTableList(currentPage, countPerPage);
  model.addAttribute("result", result );
return "/charge/chargeTableList";
 .......

그래서 본격적으로 프로젝트 기본 템플릿을 만들기로 했다. 그러나 템플릿 예제를 만들던중 문제가 생겼다.  문제는 이렇다.

보통 어떤 행위가 없는(텅빈) Controller를 만들어 SimpleUrlHandlerMapping에 매핑해야 할때가 있다. 요청하는 행위가 단순히  Spring DispatcherServlet통해서 html이나 jsp를 매핑만 시켜주는 행위만 필요하다면 편리하게 처리하는 방법이 있다. 바로 UrlFilenameViewController 이다. 보통 아래와 같이 사용한다.

    <bean id="urlFilenameViewController" class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />

 <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  <property name="mappings">
       <props>
       <prop key="*.html">urlFilenameViewController</prop>     
       </props>
   </property>
 </bean>


이렇게 설정만 하면 수많은 html,jsp파일을 Controller없이 매핑 시킬수 있다.

그런데 Spring2.5의 Annotation기반으로 SpringMVC를 사용하면 위와 같은 설정이 안먹힌다.
포럼에도 이것 때문에 삽질한(나를포함) 사람들이 좀 있는것 같다.
왜그럴까? 하고 고민하고 학습 해야하는데 못했다.-_-;
그러나 해결방법은 간단하다. DefaultAnnotationHandlerMapping을 추가해 주면된다.

    <bean id="urlFilenameViewController" class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
 <bean id="annotationMapper" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
   <property name="order" value="2" />
</bean>
 <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  <property name="mappings">
       <props>
       <prop key="*.html">urlFilenameViewController</prop>     
       </props>
   </property>
  <property name="order" value="1" />
 </bean>

만약 Mapping 순서를 정하고 싶을때 order 속성으로 우선순위를 정할 수 있다.

출처 : http://yunsunghan.tistory.com/135