简体   繁体   English

Spring Boot - 无法解决白标签错误页面

[英]Spring Boot - Unable to resolve Whitelabel Error Page

I am trying to run a simple Spring boot application from 2 days but still unable to make it work.我试图从 2 天开始运行一个简单的 Spring 启动应用程序,但仍然无法使其工作。 I checked all the related questions and blogs but still issue persist.我检查了所有相关的问题和博客,但问题仍然存在。

My project structure appears as below.我的项目结构如下所示。

项目结构

POM.xml POM文件

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <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-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
     <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
</dependencies>

WebApplication.java网络应用程序

@SpringBootApplication
public class WebApplication extends SpringBootServletInitializer{


    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(WebApplication.class);
    }

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

}

Application.properties应用程序属性

server.servlet.context-path=/EBS-web
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
logging.level.org.springframework.web=DEBUG

LoginController登录控制器

@Controller
public class LoginController {

    @RequestMapping(path="/")
    public String login() {
        System.out.println("******************logging************************");
        return "login";
    }

}

Login.jsp登录.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Hi Login
</body>
</html>

Whenever I try to run the application as Spring Boot App (STS plugin for Eclipse) and access the http://localhost:8080/EBS-web/ It gives below error message on UI每当我尝试将应用程序作为 Spring Boot App(Eclipse 的 STS 插件)运行并访问http://localhost:8080/EBS-web/它在 UI 上给出以下错误消息

Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.白标错误页面 此应用程序没有明确的 /error 映射,因此您将其视为后备。

Sun Sep 23 17:34:52 IST 2018 There was an unexpected error (type=Not Found, status=404). Sun Sep 23 17:34:52 IST 2018 出现意外错误(类型=未找到,状态=404)。 No message available没有可用的消息

From the below stack trace, I can see spring is able to fetch the handler method but not able to find the associated view.从下面的堆栈跟踪中,我可以看到 spring 能够获取处理程序方法,但无法找到关联的视图。

2018-09-23 17:44:12.248 DEBUG 14728 --- [nio-8080-exec-2] osweb.servlet.DispatcherServlet : Last-Modified value for [/EBS-web/] is: -1 2018 年 9 月 23 日 17:44:12.248 调试 14728 --- [nio-8080-exec-2] osweb.servlet.DispatcherServlet:[/EBS-web/] 的最后修改值为:-1

******************logging************************ ******************日志记录************************

2018-09-23 17:44:12.258 DEBUG 14728 --- [nio-8080-exec-2] oswsvContentNegotiatingViewResolver : Requested media types are [text/html, application/xhtml+xml, image/webp, image/apng, application/xml;q=0.9, / ;q=0.8] based on Accept header types and producible media types [ / ]) 2018-09-23 17:44:12.258 DEBUG 14728 --- [nio-8080-exec-2] oswservlet.view.BeanNameViewResolver : No matching bean found for view name 'login' 2018-09-23 17:44:12.260 DEBUG 14728 --- [nio-8080-exec-2] oswsvContentNegotiatingViewResolver : Returning [org.springframework.web.servlet.view.JstlView: name 'login'; 2018-09-23 17:44:12.258 DEBUG 14728 --- [nio-8080-exec-2] oswsvContentNegotiatingViewResolver:请求的媒体类型是 [text/html, application/xhtml+xml, image/webp, image/apng, application /xml;q=0.9,/ q = 0.8]根据Accept报头类型和可生产的媒体类型[/])2018年9月23日17:44:12.258 DEBUG 14728 --- [NIO-8080-EXEC-2] oswservlet.view.BeanNameViewResolver:未找到与视图名称“登录”匹配的 bean 2018-09-23 17:44:12.260 DEBUG 14728 --- [nio-8080-exec-2] oswsvContentNegotiatingViewResolver:返回 [org.spring.framework] servlet.view.JstlView:名称“登录”; URL [/WEB-INF/jsp/login.jsp]] based on requested media type 'text/html' 2018-09-23 17:44:12.260 DEBUG 14728 --- [nio-8080-exec-2] osweb.servlet.DispatcherServlet : Rendering view [org.springframework.web.servlet.view.JstlView: name 'login'; URL [/WEB-INF/jsp/login.jsp]] 基于请求的媒体类型 'text/html' 2018-09-23 17:44:12.260 DEBUG 14728 --- [nio-8080-exec-2] osweb。 servlet.DispatcherServlet : 渲染视图 [org.springframework.web.servlet.view.JstlView: name 'login'; URL [/WEB-INF/jsp/login.jsp]] in DispatcherServlet with name 'dispatcherServlet' 2018-09-23 17:44:12.264 DEBUG 14728 --- [nio-8080-exec-2] osweb.servlet.view.JstlView : Forwarding to resource [/WEB-INF/jsp/login.jsp] in InternalResourceView 'login' 2018-09-23 17:44:12.266 DEBUG 14728 --- [nio-8080-exec-2] osweb.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/EBS-web/WEB-INF/jsp/login.jsp] 2018-09-23 17:44:12.267 DEBUG 14728 --- [nio-8080-exec-2] swsmmaRequestMappingHandlerMapping : Looking up handler method for path /WEB-INF/jsp/login.jsp 2018-09-23 17:44:12.268 DEBUG 14728 --- [nio-8080-exec-2] swsmmaRequestMappingHandlerMapping : Did not find handler method for [/WEB-INF/jsp/login.jsp] 2018-09-23 17:44:12.268 DEBUG 14728 --- [nio-8080-exec-2] oswshandler.SimpleUrlHandlerMapping : Matching patterns for request [/WEB-INF/jsp/login.jsp] are [/**] URL [/WEB-INF/jsp/login.jsp]] 在 DispatcherServlet 中,名称为“dispatcherServlet” 2018-09-23 17:44:12.264 DEBUG 14728 --- [nio-8080-exec-2] osweb.servlet.view .JstlView : 转发到 InternalResourceView 'login' 中的资源 [/WEB-INF/jsp/login.jsp] 2018-09-23 17:44:12.266 DEBUG 14728 --- [nio-8080-exec-2] osweb.servlet .DispatcherServlet : DispatcherServlet 名为 'dispatcherServlet' 处理 [/EBS-web/WEB-INF/jsp/login.jsp] 的 GET 请求 2018-09-23 17:44:12.267 DEBUG 14728 --- [nio-8080-exec -2] swsmmaRequestMappingHandlerMapping :查找路径 /WEB-INF/jsp/login.jsp 的处理程序方法 2018-09-23 17:44:12.268 DEBUG 14728 --- [nio-8080-exec-2] swsmmaRequestMappingHandlerMapping :没有找到[/WEB-INF/jsp/login.jsp] 2018-09-23 17:44:12.268 DEBUG 14728 --- [nio-8080-exec-2] oswshandler.SimpleUrlHandlerMapping 的处理程序方法:请求匹配模式[/WEB -INF/jsp/login.jsp] 是 [/**]

Add @RestController instead of @Controller .添加@RestController而不是@Controller Then run your Java application.然后运行您的 Java 应用程序。 this should solve the WhiteLabel error这应该可以解决WhiteLabel错误

Please define method in your controller: You may define @RequestMapping(value = "/", method = RequestMethod.GET) or you may directly use @GetMapping请在你的控制器中定义方法:你可以定义@RequestMapping(value = "/", method = RequestMethod.GET)或者你可以直接使用@GetMapping

@Controller
public class LoginController {

    @GetMapping(path="/")
    public String login() {
        System.out.println("******************logging************************");
        return "login";
    }

}

there might be some more conflicts in your pom, like no need add tomcat dependency as its already embedded so below can be removed.您的 pom 中可能存在更多冲突,例如无需添加 tomcat 依赖项,因为它已经嵌入,因此可以删除以下内容。

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

To enable support for JSP's, add a dependency on tomcat-embed-jasper.要启用对 JSP 的支持,请添加对 tomcat-embed-jasper 的依赖。

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

and after 2.x release no need to extend SpringBootServletInitializer so below should be sufficient to start.并且在 2.x 版本之后不需要扩展SpringBootServletInitializer所以下面应该足以启动。

@SpringBootApplication
public class WebApplication{


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

}

Please make sure all the classess are under the root package.请确保所有类都在根包下。 I spent hours in this error.我在这个错误上花了几个小时。 My application was running on self defined port 8085,but while hitting that port there was no response.我的应用程序在自定义端口 8085 上运行,但是在访问该端口时没有响应。 i was not getting error in the console.我没有在控制台中收到错误。 after restructuring the package under root package.在root包下重组包后。 Error is Resolved!!!!!!!!!!错误已解决!!!!!!!!!!

Happy Learning :)快乐学习:)

Regards, Vicky Sharma问候, 维姬·夏尔马

只需在主生成的包中添加您的类文件,然后错误就会得到解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM