我的Spring应用程序无法正常工作。出现404错误,而不是jsp文件

时间:2018-09-23 11:05:02

标签: java spring-mvc

下面是我的PageController.java类

package com.fayis.shopping.controller;


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

@Controller
public class PageController {

@RequestMapping(value = {"/","/home","/index"})
public ModelAndView index()
{
    ModelAndView mv=new ModelAndView("page");
    mv.addObject("greeting", "Hi");
    return mv;
}

}

下面是我的Page.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>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    test
</body>
</html>

下面是我的调度程序servlet dispatcher-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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
 http://www.springframework.org/schema/mvc htt p://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
>
<mvc:default-servlet-handler/>
<context:component-scan base-package="com.fayis.shopping.controller" />
<bean

class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/view/" />
    <property name="suffix" value=".jsp" />
 </bean>

 </beans>

下面是我的web.xml文件-web.xml

  <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
     http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     version="3.1">
  <display-name>Archetype Created Web Application</display-name>

 <!--  Front Controller -->
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet- 
  class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
 </servlet>
  <servlet-mapping>
   <servlet-name>dispatcher</servlet-name>
   <url-pattern>/*</url-pattern>
</servlet-mapping>

</web-app>

下面是我的pom.xml文件-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.fayis</groupId>
  <artifactId>shopping</artifactId>
  <packaging>war</packaging>
   <version>0.0.1-SNAPSHOT</version>
  <name>shopping Maven Webapp</name>
   <url>http://maven.apache.org</url>
  <dependencies>

    <dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-web-api</artifactId>
    <version>7.0</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
     <groupId>junit</groupId>
     <artifactId>junit</artifactId>
     <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
     <!-- Spring Dependancy -->

   <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>4.3.6.RELEASE</version>
   </dependency>
   </dependencies>


  <build>
   <plugins>
    <plugin>
         <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
     </plugin>
  </plugins>
  <finalName>shopping</finalName>
 </build>
</project>

下面是我的控制台日志

Sep 23, 2018 6:20:57 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:shopping' did not find a matching property.
Sep 23, 2018 6:20:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version:        Apache Tomcat/8.0.53
Sep 23, 2018 6:20:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          Jun 29 2018 14:42:45 UTC
Sep 23, 2018 6:20:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number:         8.0.53.0
Sep 23, 2018 6:20:57 PM org.apache.catalina.startup.VersionLoggerListener 
log
INFO: OS Name:               Windows 8.1
Sep 23, 2018 6:20:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            6.3
Sep 23, 2018 6:20:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          amd64
Sep 23, 2018 6:20:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             C:\Program Files\Java\jre1.8.0_111
Sep 23, 2018 6:20:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.8.0_111-b14
Sep 23, 2018 6:20:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
Sep 23, 2018 6:20:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         C:\Users\Mohammed Fayis\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Sep 23, 2018 6:20:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         C:\Program Files\Apache Software Foundation\Tomcat 8.0
Sep 23, 2018 6:20:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=C:\Users\Mohammed Fayis\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Sep 23, 2018 6:20:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=C:\Program Files\Apache Software Foundation\Tomcat 8.0
Sep 23, 2018 6:20:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=C:\Users\Mohammed Fayis\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
Sep 23, 2018 6:20:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=C:\Program Files\Apache Software Foundation\Tomcat 8.0\endorsed
Sep 23, 2018 6:20:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
Sep 23, 2018 6:20:57 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\jre1.8.0_111\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre1.8.0_111/bin/server;C:/Program Files/Java/jre1.8.0_111/bin;C:/Program Files/Java/jre1.8.0_111/lib/amd64;C:\ProgramData\Oracle\Java\javapath;E:\app\oracl\product\11.2.0\dbhome_1\bin;c:\Program Files (x86)\Intel\iCLS Client\;c:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\nodejs\;C:\Program Files\Java\jdk1.8.0_111\bin;C:\Users\Mohammed Fayis\AppData\Roaming\npm;C:\Program Files\Microsoft VS Code\bin;E:\Softwares\eclipse;;.
Sep 23, 2018 6:20:58 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Sep 23, 2018 6:20:58 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Sep 23, 2018 6:20:58 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
Sep 23, 2018 6:20:58 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Sep 23, 2018 6:20:58 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1766 ms
Sep 23, 2018 6:20:58 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Sep 23, 2018 6:20:58 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.0.53
Sep 23, 2018 6:21:01 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Sep 23, 2018 6:21:01 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Sep 23, 2018 6:21:01 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'dispatcher'
Sep 23, 2018 6:21:01 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'dispatcher': initialization started
Sep 23, 2018 6:21:01 PM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Sun Sep 23 18:21:01 IST 2018]; root of context hierarchy
Sep 23, 2018 6:21:01 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/dispatcher-servlet.xml]
Sep 23, 2018 6:21:02 PM org.springframework.web.servlet.handler.SimpleUrlHandlerMapping registerHandler
INFO: Mapped URL path [/**] onto handler 'org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler#0'
Sep 23, 2018 6:21:03 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'dispatcher': initialization completed in 1726 ms
Sep 23, 2018 6:21:03 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Sep 23, 2018 6:21:03 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Sep 23, 2018 6:21:03 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 4385 ms

aaaaa a a a a aaaaa a a a a a a aaaaa a aaaaa

这些是我的代码,请帮助我解决这个问题。

下面是github中的Project链接。请帮我解决这个问题

https://github.com/mohammedfayisp/shoppingcart.git

2 个答案:

答案 0 :(得分:0)

您尚未指定servlet容器,但是例如在tomcat中,要进入您的应用,您必须在网址中包含war文件名,例如:localhost:8080/your_app-1.0-SNAPSHOT

第二个web.xml中有<url-pattern>/</url-pattern>,它仅映射/,而不会映射/index/home。 要匹配此URL,您的模式应类似于<url-pattern>/*</url-pattern>

答案 1 :(得分:0)

现在有效。就像奇迹一样。当我更新dispatcher-servlet spring bean定义时,它起作用了。后来我把它改回来,仍然可以工作。