遇到个奇怪的问题:IDEA中运行Spring boot web项目无法访问页面;
同样的代码,eclipse能运行也能访问到页面,IDEA能运行能直接访问后端接口但不能访问页面,错误提示如下:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sat Apr 28 13:23:48 CST 2018
There was an unexpected error (type=Not Found, status=404).
No message available经过多方查找原因发现eclipse和IDEA构建的方式存在差异,idea创建工程的方式有关.

如果你将idea工程创建在open-platform这一级,在IDEA中运行module1时,在C:\Users\xxx\\AppData\Local\Temp这个目录下,会生成两个tomcat目录, 如下:

其中, tomcat.xxxx.port这个目录下没有自动生成页面资源和class文件。
但是如果将idea工程建在open-console这一级,
在IDEA中运行module1时,在C:\Users\xxx\AppData\Local\Temp这个目录下,只会生成tomcat.xxxx.port一个目录,而且当你在IE中访问时,在这个路径下会生成相应页面资源和class文件,相应的页面也可以访问到,不会出现异常;
在eclipse里运行没有异常,eclipse运行时只会生成tomcat.xxx.port目录,并且会自动生成jsp java和class文件。
解决方式:
1. 进入open-console目录,使用mvn spring-boot:run方式启动,不会出现页面文件找不到问题
2. 在open-console中,增加如下配置类,也可以解决jsp文件找不到问题
package com.winxuan.open;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.File;
/**
* @author leitao.
* @time: 2018/4/27 19:53
* @version: 1.0
* @description:
**/
@Configuration
public class CommonConfiguration {
@Bean
public EmbeddedServletContainerFactory embeddedServletContainerFactory(){
ConfigurableEmbeddedServletContainer factory = new TomcatEmbeddedServletContainerFactory();
factory.setDocumentRoot(new File("E:\\WorkSpace\\open-platform\\open-console\\src\\main\\webapp\\"));
return (EmbeddedServletContainerFactory) factory;
}
}
3.其实直接运行spring-boot:run是一样的结果,如图

补充:Spring boot 启动web项目是报这个错解决办法:

<scope>provided</scope>
注释掉就OK了
本文介绍了解决IDEA运行Spring Boot Web项目时无法访问页面的问题。通过调整项目创建位置、使用Maven运行或配置EmbeddedServletContainerFactory等方法解决了页面资源未加载的问题。
3万+

被折叠的 条评论
为什么被折叠?



