收到警告“[org.springframework.web.servlet.PageNotFound](默认任务 1)GET /ProjectFE/ 没有映射”

getting an warning as" [org.springframework.web.servlet.PageNotFound] (default task-1) No mapping for GET /ProjectFE/"

正如标题所说,我遇到了一个错误 [org.springframework.web.servlet.PageNotFound] (default task-1) No mapping for GET /ProjectFE/ 我该如何解决这个问题?另外,代码没有错误。

这是我的代码:

web.xml 文件-

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<!-- The definition of the Root Spring Container shared by all Servlets 
    and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring-config.xml
    </param-value>
</context-param>

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

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

spring-config.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans
xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    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">

<annotation-driven />

<resources mapping="/resources/**" location="/resources/" />

<beans:bean 
 class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/views/" />
    <beans:property name="suffix" value=".jsp" />
 </beans:bean>

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

 </beans:beans>

homepage.java 文件:

package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HomeController {
        @RequestMapping(value="/", method=RequestMethod.GET)
    public String home()
    {
       return "homepage";
    }

 }

在 运行 之后服务器上的代码是我得到的:

17:59:10,772 INFO  [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads - 1) WFLYDS0004: Found ProjectFE.war in deployment directory. To trigger deployment create a file called ProjectFE.war.dodeploy
17:59:10,822 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-8) WFLYSRV0027: Starting deployment of "ProjectFE.war" (runtime-name: "ProjectFE.war")
17:59:15,226 WARN  [org.jboss.as.ee] (MSC service thread 1-5) WFLYEE0007: Not installing optional component org.springframework.http.server.reactive.ServletServerHttpResponse$ResponseAsyncListener due to an exception (enable DEBUG log level to see the cause)
17:59:15,227 WARN  [org.jboss.as.ee] (MSC service thread 1-5) WFLYEE0007: Not installing optional component org.springframework.http.server.reactive.ServletHttpHandlerAdapter$HandlerResultAsyncListener due to an exception (enable DEBUG log level to see the cause)
17:59:15,230 WARN  [org.jboss.as.ee] (MSC service thread 1-5) WFLYEE0007: Not installing optional component org.springframework.http.server.ServletServerHttpAsyncRequestControl due to an exception (enable DEBUG log level to see the cause)
17:59:15,231 WARN  [org.jboss.as.ee] (MSC service thread 1-5) WFLYEE0007: Not installing optional component org.springframework.web.context.request.async.StandardServletAsyncWebRequest due to an exception (enable DEBUG log level to see the cause)
17:59:15,234 WARN  [org.jboss.as.ee] (MSC service thread 1-5) WFLYEE0007: Not installing optional component org.springframework.http.server.reactive.ServletServerHttpRequest$RequestAsyncListener due to an exception (enable DEBUG log level to see the cause)
17:59:15,251 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-5) WFLYJCA0004: Deploying JDBC-compliant driver class org.h2.Driver (version 1.4)
17:59:15,252 WARN  [org.jboss.weld.deployer] (MSC service thread 1-5) WFLYWELD0013: Deployment ProjectFE.war contains CDI annotations but no bean archive was found (no beans.xml or class with bean defining annotations was present).
17:59:15,274 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-6) WFLYJCA0018: Started Driver service with driver-name = ProjectFE.war_org.h2.Driver_1_4
17:59:15,306 INFO  [io.undertow.servlet] (ServerService Thread Pool -- 74) No Spring WebApplicationInitializer types detected on classpath
17:59:15,308 INFO  [io.undertow.servlet] (ServerService Thread Pool -- 74) Initializing Spring root WebApplicationContext
17:59:15,308 INFO  [org.springframework.web.context.ContextLoader] (ServerService Thread Pool -- 74) Root WebApplicationContext: initialization started
17:59:16,053 INFO  [org.springframework.web.context.ContextLoader] (ServerService Thread Pool -- 74) Root WebApplicationContext initialized in 745 ms
17:59:16,057 INFO  [javax.enterprise.resource.webcontainer.jsf.config] (ServerService Thread Pool -- 74) Initializing Mojarra 2.2.13.SP5  for context '/ProjectFE'
17:59:17,076 INFO  [io.undertow.servlet] (ServerService Thread Pool -- 74) Initializing Spring DispatcherServlet 'appServlet'
17:59:17,076 INFO  [org.springframework.web.servlet.DispatcherServlet] (ServerService Thread Pool -- 74) Initializing Servlet 'appServlet'
17:59:17,279 INFO  [org.springframework.web.servlet.DispatcherServlet] (ServerService Thread Pool -- 74) Completed initialization in 203 ms
17:59:17,280 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 74) WFLYUT0021: Registered web context: '/ProjectFE' for server 'default-server'
17:59:17,353 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) WFLYSRV0010: Deployed "ProjectFE.war" (runtime-name : "ProjectFE.war")
18:00:14,061 WARN  [org.springframework.web.servlet.PageNotFound] (default task-1) No mapping for GET /ProjectFE/

项目结构:

首先把你的web.xml安排成这样

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">


    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            WEB-INF/spring-config.xml
        </param-value>
    </context-param>
   <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

在定义 contex-param 时,您不需要使用 init-param 相同的 xml 文件。 现在在 spring-config.xml

中稍微改变一下
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans
xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    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">

<mvc:annotation-driven/>

<resources mapping="/resources/**" location="/resources/" />

<beans:bean 
 class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/Views/" />
    <beans:property name="suffix" value=".jsp" />
 </beans:bean>

  <context:component-scan    base-package="controller" />        
 </beans:beans>

仔细看<beans:property name="prefix" value="/Views/" />行。您的文件夹名称是 Views,但您定义了 view。这就是为什么您可能会收到错误的原因。

如果有效,请大声疾呼。