首页 文章

Spring Boot - 无法解析Whitelabel错误页面

提问于
浏览
0

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

我的项目结构如下所示 .

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

}

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页面

<%@ 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的显式映射,因此您将此视为回退 . Sun Sep 23 17:34:52 IST 2018出现意外错误(type = Not Found,status = 404) . 没有消息可用

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

2018-09-23 17:44:12.248 DEBUG 14728 --- [nio-8080-exec-2] osweb.servlet.DispatcherServlet:[/ EBS-web /]的Last-Modified值为:-1 ** 记录******** 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-09-23 17:44:12.258 DEBUG 14728 --- [nio-8080-exec-2] oswservlet.view.BeanNameViewResolver:无匹配为视图名称'login'找到的bean 2018-09-23 17:44:12.260 DEBUG 14728 --- [nio-8080-exec-2] oswsvContentNegotiatingViewResolver:返回[org.springframework.web.servlet.view.JstlView:name '登录'; 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'; DispatcherServlet中的URL [/WEB-INF/jsp/login.jsp]],名称为“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]是[/ **]

1 回答

  • 0

    请在控制器中定义方法:您可以定义 @RequestMapping(value = "/", method = RequestMethod.GET) ,也可以直接使用@GetMapping

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

    你的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.

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

    并且2.x版本之后无需延长 SpringBootServletInitializer 所以下面应该足以启动 .

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

相关问题