2013-07-05 3 views
1

이에 컨트롤러와 OPTIONS 요청에있는 하나의 방법에 GET 요청을지도로 :구성 스프링 MVC는 아주 쉽게 사용 주석 것 다른 방법

@Controller 
public class MyController { 

    @RequestMapping(value="/hitmycontroller", method= RequestMethod.OPTIONS) 
    public static void options(HttpServletRequest req,HttpServletResponse resp){ 
    //Do options 
    } 
    @RequestMapping(value="/hitmycontroller", method= RequestMethod.GET) 
    public static void get(HttpServletRequest req,HttpServletResponse resp){ 
    //Do get 
    } 
} 

하지만 XML에서이 작업을 수행하는 방법을 찾을 수 없습니다. 다음과 같은 작업을 수행 할 매핑 핸들러가 있습니까 :

<bean id="handlerMapping" 
    class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
    <property name="mappings"> 
     <mapping> 
     <url>/hitmycontroller</url> 
     <httpMethod>GET</httpMethod> 
     <method>get</method> 
     <controller>MyController</controller> 
     </mapping> 
     <mapping> 
     <url>/hitmycontroller</url> 
     <httpMethod>OPTIONS</httpMethod> 
     <method>options</method> 
     <controller>MyController</controller> 
     </mapping> 
    </property> 
</bean> 

모든 포인터가 도움이 될 것입니다.

답변

1

SimpleUrlHandlerMapping을 사용하면 http 메서드를 지정할 수 없습니다. 아마도 Spring MVC REST 프로젝트 (http://spring-mvc-rest.sourceforge.net/)의 MethodUrlHandlerMapping과 같은 다른 매핑을 사용해야 할 것이다.

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
    <property name="mappings"> 
     <props> 
      <prop key="GET /hitmycontroller">MyController</prop> 
      <prop key="OPTIONS /hitmycontroller">MyController</prop> 
     </props> 
    </property> 
</bean> 

당신은 자신의 페이지에있는 예를 볼 수 있습니다 : 파트 2에서

http://spring-mvc-rest.sourceforge.net/introduction.html

방법

은 다음과 같이되어야 MethodUrlHandlerMapping를 사용하여 매핑을 선언합니다.

0

@RequestMapping 특수 효과가 작동해야합니다. xml 구성에서 handlerMapping bean을 삭제하고 MVC 주석을 활성화하십시오.

다음은 샘플 구성입니다. 컨트롤러 클래스를 포함하는 패키지변경 기본 패키지

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 

    xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans  
     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.2.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> 

    <context:component-scan base-package="your.package" /> 
    <mvc:annotation-driven> 
</beans>