【问题标题】:Spring MVC Ambiguous mapping. Cannot mapSpring MVC 模糊映射。无法映射
【发布时间】:2017-01-19 12:04:45
【问题描述】:

我想将“值”从其中一个单选按钮发送到 SpringMVC,但我看到了我没想到的问题。 我已经阅读了关于我的主题的类似答案,但我还没有找到关于我的问题的答案 HTML:

 <form action="addRadio"  method="post" >

            <input type="radio" name="rbn" id="rbn" value="Red"/>Red
            <input type="radio" name="rbn"  value="White"/>White
            <input type="radio" name="rbn"  value="Blue"/>Blue
            <input type="radio" name="rbn"  value="Yellow"/>Yellow

            <input type="submit" value="Submit"/>

    </form>

尝试在 Java 中接收:

@Controller
public class testController {
@RequestMapping(name = "/test", method = {RequestMethod.GET, RequestMethod.POST})
public ModelAndView seeTest(){
    ModelAndView m = new ModelAndView();
    m.setViewName("addFabric/testRadio");
    return m;
}
@RequestMapping(name = "/addRadio", method = {RequestMethod.GET, RequestMethod.POST})
public ModelAndView add(@RequestParam(value = "rbn", required = false) String name){
    ModelAndView modelAndView = new ModelAndView();
    System.out.println("Type Radio: "+name);
    modelAndView.setViewName("index");
    return modelAndView;
}

} 我收到了这个错误:

        org.springframework.beans.factory.BeanCreationException: Error creating bean     with name 'requestMappingHandlerMapping'
 defined in class path resource   [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: 
Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map '/test' method 
    public org.springframework.web.servlet.ModelAndView margo.controller.add.testController.seeTest()
    to {[],methods=[GET || POST]}: There is already 'addTestController' bean method
    public org.springframework.web.servlet.ModelAndView margo.controller.add.addTestController.add(java.lang.String) mapped.
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1589) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]

【问题讨论】:

  • 读取异常。作为例外,这是一个非常易读的,并告诉你究竟出了什么问题。

标签: java spring


【解决方案1】:

您已经将addTestController 类中的方法add 映射到路径/test。所以更改addTestController 或testController 中的路径。此外,您可能希望遵守 Java 命名约定并以大写字母开头的类名。

【讨论】:

    【解决方案2】:

    作为@dunni mentioned,还有另一个控制器addTestController 有重复的方法。

    此外,您应该从seeTest() 方法中删除RequestMethod.POST,并从add(...) 中删除RequestMethod.GET:

    @RequestMapping(name = "/test", method = RequestMethod.GET)
    public ModelAndView seeTest(){ ... }
    
    @RequestMapping(name = "/addRadio", method = RequestMethod.POST)
    public ModelAndView add(@RequestParam(value = "rbn", required = false) String name){ ... }
    

    这是因为您的表单使用POST 方法发送数据,因此无需为/addRadio 声明GET 资源。同时要使用表单GET 方法检索页面。

    【讨论】:

      【解决方案3】:

      尝试为 seeTest() 方法提供@RequestBody

      【讨论】:

        猜你喜欢
        • 2017-10-12
        • 1970-01-01
        • 1970-01-01
        • 2021-09-16
        • 2012-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多