我想发送"价值"从Radio按钮之一到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]
答案 0 :(得分:2)
您已将类add
中的方法addTestController
映射到路径/test
。因此,请更改addTestController
或testController
中的路径。此外,您可能希望坚持Java命名约定并使用大写字母启动类名。
答案 1 :(得分:1)
作为@dunni mentioned,还有另一个控制器addTestController
,您可以在其中使用重复的方法。
此外,您应该从RequestMethod.POST
方法移除seeTest()
,并从RequestMethod.GET
移除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){ ... }
这是因为您的表单使用POST
方法发送数据,因此无需为GET
声明/addRadio
资源。同时,只需使用表单GET
检索页面即可。
答案 2 :(得分:0)
尝试提供@RequestBody以查看Test()方法