【问题标题】:JSP file does not load in browser - whitelabel error pageJSP 文件未在浏览器中加载 - whitelabel 错误页面
【发布时间】:2018-09-17 15:38:54
【问题描述】:

我正在尝试使用 Spring Boot 编写一个简单的 Web MVC 应用程序。但是,当我在 IntelliJ 中运行应用程序时,我不断收到默认的 whitelabel 错误页面。

我的主要课程如下:

@SpringBootApplication
public class MyApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(MyApplication.class);
    }

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

}

控制器如下所示:

@Controller
public class MainController {

    @RequestMapping("/")
    public String welcome(Model model) {
        model.addAttribute("message", "test");
        return "welcome";
    }

}

这是我的 build.gradle 文件:

buildscript {
    repositories {
        mavenCentral()
     }
     dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.0.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'war'
repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    providedCompile 'org.springframework.boot:spring-boot-starter-web'
    providedCompile 'org.apache.tomcat.embed:tomcat-embed-jasper:9.0.6'
    compile 'javax.servlet:jstl:1.2'

    testCompile 'junit:junit'
}

这是我的 application.properites 文件:

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

我的jsp文件在这里:src/main/webapp/WEB-INF/jsp/welcome.jsp

看起来像这样:

<!DOCTYPE html>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<html lang="en">
<head>
    <link rel="stylesheet" type="text/css" href="webjars/bootstrap/3.3.7/css/bootstrap.min.css" />

</head>

<body>
    <div class="container">

        <div class="starter-template">
           <h1>Message: ${message}</h1>
        </div>

    </div>

    <script type="text/javascript" src="webjars/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</body>
</html>

我想知道我在这里缺少什么?我需要在我的 gradle 文件中添加/更改什么吗?我是否缺少某种配置bean?当我尝试注释@RestController 时,我没有得到一个whitelabel 错误页面,但我没有得到jsp 文件。相反,我只是得到“欢迎”,正如我的 MainController 中的方法返回的那样。据我了解,@RestController 不允许查看,但 @Controller 允许。但是,即使我使用 @Controller,我也无法渲染 jsp。

【问题讨论】:

    标签: java jsp spring-mvc spring-boot


    【解决方案1】:

    我认为缺少 starter-tomcat 可能是个问题。 将此添加到您的 gradle 中。

    providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
    

    【讨论】:

    • 你可能是对的,我需要那个。但是,在添加此内容并从 IntelliJ 本地运行后,我仍然遇到相同的错误。但是,当从命令提示符使用此命令时,我能够让应用程序按预期运行:gradle bootRun。我在 maven 中做了一个类似的项目,结果相同,但是使用 mvn spring-boot:run 命令让它运行。我不知道为什么。可能是我使用 IntelliJ 错误(我对 Eclipse 比较熟悉),或者我需要更新 IntelliJ。
    猜你喜欢
    • 2017-10-06
    • 1970-01-01
    • 1970-01-01
    • 2017-06-04
    • 2013-11-12
    • 1970-01-01
    • 2015-09-22
    • 1970-01-01
    • 2011-09-14
    相关资源
    最近更新 更多