Spring Boot Whitelabel 错误页面几乎无处不在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44007326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:25:04  来源:igfitidea点击:

Spring Boot Whitelabel Error Page almost everywhere

springmavenjspspring-boot

提问by Zhannur Diyas

Facing some difficulties trying to run Spring Boot to work with JSP pages. I know the limitations provided in documentation, so made everything according to the rules. However, can't find a problem.

在尝试运行 Spring Boot 以处理 JSP 页面时遇到一些困难。我知道文档中提供的限制,所以一切都按照规则进行。但是,找不到问题。

I'll try to provide as much information as possible.

我会尽量提供尽可能多的信息。

Wanted to start using Spring Boot. I'm using:

想开始使用 Spring Boot。我正在使用:

IntelliJ IDEA, Maven, Maven Spring Boot Plugin, Embedded Tomcat. Linkto the project sample.

IntelliJ IDEA、Maven、Maven Spring Boot 插件、嵌入式 Tomcat。链接到项目示例。

The REST controller works fine, but when I try to link jsp to the common controller, I 100% end up with Whitelabel error page, stating 404.

REST 控制器工作正常,但是当我尝试将 jsp 链接到通用控制器时,我 100% 最终会看到 Whitelabel 错误页面,指出 404。

Moving files from WEB-INFto resources/staticdoesn't help.

将文件从 移动WEB-INFresources/static没有帮助。

The problem is even when I'm trying to run the simplest quick-start Spring Boot projects, I'm facing exceptions. Here's exception log when I tried to load the example from mkyong site.

问题是,即使当我尝试运行最简单的快速启动 Spring Boot 项目时,我也遇到了异常。是我尝试从 mkyong 站点加载示例时的异常日志。

I tried to manage maven dependencies manually, as I browsed through some "potential" solutions and found that it could be jar conflict. I even tried to launch the project on a completely fresh operational system - that's the level of my despair.

我尝试手动管理 maven 依赖项,因为我浏览了一些“潜在”解决方案,发现可能是 jar 冲突。我什至试图在一个全新的操作系统上启动这个项目——这就是我的绝望程度。

Any guidance will be accepted as a divine ambrosia.

任何指导都将被接受为神圣的佳肴。

UPDATE

更新

When trying to run the project again using mvn spring-bootafter adding javax.servlet-apiin dependencies got the following error. Log:

mvn spring-boot添加javax.servlet-api依赖项后尝试再次运行项目时出现以下错误。日志:

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-
maven-plugin:1.4.5.RELEASE:run (default-cli) on project demo: An 
exception occurred while running. null: InvocationTargetException: 
Unable to start embedded container; nested exception is org.springframework.boot.context.embedded.EmbeddedServletContainerExceptio
n: Unable to start embedded Tomcat: Failed to start component 
[StandardServer[-1]]: Failed to start component [StandardService[Tomcat]]: 
Failed to start component [StandardEngine[Tomcat]]: A child container 
failed during start -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with 
the -
e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, 
please read the following articles:
[ERROR] [Help 1] 
http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

UPDATE

更新

Using suggestion of @Piotr Gwiazda, tried the same but with Thymeleaf instead of JSP. Worked. However, I would like to know why JSP did not.

使用@Piotr Gwiazda 的建议,尝试了相同的方法,但使用 Thymeleaf 而不是 JSP。工作了。但是,我想知道为什么 JSP 没有。

采纳答案by Achille_vanhoutte

For some reason, your code has two webapp folders. One (wrong location)under src/and the other one under (correct)src/main/. Now your WEB-INF/jsp whas in the wrong one. So, move it to the appropriate folder.

出于某种原因,您的代码有两个 webapp 文件夹。一个(错误的位置)在src/ 下,另一个在(正确的)src/main/ 下。现在你的 WEB-INF/jsp 出错了。因此,将其移动到相应的文件夹。

enter image description here

在此处输入图片说明

回答by niteshbisht

you can use one such sample configuration

您可以使用一种这样的示例配置

Create src/main/resources source package and place the static resources in that location.

创建 src/main/resources 源包并将静态资源放置在该位置。

@Configuration
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
            "classpath:/resources/", "classpath:/static/", "classpath:/public/" };

    @Bean
    public ViewResolver getViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        //resolver.setPrefix("/resources/");
        resolver.setSuffix(".html");
        return resolver;
    }

     @Override
        public void configureDefaultServletHandling(
                DefaultServletHandlerConfigurer configurer) {
            configurer.enable();
        }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
    }

}

回答by Piotr Gwiazda

Seems that you're trying to use JSP page as template for view rendering for HelloController. Means that JSP file should be probably in src/main/resources/templates.

似乎您正在尝试使用 JSP 页面作为HelloController. 意味着 JSP 文件应该在src/main/resources/templates.

Mind that JSPs are not the suggested way for creating views. I'd give Thymeleafor others a chance. See here

请注意,JSP 不是创建视图的建议方式。我会给Thymeleaf或其他人一个机会。看这里