2012-01-18 82 views
15

我在玩Spring MVC 3.1并测试不同的功能。我想验证下从@RequestMapping#value docSpring MVC 3:发现模糊映射

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. 

现在的问题这个控制器是模拟文档中写入的内容吗?我觉得我没有得到正确的。请指导我对控制器进行建模以匹配关于多个默认处理程序的声明。

谢谢,阿米特

回答

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!"; 
    } 
} 

在您的示例中,您有两个方法没有显式路径映射。

+0

'mydefault()'将处理/ book only not/book/abc。 –

+0

Arun,'如果你有多种这样的默认方法......'。是否可以配置多个默认方法? –

+0

你是什么意思的多个默认值?在任何设计中只会有一个默认的 –

5

Arun,你的回答是正确的,在Spring 3.1中它取决于哪个HandlerMapping-HandlerAdapter配置。

所描述的行为受该DefaultAnnotationHandlerMapping & AnnotationMethodHandlerAdapter上已投入使用以来Spring 2.5中,并仍在默认启用没有其他的HandlerMapping和豆类的HandlerAdapter定义的时候。

在Spring 3.1(请参阅Spring 3.1参考文档)中添加的RequestMappingHandlerMapping和RequestMappingHandlerAdapter替代前者不支持相同的行为 - 即在模糊映射的情况下回退方法名称以及具有默认方法(当没有定义显式映射时)。新的HandlerMapping-HandlerAdapter对默认从MVC命名空间和MVC Java配置启用,并推荐用于未来的使用。

Arun引用的Java文档需要更新。我为这个SPR-9042创建了一张票。

+0

中添加了mvc配置Hi,Rossen。您能否澄清一下'MVC命名空间默认启用的语句' - 您是指''标记,还是标记只注册另一对HandlerMapping/HandlerAdapter? –

+0

另外,你可以请看看http://stackoverflow.com/questions/13671776/spring-mvc-why-this-hello-world-run-well-without-annotation-driven-tag-unlike-a - 在这问题控制器只用'@Controller'注释,而其单一方法注释为'@RequestMapping(“/ hello”)'我是否正确理解此配置只能在3.1之前的版本中工作(没有''标签)因为Spring 3.1的变化? –