【问题标题】:Whitelabel Error Page - application has no explicit mapping for /errorWhitelabel 错误页面 - 应用程序没有 /error 的显式映射
【发布时间】:2019-05-23 11:30:18
【问题描述】:

我已经配置了一个 spring boot 应用程序,但是在启动应用程序时,我得到如下映射错误

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon Dec 24 12:46:27 IST 2018
There was an unexpected error (type=Not Found, status=404).
No message available

。我浏览了下面的链接并将我的控制器更新为包含主类的根包的一部分。 This application has no explicit mapping for /error

我已经为我的应用程序使用了下面提到的配置/代码:

build.gradle

buildscript {
    ext {
        springBootVersion = '2.1.1.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.ticket'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {
    implementation('org.springframework.boot:spring-boot-starter-data-jpa')
    implementation('javax.servlet:jstl')
    implementation('org.springframework.boot:spring-boot-starter-web')
    runtimeOnly('com.h2database:h2')
    testImplementation('org.springframework.boot:spring-boot-starter-test')
}

TicketController.java

package com.ticket.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
public class TicketController {

    static Logger log = LogManager.getLogger();

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView homeScreen(ModelMap model) {
        return new ModelAndView("view");
    }   
}

application.properties

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

jsp路径

src/main/webapp/WEB-INF/jsp/view.jsp

【问题讨论】:

    标签: java spring-boot spring-data-jpa spring-boot-starter


    【解决方案1】:

    答案很简单,如果你用的是thymeleaf,在你的pom.xml中加入下面几行:

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

    就是这样,你必须把你的视图放在src / main / resources/templates的模板包中

    如果您使用 jsp 文件作为视图,上述方法不起作用,您必须在 application.properties 文件中添加以下行:

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

    前面几行表示驱动程序将在其中查找视图的文件夹,并且它们也必须存在于该文件夹结构中。

    和你的 pom.xml 这些行:

    **

    <dependency>
          <groupId> org.apache.tomcat.embed </groupId>
          <artifactId> tomcat-embed-jasper </artifactId>
          <scope> provided </scope>
        </dependency>
    

    **

    就是这样,spring boot表示它找不到请求的资源,但上面已经描述了它必须解决。

    【讨论】:

      【解决方案2】:

      默认情况下,Spring boot 不提供解析 Jsp for view layer 的能力。 将以下属性添加到

      build.gradle

      解决了问题

      implementation('org.apache.tomcat.embed:tomcat-embed-jasper')
      

      【讨论】:

        猜你喜欢
        • 2016-10-26
        • 2019-11-10
        • 2019-01-13
        • 2020-05-11
        • 1970-01-01
        • 2019-01-26
        • 2020-10-26
        • 2015-09-17
        相关资源
        最近更新 更多