【问题标题】:@RequestMapping Couldn't find Jsp page@RequestMapping 找不到 Jsp 页面
【发布时间】:2020-10-03 16:15:28
【问题描述】:

我尝试在 spring-boot 中构建一个小应用程序,它的结构如下所示。我在使用 @RequestMapping 时遇到了一个奇怪的问题,它能够找到除一个之外的所有资源。

这是web.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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">
 
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
 
 </web-app>

下面是applicationContext.xml文件

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

    <!-- Step 3: Add support for component scanning -->
    <context:component-scan base-package="com.example.*" />

    <!-- Step 4: Add support for conversion, formatting and validation support -->
    <mvc:annotation-driven/>

    <!-- Step 5: Define Spring MVC view resolver -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

com.example.controller 包中我有两个文件FrontController.javaFormController.java。 这就是他们的样子。

FrontController.java

package com.example.controller;

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

@Controller
public class FrontController {

    public FrontController() {
        // TODO Auto-generated constructor stub
    }
    
    @RequestMapping("/")
    public String showHomePage() {
        System.out.println("Reached In");
        return "home";
        
    }
    
    @RequestMapping("/spring")
    public String spring() {
        
        return "spring";
        
    }

}

FormController.java

package com.example.controller;

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

@Controller
public class FormController {

    public FormController() {
        // TODO Auto-generated constructor stub
    }

    @RequestMapping("/showform")
    public String showForm() {
        return "showForm";
        
    }
    
    @RequestMapping("/formResp")
    public String formResp() {
        return "formResp";
        
    }
}

现在在服务器中加载我的项目时,我得到的默认页面是home.jsp,它通过以下堆栈跟踪成功显示。

09:21:49.390 [http-nio-8080-exec-3] DEBUG org.springframework.web.servlet.view.InternalResourceView - View name 'home', model {}
09:21:49.392 [http-nio-8080-exec-3] DEBUG org.springframework.web.servlet.view.InternalResourceView - Forwarding to [/WEB-INF/views/home.jsp]
09:21:49.427 [http-nio-8080-exec-3] DEBUG org.springframework.web.servlet.DispatcherServlet - Completed 200 OK

showForm.jsp 页面和FormController.java 中的formResp.jsp 页面也会发生同样的情况,它们会成功显示。下面的堆栈跟踪。

09:21:57.107 [http-nio-8080-exec-4] DEBUG org.springframework.web.servlet.DispatcherServlet - GET "/WebAppDemo/showform", parameters={}
09:21:57.107 [http-nio-8080-exec-4] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped to com.example.controller.FormController#showForm()
09:21:57.108 [http-nio-8080-exec-4] DEBUG org.springframework.web.servlet.view.InternalResourceView - View name 'showForm', model {}
09:21:57.108 [http-nio-8080-exec-4] DEBUG org.springframework.web.servlet.view.InternalResourceView - Forwarding to [/WEB-INF/views/showForm.jsp]
09:21:57.302 [http-nio-8080-exec-4] DEBUG org.springframework.web.servlet.DispatcherServlet - Completed 200 OK
09:22:02.378 [http-nio-8080-exec-5] DEBUG org.springframework.web.servlet.DispatcherServlet - GET "/WebAppDemo/formResp?name=jony", parameters={masked}
09:22:02.379 [http-nio-8080-exec-5] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped to com.example.controller.FormController#formResp()
09:22:02.380 [http-nio-8080-exec-5] DEBUG org.springframework.web.servlet.view.InternalResourceView - View name 'formResp', model {}
09:22:02.380 [http-nio-8080-exec-5] DEBUG org.springframework.web.servlet.view.InternalResourceView - Forwarding to [/WEB-INF/views/formResp.jsp]
09:22:02.621 [http-nio-8080-exec-5] DEBUG org.springframework.web.servlet.DispatcherServlet - Completed 200 OK

但当我尝试在FrontController.java 中显示spring.jsp 页面时,我遇到了404 Error。下面的堆栈跟踪。

09:22:09.179 [http-nio-8080-exec-6] DEBUG org.springframework.web.servlet.DispatcherServlet - GET "/WebAppDemo/spring", parameters={}
09:22:09.187 [http-nio-8080-exec-6] WARN org.springframework.web.servlet.PageNotFound - No mapping for GET /WebAppDemo/spring
09:22:09.187 [http-nio-8080-exec-6] DEBUG org.springframework.web.servlet.DispatcherServlet - Completed 404 NOT_FOUND

我不知道为什么它与其他 JSP 文件一起位于 WEB-INF/views 文件夹中时找不到 spring.jsp 页面。将非常感谢对此的一些帮助。提前致谢。

【问题讨论】:

    标签: java spring spring-boot spring-mvc


    【解决方案1】:

    首先,我发现我解决的项目中有很多配置问题和缺少的类。

    获取新代码:https://github.com/anish-fullstack/WebDemoApp

    更新的项目结构:

    从 web.xml 中删除所有内容。更新 web.xml :

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.1" 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">
     <display-name>WebAppDemo</display-name>
     </web-app>
    

    添加了一个缺少的类,应用程序将从那里开始。

    package com.example;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.ImportResource;
    
    @SpringBootApplication
    @ImportResource("WEB-INF/applicationContext.xml")
    public class WebAppDemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(WebAppDemoApplication.class, args);
        }
    
    }
    

    更新 application.properties。

    spring.main.allow-bean-definition-overriding=true
    

    你需要jasper依赖和jstl来支持jsp解析和jstl支持。更新 pom.xml :

    <?xml version="1.0" encoding="UTF-8"?>
    <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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.3.4.RELEASE</version>
            <relativePath /> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.example</groupId>
        <artifactId>WebAppDemo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>war</packaging>
        <name>WebAppDemo</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>11</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-jasper</artifactId>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    

    运行应用程序日志:

     .   ____          _            __ _ _
     /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
    ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
     \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |_\__, | / / / /
     =========|_|==============|___/=/_/_/_/
     :: Spring Boot ::        (v2.3.4.RELEASE)
    
    2020-10-03 13:27:01.494  INFO 7738 --- [           main] com.example.WebAppDemoApplication        : Starting WebAppDemoApplication on Anishs-MacBook-Pro.local with PID 7738 (/Users/anish/Downloads/WebDemoApp-master/WebAppDemo/target/classes started by anish in /Users/anish/Downloads/WebDemoApp-master/WebAppDemo)
    2020-10-03 13:27:01.495  INFO 7738 --- [           main] com.example.WebAppDemoApplication        : No active profile set, falling back to default profiles: default
    2020-10-03 13:27:01.955  INFO 7738 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
    2020-10-03 13:27:01.959  INFO 7738 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
    2020-10-03 13:27:01.959  INFO 7738 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.38]
    2020-10-03 13:27:02.066  INFO 7738 --- [           main] org.apache.jasper.servlet.TldScanner     : 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.
    2020-10-03 13:27:02.071  INFO 7738 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
    2020-10-03 13:27:02.071  INFO 7738 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 553 ms
    2020-10-03 13:27:02.242  INFO 7738 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
    2020-10-03 13:27:02.301  INFO 7738 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
    2020-10-03 13:27:02.309  INFO 7738 --- [           main] com.example.WebAppDemoApplication        : Started WebAppDemoApplication in 0.992 seconds (JVM running for 1.353)
    2020-10-03 13:27:11.354  INFO 7738 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
    2020-10-03 13:27:11.354  INFO 7738 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
    2020-10-03 13:27:11.358  INFO 7738 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 4 ms
    

    此外,您访问的网址有误。映射网址如下:

    1. http://localhost:8080/showform - 这将打开 showForm.jsp

    1. http://localhost:8080/formResp - 这将打开 formResp.jsp

    1. http://localhost:8080/ - 这将打开 home.jsp

    1. http://localhost:8080/spring - /spring - 这将打开 spring.jsp

    【讨论】:

    • 您好,感谢您的建议,但它不起作用,/** 没有显示您提到的上述任何网址,所有网址均以404 not found 的形式出现
    • github.com/sid111nov/WebDemoApp.git 是项目网址,感谢您的帮助。
    • @Sid111Math 更新了我的答案。请看一看。
    • 我下载了您的项目并运行了它,但我仍然遇到 404 现在发现所有页面的错误,我认为这与我的本地环境配置中的某些内容有关,但非常感谢您无论如何帮助。
    • 肯定是,正如您的屏幕截图中清楚地显示的那样,这就是为什么我觉得它与我的本地环境相关的东西可能会导致问题。您的代码没有问题。
    猜你喜欢
    • 2020-09-21
    • 2013-04-06
    • 2020-03-08
    • 2021-12-07
    • 1970-01-01
    • 2012-05-23
    • 1970-01-01
    • 2019-12-29
    • 2012-09-20
    相关资源
    最近更新 更多