Spring maven项目,构建成功,但收到404错误

时间:2016-06-03 05:23:34

标签: spring maven spring-mvc maven-2 maven-3

这仍然是我的第一个春天mvs.maven构建成功后,当我在服务器上运行时,第一页打开就像这样 [打开页面的屏幕截图] [1]

当我点击开始页面中的链接时,就会发生这种情况

HTTP Status 404 - 


type Status report

message 

description The requested resource is not available.

--------------------------------------------------------------------------------

Apache Tomcat/8.0.35

[项目目录结构的截屏] [3]

CrunchifySpringMVCTutorial / 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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>CrunchifySpringMVCTutorial</groupId>
    <artifactId>CrunchifySpringMVCTutorial</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.3</version>
        </dependency>
    </dependencies>
</project>

crunchify-servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 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.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.crunchify.controller" />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>CrunchifySpringMVCTutorial</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

    <servlet>
        <servlet-name>crunchify</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>crunchify</servlet-name>
        <url-pattern>/welcome.jsp</url-pattern>
        <url-pattern>/welcome.html</url-pattern>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

</web-app>

CrunchifyHelloWorld.java

package com.crunchify.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/*
 * author: Crunchify.com
 * 
 */

@Controller
public class CrunchifyHelloWorld {

    @RequestMapping("/welcome")
    public ModelAndView helloWorld() {

        String message = "<br><div style='text-align:center;'>"
                + "<h3>********** Hello World, Spring MVC Tutorial</h3>This message is coming from CrunchifyHelloWorld.java **********</div><br><br>";
        return new ModelAndView("welcome", "message", message);
    }
}

的index.jsp

<html>
<head>
<title>Spring MVC Tutorial Series by Crunchify.com</title>
<style type="text/css">
body {
    background-image: url('http://crunchify.com/bg.png');
}
</style>
</head>
<body>
    <br>
    <div style="text-align:center">
        <h2>
            Hey You..!! This is your 1st Spring MCV Tutorial..<br> <br>
        </h2>
        <h3>
            <a href="welcome.jsp">Click here to See Welcome Message... </a>(to
            check Spring MVC Controller... @RequestMapping("/welcome"))
        </h3>
    </div>
</body>
</html>

的welcome.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Spring MVC Tutorial by Crunchify - Hello World Spring MVC
    Example</title>
<style type="text/css">
body {
    background-image: url('http://crunchify.com/bg.png');
}
</style>
</head>
<body>${message}

    <br>
    <br>
    <div style="font-family: verdana; padding: 10px; border-radius: 10px; font-size: 12px; text-align:center;">

        Spring MCV Tutorial by <a href="http://crunchify.com">Crunchify</a>.
        Click <a
            href="http://crunchify.com/category/java-web-development-tutorial/"
            target="_blank">here</a> for all Java and <a
            href='http://crunchify.com/category/spring-mvc/' target='_blank'>here</a>
        for all Spring MVC, Web Development examples.<br>
    </div>
</body>
</html>

maven Build:

[INFO] Scanning for projects...
[INFO] 
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building CrunchifySpringMVCTutorial 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ CrunchifySpringMVCTutorial ---
[INFO] Deleting C:\Users\shamee\Documents\SpringWorkSpace\CrunchifySpringMVCTutorial\target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ CrunchifySpringMVCTutorial ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\shamee\Documents\SpringWorkSpace\CrunchifySpringMVCTutorial\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.3:compile (default-compile) @ CrunchifySpringMVCTutorial ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 1 source file to C:\Users\shamee\Documents\SpringWorkSpace\CrunchifySpringMVCTutorial\target\classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ CrunchifySpringMVCTutorial ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\shamee\Documents\SpringWorkSpace\CrunchifySpringMVCTutorial\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.3:testCompile (default-testCompile) @ CrunchifySpringMVCTutorial ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ CrunchifySpringMVCTutorial ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-war-plugin:2.6:war (default-war) @ CrunchifySpringMVCTutorial ---
[INFO] Packaging webapp
[INFO] Assembling webapp [CrunchifySpringMVCTutorial] in [C:\Users\shamee\Documents\SpringWorkSpace\CrunchifySpringMVCTutorial\target\CrunchifySpringMVCTutorial-0.0.1-SNAPSHOT]
[INFO] Processing war project
[INFO] Copying webapp resources [C:\Users\shamee\Documents\SpringWorkSpace\CrunchifySpringMVCTutorial\WebContent]
[INFO] Webapp assembled in [117 msecs]
[INFO] Building war: C:\Users\shamee\Documents\SpringWorkSpace\CrunchifySpringMVCTutorial\target\CrunchifySpringMVCTutorial-0.0.1-SNAPSHOT.war
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ CrunchifySpringMVCTutorial ---
[INFO] Installing C:\Users\shamee\Documents\SpringWorkSpace\CrunchifySpringMVCTutorial\target\CrunchifySpringMVCTutorial-0.0.1-SNAPSHOT.war to C:\Users\shamee\.m2\repository\CrunchifySpringMVCTutorial\CrunchifySpringMVCTutorial\0.0.1-SNAPSHOT\CrunchifySpringMVCTutorial-0.0.1-SNAPSHOT.war
[INFO] Installing C:\Users\shamee\Documents\SpringWorkSpace\CrunchifySpringMVCTutorial\pom.xml to C:\Users\shamee\.m2\repository\CrunchifySpringMVCTutorial\CrunchifySpringMVCTutorial\0.0.1-SNAPSHOT\CrunchifySpringMVCTutorial-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.328 s
[INFO] Finished at: 2016-06-02T16:32:58+05:30
[INFO] Final Memory: 18M/178M
[INFO] ------------------------------------------------------------------------

在服务器(Tomcat)上运行

Jun 03, 2016 1:28:07 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:CrunchifySpringMVCTutorial' did not find a matching property.
Jun 03, 2016 1:28:07 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version:        Apache Tomcat/8.0.35
Jun 03, 2016 1:28:07 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          May 11 2016 21:57:08 UTC
Jun 03, 2016 1:28:07 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number:         8.0.35.0
Jun 03, 2016 1:28:07 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Windows 8.1
Jun 03, 2016 1:28:07 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            6.3
Jun 03, 2016 1:28:07 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          amd64
Jun 03, 2016 1:28:07 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             C:\Program Files\Java\jdk1.8.0_45\jre
Jun 03, 2016 1:28:07 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.8.0_45-b15
Jun 03, 2016 1:28:07 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
Jun 03, 2016 1:28:07 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         C:\apache-tomcat-8.0.35
Jun 03, 2016 1:28:07 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         C:\apache-tomcat-8.0.35
Jun 03, 2016 1:28:07 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=C:\apache-tomcat-8.0.35
Jun 03, 2016 1:28:07 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=C:\apache-tomcat-8.0.35
Jun 03, 2016 1:28:07 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=C:\apache-tomcat-8.0.35\wtpwebapps
Jun 03, 2016 1:28:07 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=C:\apache-tomcat-8.0.35\endorsed
Jun 03, 2016 1:28:07 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
Jun 03, 2016 1:28:07 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_45\jre\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jre1.8.0_91/bin/server;C:/Program Files/Java/jre1.8.0_91/bin;C:/Program Files/Java/jre1.8.0_91/lib/amd64;C:\oraclexe\app\oracle\product\11.2.0\server\bin;;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\Program Files\Java\jdk1.8.0_45\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\nodejs\;C:\Program Files\PostgreSQL\9.3\bin;C:\Users\shamee\AppData\Roaming\npm;C:\Users\shamee\Desktop\sts-bundle\sts-3.6.0.RELEASE;;.
Jun 03, 2016 1:28:07 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Jun 03, 2016 1:28:07 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Jun 03, 2016 1:28:07 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
Jun 03, 2016 1:28:07 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Jun 03, 2016 1:28:07 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 481 ms
Jun 03, 2016 1:28:07 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Jun 03, 2016 1:28:07 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.0.35
Jun 03, 2016 1:28:07 PM org.apache.catalina.core.ApplicationContext log
INFO: Marking servlet crunchify as unavailable
Jun 03, 2016 1:28:07 PM org.apache.catalina.core.StandardContext loadOnStartup
SEVERE: Servlet [crunchify] in web application [/CrunchifySpringMVCTutorial] threw load() exception
java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1305)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1139)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:518)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:499)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:118)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1102)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1038)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4998)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5306)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1407)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1397)
    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)

Jun 03, 2016 1:28:07 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\apache-tomcat-8.0.35\webapps\docs
Jun 03, 2016 1:28:07 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory C:\apache-tomcat-8.0.35\webapps\docs has finished in 24 ms
Jun 03, 2016 1:28:07 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\apache-tomcat-8.0.35\webapps\examples
Jun 03, 2016 1:28:07 PM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextInitialized()
Jun 03, 2016 1:28:07 PM org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextInitialized()
Jun 03, 2016 1:28:07 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory C:\apache-tomcat-8.0.35\webapps\examples has finished in 309 ms
Jun 03, 2016 1:28:07 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\apache-tomcat-8.0.35\webapps\host-manager
Jun 03, 2016 1:28:07 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory C:\apache-tomcat-8.0.35\webapps\host-manager has finished in 20 ms
Jun 03, 2016 1:28:07 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\apache-tomcat-8.0.35\webapps\manager
Jun 03, 2016 1:28:07 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory C:\apache-tomcat-8.0.35\webapps\manager has finished in 24 ms
Jun 03, 2016 1:28:07 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\apache-tomcat-8.0.35\webapps\ROOT
Jun 03, 2016 1:28:07 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory C:\apache-tomcat-8.0.35\webapps\ROOT has finished in 16 ms
Jun 03, 2016 1:28:07 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Jun 03, 2016 1:28:07 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Jun 03, 2016 1:28:07 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 656 ms

3 个答案:

答案 0 :(得分:0)

您是否在index.jsp中尝试了<a href="jsp/welcome.jsp">?你的href是相对的,所以基于你的目录结构,我希望welcome.jsp与index.jsp处于同一级别

|-- WEB-INF
    |-- index.jsp
    |-- welcome.jsp

您的结构似乎

|-- WEB-INF
    |-- jsp
        |-- welcome.jsp
    |-- index.jsp

答案 1 :(得分:0)

java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1305)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1139)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:518)

tomcat找不到org.springframework.web.servlet.DispatcherServlet。

我认为你的包中没有spring-webmvc。

你需要确保包构建成功,并检查{tomcat webapps} / yourproject / WEB-INF / lib是否有spring-webmvc

答案 2 :(得分:-1)

您必须通过在web.xml中添加以下行来启动Spring容器

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>



   <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:crunchify-servlet.xml</param-value>
    </context-param>

确保crunchify-servlet.xml位于类路径的根目录。