为此,我还在stackoverflow上阅读了一些较旧的帖子,但我找不到我需要的解决方案。
我正在创建一个简单的Spring MVC HelloWeb应用程序。
为此,我使用以下文件
1)web.xml
用于配置DispatcherServlet
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Web Application</display-name>
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
2)然后我写简单HelloController
如下:
package com.tutorialspoint;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;
@Controller
public class HelloController{
@RequestMapping(value="hello" ,method = RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC Framework!");
return "hello";
}
}
3)之后我按如下方式配置HelloWeb-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.tutorialspoint"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
<property name="order" value = "1" ></property>
</bean>
</beans>
4)hello.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>
5)package-nfo.java
package com.tutorialspoint;
我将战争导出到tomcat 7 webapps文件夹并重新启动了tomcat服务。
我在正在运行的tomcat控制台中检查了HelloWeb应用程序。
我点击了网址http://<URL>:8080/HelloWeb/hello.
我收到以下错误
javax.servlet.ServletException:servlet HelloWeb的Servlet.init() 扔了例外 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:379) org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:242) org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:259) org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:237) org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:281) java.util.concurrent.ThreadPoolExecutor.runWorker(未知来源) java.util.concurrent.ThreadPoolExecutor $ Worker.run(未知来源) java.lang.Thread.run(未知来源) 根本原因 org.springframework.beans.factory.BeanCreationException:使用
创建bean时出错 名称&#39; org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping&#39 ;: bean的初始化失败;嵌套异常是 java.lang.NoSuchMethodError:
org.springframework.context.ApplicationContext.findAnnotationOnBean(Ljava /郎/字符串; Ljava /郎/类;)Ljava /郎/注解/注释; org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:480) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory $ 1.run(AbstractAutowireCapableBeanFactory.java:409) java.security.AccessController.doPrivileged(本机方法) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean (AbstractAutowireCapableBeanFactory.java:255) org.springframework.web.servlet.DispatcherServlet.createDefaultStrategy(DispatcherServlet.java:672) org.springframework.web.servlet.DispatcherServlet.getDefaultStrategies(DispatcherServlet.java:640) org.springframework.web.servlet.DispatcherServlet.initHandlerMappings(DispatcherServlet.java:435) org.springframework.web.servlet.DispatcherServlet.initStrategies(DispatcherServlet.java:334) org.springframework.web.servlet.DispatcherServlet.onRefresh(DispatcherServlet.java:323) org.springframework.web.servlet.FrameworkServlet.onApplicationEvent(FrameworkServlet.java:498) org.springframework.web.servlet.FrameworkServlet.onApplicationEvent(FrameworkServlet.java:1) org.springframework.context.event.SourceFilteringListener.onApplicationEventInternal(SourceFilteringListener.java:81) org.springframework.context.event.SourceFilteringListener.onApplicationEvent(SourceFilteringListener.java:66) org.springframework.context.event.SimpleApplicationEventMulticaster $ 1.run(SimpleApplicationEventMulticaster.java:78) org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:49) org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:76) org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:274) org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:736) org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:383) org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:427) org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:341) org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:307) org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127) javax.servlet.GenericServlet.init(GenericServlet.java:158) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:379) org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:242) org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:259) org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:237) org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:281) java.util.concurrent.ThreadPoolExecutor.runWorker(未知来源) java.util.concurrent.ThreadPoolExecutor $ Worker.run(未知来源) java.lang.Thread.run(未知来源)根本原因java.lang.NoSuchMethodError: org.springframework.context.ApplicationContext.findAnnotationOnBean(Ljava /郎/字符串; Ljava /郎/类;)Ljava /郎/注解/注释; org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping.determineUrlsForHandler(DefaultAnnotationHandlerMapping.java:108) org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping.detectHandlers(AbstractDetectingUrlHandlerMapping.java:80) org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping.initApplicationContext(AbstractDetectingUrlHandlerMapping.java:58) org.springframework.context.support.ApplicationObjectSupport.initApplicationContext(ApplicationObjectSupport.java:119) org.springframework.web.context.support.WebApplicationObjectSupport.initApplicationContext(WebApplicationObjectSupport.java:69) org.springframework.context.support.ApplicationObjectSupport.setApplicationContext(ApplicationObjectSupport.java:73) org.springframework.context.support.ApplicationContextAwareProcessor.postProcessBeforeInitialization(ApplicationContextAwareProcessor.java:70) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:350) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1331) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory $ 1.run(AbstractAutowireCapableBeanFactory.java:409) java.security.AccessController.doPrivileged(本机方法) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:255) org.springframework.web.servlet.DispatcherServlet.createDefaultStrategy(DispatcherServlet.java:672) org.springframework.web.servlet.DispatcherServlet.getDefaultStrategies(DispatcherServlet.java:640) org.springframework.web.servlet.DispatcherServlet.initHandlerMappings(DispatcherServlet.java:435) org.springframework.web.servlet.DispatcherServlet.initStrategies(DispatcherServlet.java:334) org.springframework.web.servlet.DispatcherServlet.onRefresh(DispatcherServlet.java:323) org.springframework.web.servlet.FrameworkServlet.onApplicationEvent(FrameworkServlet.java:498) org.springframework.web.servlet.FrameworkServlet.onApplicationEvent(FrameworkServlet.java:1) org.springframework.context.event.SourceFilteringListener.onApplicationEventInternal(SourceFilteringListener.java:81) org.springframework.context.event.SourceFilteringListener.onApplicationEvent(SourceFilteringListener.java:66) org.springframework.context.event.SimpleApplicationEventMulticaster $ 1.run(SimpleApplicationEventMulticaster.java:78) org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:49) org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:76) org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:274) org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:736) org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:383) org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:427) org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:341) org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:307) org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127) javax.servlet.GenericServlet.init(GenericServlet.java:158) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:379) org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:242) org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:259) org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:237) org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:281) java.util.concurrent.ThreadPoolExecutor.runWorker(未知来源) java.util.concurrent.ThreadPoolExecutor $ Worker.run(未知来源)
答案 0 :(得分:1)
这是因为Spring jar版本不匹配。我从Spring 2将应用程序升级到Spring 3。我们需要删除spring-2.5.6.jar,因为Spring 3不需要它。它解决了我的问题。
答案 1 :(得分:0)
我认为你在RequestMapping中缺少斜杠。你可以尝试下一次修复吗?
@RequestMapping(value="/hello" ,method = RequestMethod.GET)
答案 2 :(得分:0)
在'HelloWeb-servlet.xml'中,通过添加''启用注释配置。您已在控制器类中使用了注释,但Spring容器如何识别Controller。