我无法在这里看到模糊映射问题的位置。我在我的控制器中使用了新的Spring MVC注释@GetMapping和@PostMapping,因为我将Spring版本更改为5.0.0M1。
WebXml.java
public class WebXml implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) throws ServletException {
AnnotationConfigWebApplicationContext webXml = new AnnotationConfigWebApplicationContext();
webXml.register(WebAppConfig.class);
DispatcherServlet dispatcherServlet = new DispatcherServlet(webXml);
dispatcherServlet.setContextConfigLocation("/WEB-INF/applicationContext.xml");
ServletRegistration.Dynamic dispatcher = container.addServlet("mvc-dispatcher", dispatcherServlet);
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
container.addListener(new ContextLoaderListener(webXml));
/** This is useful because current browsers typically do not - set a
character encoding even if specified in the HTML page or form */
Dynamic filter = container.addFilter("encodingFilter", CharacterEncodingFilter.class);
filter.addMappingForUrlPatterns(null, Boolean.FALSE, "/*");
filter.setInitParameter("forceEncoding", "true");
}
}
AbstractPessoaController
public abstract class AbstractPessoaController extends AbstractController {
protected final Logger LOG = LoggerFactory.getLogger(getClass());
@Override
protected void init(WebDataBinder binder) {
SimpleDateFormat df = new SimpleDateFormat(DateUtil.DD_MM_YYYY);
binder.registerCustomEditor(Date.class, new CustomDateEditor(df, Boolean.TRUE));
binder.registerCustomEditor(String.class, "celular", new TelefoneEditorSupport());
}
protected void criaPessoaPadrao(Pessoa pessoa) {
pessoa.gerarCodigo();
pessoa.setSexo(Sexo.MASCULINO);
Usuario usuario = new Usuario();
pessoa.setUsuario(usuario);
usuario.setPessoa(pessoa);
}
}
AlunoController。
@Controller
@RequestMapping("/aluno")
public class AlunoController extends AbstractPessoaController {
public static final String ALUNO_LISTA = "aluno-lista";
public static final String ALUNO_CADASTRO = "aluno-cadastro";
public static final String ALUNO_HOME = "aluno.home";
@Autowired
private AlunoService service;
@GetMapping
public String cadastro(Model model) {
Aluno aluno = new Aluno();
criaPessoaPadrao(aluno);
model.addAttribute("aluno", aluno);
adicionaSexoAoModel(model);
return ALUNO_CADASTRO;
}
@GetMapping("/lista")
public String lista(Model model) {
List<Aluno> alunos = service.findAll();
model.addAttribute("alunos", alunos);
return ALUNO_LISTA;
}
@PostMapping
public String cadastroSubmit(Model model, @Valid Aluno aluno, BindingResult result) {
model.addAttribute("aluno", aluno);
adicionaSexoAoModel(model);
if (result.hasErrors()) {
return ALUNO_CADASTRO;
}
gravar(aluno, model);
return ALUNO_CADASTRO;
}
private void gravar(Aluno aluno, Model model) {
try {
service.gravar(aluno);
mensagemConfirmacao(model);
} catch (NegocioException e) {
LOG.error("Erro ao tentar gravar o aluno. ", e);
mensagemErro(e.getMessage(), model);
}
}
@Override
protected void init(WebDataBinder binder) {
super.init(binder);
binder.registerCustomEditor(String.class, "altura", new CustomNumberEditor(Double.class, Boolean.FALSE));
}
private void adicionaSexoAoModel(Model model) {
model.addAttribute("sexo", CollectionUtils.arrayToList(Sexo.values()));
}
}
我得到了一个例外:
引起:java.lang.IllegalStateException:找到了不明确的映射。无法映射&#39; alunoController&#39;豆方法 public java.lang.String br.com.activepersonal.AlunoController.cadastro(org.springframework.ui.Model) 到{[/ aluno],methods = [GET]}:已经有了&#39; alunoController&#39; bean方法 public java.lang.String br.com.activepersonal.AlunoController.lista(org.springframework.ui.Model)已映射。 在org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.registerHandlerMethod(AbstractHandlerMethodMapping.java:212) 在org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.detectHandlerMethods(AbstractHandlerMethodMapping.java:184) 在org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initHandlerMethods(AbstractHandlerMethodMapping.java:144) 在org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.afterPropertiesSet(AbstractHandlerMethodMapping.java:123) 在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.afterPropertiesSet(RequestMappingHandlerMapping.java:126) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
发生了另一个错误:
java.lang.IllegalStateException:无法初始化上下文,因为已存在根应用程序上下文 - 检查web.xml中是否有多个ContextLoader *定义! 在org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:277) 在org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4715) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5179) 在org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 在org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1403) 在org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1393) at java.util.concurrent.FutureTask.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source) 在java.lang.Thread.run(未知来源)
任何帮助我该如何解决?感谢。