[英]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. 我想将“值”从单选按钮之一发送到SpringMVC,但是我看到了意料之外的问题。 And I has read similar answers on my topic, but I hasn't found the answer on my question HTML: 我在主题上也读过类似的答案,但是在我的问题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: 尝试使用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
. 您已经add
addTestController
类中的addTestController
方法映射到/test
路径。 So change either the path in addTestController
or in testController
. 因此,请更改addTestController
或testController
的路径。 Also you might want to stick to Java naming conventions and start class names with an upper case letter. 另外,您可能希望遵守Java命名约定,并以大写字母开头类名称。
As @dunni mentioned , there is another controller addTestController
where you have duplicated methods. 如@dunni 所述 ,还有另一个控制器addTestController
,其中您有重复的方法。
Additionally you should remove RequestMethod.POST
from seeTest()
method, and remove RequestMethod.GET
from add(...)
: 此外,您应该删除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){ ... }
That's because your form send data using POST
method, so no need for declaring GET
resource for /addRadio
. 那是因为您的表单使用POST
方法发送数据,因此不需要为/addRadio
声明GET
资源。 In the same time for just retrieving page with form GET
method should be used. 在同一时间只应使用GET
方法检索页面。
尝试提供@RequestBody到seeTest()方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.