我是Spring Mvc的新手。我正在尝试创建一个基于Maven的Spring Mvc演示..
基本"你好世界"演示工作正常。它打印了" Hello World"当我点击" http://localhost:8080/SampleRestful/"
但是,当我将spring-webmvc
依赖项添加到我的pom.xml并在服务器上运行时,它会在点击" http://localhost:8080/SampleRestful/&时抛出错误404 #34;
这是pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.promact.maven</groupId>
<artifactId>SampleRestful</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SampleRestful Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.0.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>SampleRestful</finalName>
</build>
</project>
以下是错误日志:
Mar 24, 2017 12:19:48 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version: Apache Tomcat/8.5.8
Mar 24, 2017 12:19:48 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built: Nov 3 2016 21:14:13 UTC
Mar 24, 2017 12:19:48 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number: 8.5.8.0
Mar 24, 2017 12:19:48 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name: Windows 7
Mar 24, 2017 12:19:48 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version: 6.1
Mar 24, 2017 12:19:48 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture: amd64
Mar 24, 2017 12:19:48 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home: C:\Program Files\Java\jdk1.8.0_92\jre
Mar 24, 2017 12:19:48 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version: 1.8.0_92-b14
Mar 24, 2017 12:19:48 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor: Oracle Corporation
Mar 24, 2017 12:19:48 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE: D:\SpringWorkspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Mar 24, 2017 12:19:48 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME: C:\Program Files\Apache Software Foundation\Tomcat 8.5
Mar 24, 2017 12:19:48 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=D:\SpringWorkspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Mar 24, 2017 12:19:48 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=C:\Program Files\Apache Software Foundation\Tomcat 8.5
Mar 24, 2017 12:19:48 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=D:\SpringWorkspace
Mar 24, 2017 12:19:48 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=C:\Program Files\Apache Software Foundation\Tomcat 8.5\endorsed
Mar 24, 2017 12:19:48 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
Mar 24, 2017 12:19:48 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jdk1.8.0_92\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre1.8.0_92/bin/server;C:/Program Files/Java/jre1.8.0_92/bin;C:/Program Files/Java/jre1.8.0_92/lib/amd64;C:\ProgramData\Oracle\Java\javapath;C:\webserver\ImageMagick;C:\Program Files (x86)\ImageMagick-6.8.1-Q16;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Python27;D:\Nishant\Android\android-sdk\platform-tools;D:\Nishant\Android\android-sdk\tools;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files (x86)\Ant 1.8\bin;C:\Program Files (x86)\Ant 1.8\lib;C:\Program Files\Java\jdk1.7.0_06\bin;C:\Program Files\Java\jdk1.6.0_30\bin;C:\Program Files\OpenSSL\bin;C:\webserver\MySQL\bin;C:\Ruby193\bin;C:\Users\nishant\Downloads\libs\depot_tools\;D:\Nishant\Android\android-ndk-r8e\;C:\Python27\;C:\Python27\Scripts\;C:\Program Files\gradle-2.1-all\gradle-2.1\bin\;C:\Program Files\TortoiseGit\bin;C:\Users\m\AppData\Local\GitHub\PortableGit_054f2e797ebafd44a30203088cd3d58663c627ef\bin;D:\setups\apache-ant-1.9.4\bin;C:\Program Files\nodejs;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Java\jdk1.8.0_92\bin\;C:\Program Files\Java\jdk1.8.0_92\;C:\Program Files\nodejs\;D:\setups\java\apache-maven-3.3.9\bin;C:\Users\m\AppData\Roaming\npm;C:\Users\m\AppData\Local\atom\bin;D:\setups\java\spring-tool-suite-3.8.2.RELEASE-e4.6.1-win32-x86_64\sts-bundle\sts-3.8.2.RELEASE;;.
Mar 24, 2017 12:19:49 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Mar 24, 2017 12:19:49 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Mar 24, 2017 12:19:49 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
Mar 24, 2017 12:19:49 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Mar 24, 2017 12:19:49 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 835 ms
Mar 24, 2017 12:19:49 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Mar 24, 2017 12:19:49 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.5.8
Mar 24, 2017 12:19:49 PM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [110] milliseconds.
Mar 24, 2017 12:19:49 PM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor D:\SpringWorkspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\conf\Catalina\localhost\SampleRestful.xml
Mar 24, 2017 12:19:49 PM org.apache.catalina.startup.SetContextPropertiesRule begin
WARNING: [SetContextPropertiesRule]{Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:SampleRestful' did not find a matching property.
Mar 24, 2017 12:19:49 PM org.apache.catalina.core.ContainerBase addChildInternal
SEVERE: ContainerBase.addChild: start:
org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/SampleRestful]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:752)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:728)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:596)
at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1805)
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:745)
Caused by: org.apache.catalina.LifecycleException: Failed to initialize component [org.apache.catalina.webresources.StandardRoot@3f797939]
at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:112)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:140)
at org.apache.catalina.core.StandardContext.resourcesStart(StandardContext.java:4842)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:4974)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 10 more
Caused by: org.apache.catalina.LifecycleException: Failed to initialize component [org.apache.catalina.webresources.DirResourceSet@572b1df8]
at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:112)
at org.apache.catalina.webresources.StandardRoot.initInternal(StandardRoot.java:675)
at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:107)
... 14 more
Caused by: java.lang.IllegalArgumentException: The directory specified by base and internal path [D:\SpringWorkspace\SampleRestful\target\m2e-wtp\web-resources]\[] does not exist.
at org.apache.catalina.webresources.DirResourceSet.checkType(DirResourceSet.java:251)
at org.apache.catalina.webresources.AbstractFileResourceSet.initInternal(AbstractFileResourceSet.java:145)
at org.apache.catalina.webresources.DirResourceSet.initInternal(DirResourceSet.java:259)
at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:107)
... 16 more
Mar 24, 2017 12:19:49 PM org.apache.catalina.startup.HostConfig deployDescriptor
SEVERE: Error deploying configuration descriptor D:\SpringWorkspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\conf\Catalina\localhost\SampleRestful.xml
java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/SampleRestful]]
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:756)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:728)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:596)
at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1805)
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:745)
Mar 24, 2017 12:19:49 PM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deployment of configuration descriptor D:\SpringWorkspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\conf\Catalina\localhost\SampleRestful.xml has finished in 15 ms
Mar 24, 2017 12:19:49 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler [http-nio-8080]
Mar 24, 2017 12:19:49 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler [ajp-nio-8009]
Mar 24, 2017 12:19:49 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 632 ms
我无法找到问题背后的原因。请帮忙..!! :(
答案 0 :(得分:1)
Spring MVC不能单独使用,它需要其他依赖,如:Spring-core,Spring-context,Spring-beans和Spring-web。
如果您想开始一个新的SpringMVC项目,我建议您按照以下步骤进行操作(这是我在开始一个新的SpringMVC项目时的工作方式):
1)将Spring Tool Suite添加到Eclipse(转到“帮助”(菜单) - &gt; Eclipse Marketplace-&gt;(在查找serch栏中)输入“STS”并安装它。
2)然后在Eclipse中,转到File-&gt; Spring Legacy Project-&gt; Spring MVC Project(输入项目名称),单击next,输入包名称并完成。
你将有一个预配置的Spring MVC项目,它可以正常工作。我建议你改变Java和其他dependecies的版本。
我希望这会对你有所帮助。
答案 1 :(得分:0)
基本上这意味着,Tomcat不知道如何处理来自上下文的source属性。 首先,你不能只用一个jar来启动mvc maven项目。 添加其他DI,如spring core,以及其他运行它:What are the jars needed for only Spring-MVC? 您可以更改tomcat服务器配置。打开服务器视图,双击服务器以打开服务器配置。然后单击以激活&#34;将模块内容发布到单独的XML文件&#34;。最后,重新启动服务器,问题将得到解决。 你可以做的第二件事是尝试使用其他版本的tomcat。更新项目,清理maven,构建它并在此之后运行。 如果这对你没有帮助的话。 这是一个很好的例子,说明如何使用Maven的tomcat-maven-plugin在Tomcat服务器上部署项目:http://www.mkyong.com/maven/how-to-deploy-maven-based-war-file-to-tomcat/