IDEA中运行Spring boot web项目无法访问页面

遇到个奇怪的问题: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了







  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值