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]
Viking
  • 177
  • 5
  • 16

3 Answers3

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.

dunni
  • 43,386
  • 10
  • 104
  • 99
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.

Community
  • 1
  • 1
Andremoniy
  • 34,031
  • 20
  • 135
  • 241
0

Try providing a @RequestBody to seeTest() method

Manish
  • 909
  • 1
  • 11
  • 23