Spring MVC 3:发现模糊映射
问题内容:
我正在使用Spring MVC 3.1并测试不同的功能。我想验证以下来自@ RequestMapping#value
doc的
语句
If you have a single default method (without explicit path mapping), then all requests without a more specific mapped method found will be dispatched to it. If you have multiple such default methods, then the method name will be taken into account for choosing between them
因此,我使用多个默认处理程序方法创建了以下控制器。
@Controller
@RequestMapping("/book")
public class BookController {
@RequestMapping
public @ResponseBody String greet() {
return "Hi Book!";
}
@RequestMapping
public @ResponseBody String meet() {
return "Nice to meet you Book!";
}
}
这是Web应用程序上下文配置
<beans ....>
<!-- Use @Component annotations for bean definitions -->
<context:component-scan base-package="com.botreeconsulting.lms.web"/>
<!-- Use @Controller annotations for MVC controller definitions -->
<mvc:annotation-driven />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
但是似乎我搞砸了一些东西,因为它在部署时给我以下错误:
java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'bookController' bean method
public java.lang.String com.botreeconsulting.lms.web.BookController.meet()
to {[/book],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}: There is already 'bookController' bean method
public java.lang.String com.botreeconsulting.lms.web.BookController.greet() mapped.
现在的问题是该控制器对文件中的内容进行建模吗?我觉得我做得不好。请指导我为控制器建模,以匹配有关多个默认处理程序的语句。
谢谢阿米特
问题答案:
如果你有一个控制器,如下面给出的,比其他所有的请求/book/edit
将被引导到mydefault()
,而/book/edit
将被发送到meet()
。
@Controller
@RequestMapping("/book")
public class BookController {
@RequestMapping
public @ResponseBody String mydefault() {
return "Hi Book!";
}
@RequestMapping("/edit")
public @ResponseBody String meet() {
return "Nice to meet you Book!";
}
}
在您的示例中,您有两种没有显式路径映射的方法。