我试图为 spring 设置一个基于约定的映射,但它总是给我带来不明确的映射错误。 这是我的 Controller 类:
package com.bsci.tracker.mvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class BaseController {
@RequestMapping
public void index(){}
@RequestMapping
public void home(){}
}
这是我的 xml 配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.bsci.tracker.mvc" />
<mvc:annotation-driven />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
此外,我不想在 Controller 方法上提供显式请求映射,我希望它们能够通过方法名称自动调用。
最佳答案
确实如此,因为您有两个 @RequestMapping
映射映射到同一默认路径,如果我没记错的话,它是“/”。
您需要执行以下操作:
@Controller
public class BaseController {
@RequestMapping(value="/yourmapping1",method=RequestMethod.GET)
public void index(){}
@RequestMapping(value="/yourmapping2",method=RequestMethod.POST)
public void home(){}
}
希望有帮助。
关于java - Spring MVC 3 模糊映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10371029/