java - Spring Boot - 无法解决 Whitelabel 错误页面

标签 java spring-mvc jsp spring-boot

我试图运行一个简单的 Spring boot 应用程序 2 天,但仍然无法使其工作。我检查了所有相关问题和博客,但问题仍然存在。

我的项目结构如下所示。

Project Structure

POM.xml

<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);
    }

}

应用程序属性

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

登录 Controller

@Controller
public class LoginController {

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

}

登录.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>

每当我尝试将应用程序作为 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.

Sun Sep 23 17:34:52 IST 2018 There was an unexpected error (type=Not Found, status=404). No message available

从下面的堆栈跟踪中,我可以看到 spring 能够获取处理程序方法,但无法找到关联的 View 。

2018-09-23 17:44:12.248 DEBUG 14728 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/EBS-web/] is: -1

******************logging************************

2018-09-23 17:44:12.258 DEBUG 14728 --- [nio-8080-exec-2] o.s.w.s.v.ContentNegotiatingViewResolver : 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] o.s.w.servlet.view.BeanNameViewResolver : No matching bean found for view name 'login' 2018-09-23 17:44:12.260 DEBUG 14728 --- [nio-8080-exec-2] o.s.w.s.v.ContentNegotiatingViewResolver : Returning [org.springframework.web.servlet.view.JstlView: name 'login'; 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] o.s.web.servlet.DispatcherServlet : Rendering view [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] o.s.web.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] o.s.web.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] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /WEB-INF/jsp/login.jsp 2018-09-23 17:44:12.268 DEBUG 14728 --- [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/WEB-INF/jsp/login.jsp] 2018-09-23 17:44:12.268 DEBUG 14728 --- [nio-8080-exec-2] o.s.w.s.handler.SimpleUrlHandlerMapping : Matching patterns for request [/WEB-INF/jsp/login.jsp] are [/**]

最佳答案

添加@RestController而不是@Controller。然后运行您的 Java 应用程序。这应该可以解决 WhiteLabel 错误

关于java - Spring Boot - 无法解决 Whitelabel 错误页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52466063/

相关文章:

java - Spring MVC Web应用程序-Model的正确使用

java - 用Java从ZIP压缩包中提取UTF-16编码的文件

java - Spring - addResourceHandlers 未使用 Rest Controller 解析静态资源

java - 一次以固定数量的结果流式传输 MySql ResultSet

java - 如何在登录页面上确定是否有用户已经登录?

java - 表单未在 Controller 上为 sql.date 提供值

javascript - 自动完成搜索建议下拉弹出窗口

java - mysql更新查询中的自动增量

java - 使用正则表达式提取两个标签之间的文本

Java e(fx)clipse build.build - 以编程方式获取版本号