问题描述
早上好,我正在处理一个模糊映射,我无法解码...
我使用Spring mvc 4.0.6和hibernate 4.3.6
我得到这个错误
ERROR [localhost-startStop-2]:上下文初始化失败
org.springframework。 beans.factory.BeanCreationException:类org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfigurationrequestMappingHandlerMapping定义错误创建与名称豆:init方法的调用失败;嵌套异常是java.lang.IllegalStateException:找到模糊映射。无法映射'appController'bean方法
public java.lang.String it.besmart.controller.AppController.newClient(org.springframework.ui.ModelMap)
to {[// new],methods = [ POST],params = [],headers = [],consumes = [],生产= [],custom = []}:已经有'appController'bean方法
public java.lang.String it.besmart .controller.AppController.saveClient(it.besmart.models.Client,org.springframework.validation.BindingResult,org.springframework.ui.ModelMap)映射。在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553)
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
在org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject
(AbstractBeanFactory.java:302 )美元,org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228 b $ b)
在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298 )
处org.springframework.beans.factory.support.DefaultListableBeanFactory org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
。 preInstantiateSingletons(DefaultListableBeanFactory.java:703)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext。 Java的:482),美元,org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403 b $ b)
在org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)在org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
在org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4727)
在org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5167)
在org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
在org.apache .catalina.core.ContainerBase.addChi ldInternal(ContainerBase.java:725)
在org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
在org.apache.catalina.core.StandardHost.addChild(StandardHost。 Java的:在org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:945 717)
)
在org.apache.catalina.startup.HostConfig $ DeployWar.run(HostConfig.java: 1768)
在java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:511)
在java.util.concurrent.FutureTask.run(FutureTask.java:266)
在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:617)
在java.lang.Thread中.run(Thread.java:744)
引起:java.lang.IllegalStateException:找到模糊映射。无法映射'appController'bean方法
public java.lang.String it.besmart.controller.AppController.newClient(org.springframework.ui.ModelMap)
to {[// new],methods = [ POST],params = [],headers = [],consumes = [],生产= [],custom = []}:已经有'appController'bean方法
public java.lang.String it.besmart .controller.AppController.saveClient(it.besmart.models.Client,org.springframework.validation.BindingResult,org.springframework.ui.ModelMap)映射。在org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.registerHandlerMethod(AbstractHandlerMethodMapping.java:192)
在org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.detectHandlerMethods(AbstractHandlerMethodMapping.java:164)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initHandlerMethods(AbstractHandlerMethodMapping.java:124)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.afterPropertiesSet(AbstractHandlerMethodMapping.java:103)
在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.afterPropertiesSet(RequestMappingHandlerMapping.java:126)
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory。 java:1612)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFa ctory.java:1549)
... 25 more
我不明白为什么我收到这个错误。 AppController非常直观
package it.besmart.controller;
导入it.besmart.models.Client;
导入it.besmart.service.ClientService;
import java.util.List;
import java.util.Locale;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping(/)
公共类AppController {
$ b $ @Autowired
ClientService clientService;
@Autowired
MessageSource messageSource;
@RequestMapping(value = {/,/ list},method = RequestMethod.GET)
public String listClients(ModelMap model){
List< Client> clients = clientService.findAllClients();
model.addAttribute(clients,clients);
返回allclients;
$ b @RequestMapping(value = {/ new},method = RequestMethod.POST)
public String newClient(ModelMap model){
Client client =新客户端();
model.addAttribute(client,client);
model.addAttribute(edit,false);
返回注册;
$ b @RequestMapping(value = {/ new},method = RequestMethod.POST)
public String saveClient(@Valid客户端客户端,BindingResult结果,ModelMap模型){
if(result.hasErrors()){
returnregistration;
}
clientService.saveClient(client);
model.addAttribute(success,Client+ client.getNomeClient()+registrato correttamente);
返回成功;
$ b @RequestMapping(value = {/ edit- {name} -client},method = RequestMethod.POST)
public字符串updateClient(@Valid客户端客户端,BindingResult结果,ModelMap模型,@PathVariable字符串名称){
if(result.hasErrors()){
returnregistration;
$ b $ if(!clientService.isClientNameUnique(client.getIdClient(),client.getNomeClient())){
FieldError idErr = new FieldError(client,name ,messageSource.getMessage(non.unique.nome_client,new String [] {client.getNomeClient()},Locale.getDefault()));
result.addError(idErr);
返回注册;
}
clientService.saveClient(client);
model.addAttribute(success,Client+ client.getNomeClient()+aggiornato correttamente);
返回成功;
$ b @RequestMapping(value = {/ delete- {id} -client},method = RequestMethod.GET)
public String deleteClient(@ PathVariable int id){
clientService.deleteClientById(id);
返回redirect:/ list;
}
}
ClientService.java
package it.besmart.service;
导入it.besmart.models.Client;
import java.util.List;
public interface ClientService {
客户端findById(int id);
void saveClient(客户端客户端);
void updateClient(客户端客户端);
void deleteClientById(int id);
列表< Client> findAllClients();
客户端findClientByName(String name);
boolean isClientNameUnique(Integer id,String name);
$ / code>
它看起来对我来说很简单,我对这种应用程序很陌生。
Thanks
这是您收到的错误消息:
它告诉你你正在映射多个方法来处理 POST
到URL / new
。如果Web浏览器向URL / new
发出 POST
请求,那么您的哪些方法应该处理它?
以下是两个违规方法:
@RequestMapping(value = { / new},method = RequestMethod.POST)
public String newClient(ModelMap model){
Client client = new Client();
model.addAttribute(client,client);
model.addAttribute(edit,false);
返回注册;
$ b @RequestMapping(value = {/ new},method = RequestMethod.POST)
public String saveClient(@Valid客户端客户端,BindingResult结果,ModelMap模型){
if(result.hasErrors()){
returnregistration;
}
clientService.saveClient(client);
model.addAttribute(success,Client+ client.getNomeClient()+registrato correttamente);
返回成功;
}
我怀疑第一个是不正确的;您可能希望使用 RequestMethod.GET
代替 RequestMethod.POST
。
Good morning all, i'm dealing with an Ambiguous mapping i cannot decode...I'm using Spring mvc 4.0.6 and hibernate 4.3.6I'm getting this error while launching the war in tomcat:
ERROR [localhost-startStop-2]: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'appController' bean method
public java.lang.String it.besmart.controller.AppController.newClient(org.springframework.ui.ModelMap)
to {[//new],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}: There is already 'appController' bean method
public java.lang.String it.besmart.controller.AppController.saveClient(it.besmart.models.Client,org.springframework.validation.BindingResult,org.springframework.ui.ModelMap) mapped.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4727)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5167)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:945)
at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1768)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'appController' bean method
public java.lang.String it.besmart.controller.AppController.newClient(org.springframework.ui.ModelMap)
to {[//new],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}: There is already 'appController' bean method
public java.lang.String it.besmart.controller.AppController.saveClient(it.besmart.models.Client,org.springframework.validation.BindingResult,org.springframework.ui.ModelMap) mapped.
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.registerHandlerMethod(AbstractHandlerMethodMapping.java:192)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.detectHandlerMethods(AbstractHandlerMethodMapping.java:164)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initHandlerMethods(AbstractHandlerMethodMapping.java:124)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.afterPropertiesSet(AbstractHandlerMethodMapping.java:103)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.afterPropertiesSet(RequestMappingHandlerMapping.java:126)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549)
... 25 more
I can't understand why i'm getting this error. AppController is quite straight
package it.besmart.controller;
import it.besmart.models.Client;
import it.besmart.service.ClientService;
import java.util.List;
import java.util.Locale;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/")
public class AppController {
@Autowired
ClientService clientService;
@Autowired
MessageSource messageSource;
@RequestMapping(value = { "/", "/list" }, method = RequestMethod.GET)
public String listClients(ModelMap model){
List<Client> clients = clientService.findAllClients();
model.addAttribute("clients", clients);
return "allclients";
}
@RequestMapping(value = {"/new"}, method = RequestMethod.POST)
public String newClient(ModelMap model){
Client client = new Client();
model.addAttribute("client", client);
model.addAttribute("edit", false);
return "registration";
}
@RequestMapping(value = {"/new"}, method = RequestMethod.POST)
public String saveClient(@Valid Client client, BindingResult result, ModelMap model){
if(result.hasErrors()){
return "registration";
}
clientService.saveClient(client);
model.addAttribute("success", "Client" + client.getNomeClient() + "registrato correttamente");
return "success";
}
@RequestMapping(value = { "/edit-{name}-client"}, method = RequestMethod.POST)
public String updateClient(@Valid Client client, BindingResult result, ModelMap model, @PathVariable String name ){
if(result.hasErrors()){
return "registration";
}
if(!clientService.isClientNameUnique(client.getIdClient(), client.getNomeClient())){
FieldError idErr = new FieldError("client", "name", messageSource.getMessage("non.unique.nome_client", new String[]{client.getNomeClient()}, Locale.getDefault()));
result.addError(idErr);
return "registration";
}
clientService.saveClient(client);
model.addAttribute("success", "Client" + client.getNomeClient() + "aggiornato correttamente");
return "success";
}
@RequestMapping(value = { "/delete-{id}-client" }, method = RequestMethod.GET)
public String deleteClient(@PathVariable int id){
clientService.deleteClientById(id);
return "redirect:/list";
}
}
ClientService.java
package it.besmart.service;
import it.besmart.models.Client;
import java.util.List;
public interface ClientService {
Client findById(int id);
void saveClient(Client client);
void updateClient(Client client);
void deleteClientById(int id);
List <Client> findAllClients();
Client findClientByName(String name);
boolean isClientNameUnique(Integer id, String name);
}
It looks to me like it's quite straight everything... I'm quite new to this kind of application..Thanks
This is the error message you are getting:
It's telling you you're mapping more than one method to handle a POST
to the URL /new
. If the web browser makes a POST
request to the URL /new
, which of your methods should handle it?
Here are the two offending methods:
@RequestMapping(value = {"/new"}, method = RequestMethod.POST)
public String newClient(ModelMap model){
Client client = new Client();
model.addAttribute("client", client);
model.addAttribute("edit", false);
return "registration";
}
@RequestMapping(value = {"/new"}, method = RequestMethod.POST)
public String saveClient(@Valid Client client, BindingResult result, ModelMap model){
if(result.hasErrors()){
return "registration";
}
clientService.saveClient(client);
model.addAttribute("success", "Client" + client.getNomeClient() + "registrato correttamente");
return "success";
}
I suspect that the first of these is incorrect; you probably want to use RequestMethod.GET
instead of RequestMethod.POST
for that.
这篇关于引起:java.lang.IllegalStateException:找到模糊映射。无法映射'appController'bean方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!