使用注释会很容易:
@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>
任何指针都会受到赞赏。
答案 0 :(得分:1)
使用SimpleUrlHandlerMapping无法指定http方法。可能你必须使用Spring MVC REST项目中的MethodUrlHandlerMapping等其他映射(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>
您可以在其页面中看到该示例:
http://spring-mvc-rest.sourceforge.net/introduction.html
请看第2部分。
答案 1 :(得分:0)
您的@RequestMapping
注释应该有效。只需从xml配置中删除handlerMapping bean并启用MVC注释。
以下是示例配置。 将base-package更改为包含控制器类的包
<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>