Spring Boot JSP 404:找不到页面

Posted

技术标签:

【中文标题】Spring Boot JSP 404:找不到页面【英文标题】:Spring boot JSP 404: Page not found 【发布时间】:2020-03-08 06:41:07 【问题描述】:

我正在尝试运行一个简单的 Spring Boot 应用程序。但是,当我运行它时,我收到以下错误:

“白标错误页面 此应用程序没有明确的 /error 映射,因此您将其视为后备。”

2019 年 11 月 12 日星期二 09:31:36 AST 出现意外错误(类型=未找到,状态=404)。 /WEB-INF/view/index.jsp

这是我的 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.2.1.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.example</groupId>
        <artifactId>jspDemo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>jspDemo</name>
        <description>Demo project for Spring Boot</description>

        <properties>
            <java.version>1.8</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-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>


              <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>

            </dependency>
            <!-- JSTL -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
            </dependency>
            <!-- To compile JSP files -->
            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-jasper</artifactId>

            </dependency>


        </dependencies>

        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>

    </project>

IndexController.java


    package com.pack.springMVC;

    import java.util.Map;

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

    @Controller
    public class IndexController 

        @RequestMapping("/")
        public String home(Map<String, Object> model) 
            model.put("message", "HowToDoInJava Reader !!");
            return "index";
        

        @RequestMapping("/next")
        public String next(Map<String, Object> model) 
            model.put("message", "You are in new page !!");
            return "next";
        

    

MvcConfiguration.java


    package com.pack.springMVC;



    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    import org.springframework.web.servlet.view.InternalResourceViewResolver;
    import org.springframework.web.servlet.view.JstlView;


    @Configuration
    @EnableWebMvc
    @ComponentScan
    public class MvcConfiguration implements WebMvcConfigurer
    
        @Override
        public void configureViewResolvers(ViewResolverRegistry registry) 
            InternalResourceViewResolver resolver = new InternalResourceViewResolver();
            resolver.setPrefix("/WEB-INF/view/");
            resolver.setSuffix(".jsp");
            resolver.setViewClass(JstlView.class);
            registry.viewResolver(resolver);
        
    

JspDemoApplication.java



    package com.pack.springMVC;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

    @SpringBootApplication
    public class JspDemoApplication extends SpringBootServletInitializer  

        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) 
            return application.sources(JspDemoApplication.class);
        


        public static void main(String[] args) 
            SpringApplication.run(JspDemoApplication.class, args);
        

    

index.jsp



    <!DOCTYPE html>
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
    <html lang="en">
    <body>
        <div>
            <div>
                <h1>Spring Boot JSP Example</h1>
                <h2>Hello $message</h2>

                Click on this <strong><a href="next">link</a></strong> to visit another page.
            </div>
        </div>
    </body>
    </html>

下一个.jsp




    <!DOCTYPE html>
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
    <html lang="en">
    <body>
        <div>
            <div>
                <h1>Another page</h1>
                <h2>Hello $message</h2>

                Click on this <strong><a href="/">link</a></strong> to visit previous page.
            </div>
        </div>
    </body>
    </html>


application.properties

spring.mvc.view.prefix=/WEB-INF/view/ spring.mvc.view.suffix=.jsp

【问题讨论】:

index.jsp的路径是什么 src/main/resource/webapp/WEB-INF/view/index.jsp 【参考方案1】:

试试这个

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@Configuration
@EnableWebMvc
@ComponentScan
public class MvcConfiguration extends WebMvcConfigurerAdapter

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) 
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/view/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        registry.viewResolver(resolver);
    

【讨论】:

以上是关于Spring Boot JSP 404:找不到页面的主要内容,如果未能解决你的问题,请参考以下文章

spring-boot 基本 JSP 404 未找到

找不到带有spring boot的html页面

spring boot找不到jsp视图

Spring boot 找不到我的模板视图出现意外错误(type=Not Found,status=404)

状态为404的Springboot Whitelabel错误页面:无法解析JSP的路径

AWS Elastic Beanstalk Spring Boot fat JAR:找不到jsp