2

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]
1
  • read the exception. as exceptions go, that's a pretty readable one, and tells you exactly what's wrong. Commented Jan 19, 2017 at 12:13

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

Try providing a @RequestBody to seeTest() method

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.