简体   繁体   中英

Spring MVC Ambiguous mapping. Cannot map

I want to send "Value" from one of the Radio Buttons to SpringMVC, but I watch the problem where I didn't expect. And I has read similar answers on my topic, but I hasn't found the answer on my question 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>

Try recieve in 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;
}

} And I recieve this mistake:

        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]

You already mapped the method add in the class addTestController to the path /test . So change either the path in addTestController or in testController . Also you might want to stick to Java naming conventions and start class names with an upper case letter.

As @dunni mentioned , there is another controller addTestController where you have duplicated methods.

Additionally you should remove RequestMethod.POST from seeTest() method, and remove RequestMethod.GET from add(...) :

@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){ ... }

That's because your form send data using POST method, so no need for declaring GET resource for /addRadio . In the same time for just retrieving page with form GET method should be used.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM