Ami*_*tel 15 spring-mvc spring-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
Run Code Online (Sandbox Code Playgroud)
所以我用多个默认处理程序方法创建了以下控制器
@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!";
}
}
Run Code Online (Sandbox Code Playgroud)
这是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>
Run Code Online (Sandbox Code Playgroud)
但似乎我弄乱了一些东西,因为它在部署时给了我跟随错误:
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.
Run Code Online (Sandbox Code Playgroud)
现在问题是这个控制器是否模拟了文档中的内容?我觉得我没弄错.请指导我对控制器进行建模以匹配有关多个默认处理程序的语句.
谢谢,阿米特
Aru*_*hny 26
如果你有一个控制器,如下面给出的,比其他所有的请求/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!";
}
}
Run Code Online (Sandbox Code Playgroud)
在您的示例中,您有两种方法没有显式路径映射.
Arun,你的答案是正确的,需要注意的是在Spring 3.1中,它取决于配置哪个HandlerMapping-HandlerAdapter对.
DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter支持所描述的行为,这些行为自Spring 2.5以来一直在使用,并且在没有定义其他HandlerMapping和HandlerAdapter bean时仍默认启用.
Spring 3.1中添加的RequestMappingHandlerMapping和RequestMappingHandlerAdapter(参见Spring 3.1参考文档)作为前者的替代,不支持相同的行为 - 即在模糊映射的情况下回退到方法名称以及使用默认方法(当没有定义明确的映射).默认情况下,新的HandlerMapping-HandlerAdapter对从MVC命名空间和MVC Java配置启用,建议继续使用.
Arun引用的Java文档需要更新.我为SPR-9042创建了一张票.
归档时间: |
|
查看次数: |
48120 次 |
最近记录: |