SpingBoot整合Vue,静态资源映射,配置端口直接访问

先把Vue打包好的文件放到自己想要放的目录,这里我放在/home/web/resource下面

1.配置文件application.yml,配置访问的路径

web-config-resourcesHandler: /resource/**

2.配置Vue页面存放的路径

web-config-resourcesLocation: file:/home/web/resource/

3.编写WebConfig配置类,配置默认路由将根路径重定向到index.html

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Value("${web-config-resourcesHandler}")
    private String[] resourcesHandler;

    @Value("${web-config-resourcesLocation}")
    private String[] resourcesLocation;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        for (int i = 0; i < resourcesHandler.length; i++) {
            registry.addResourceHandler(resourcesHandler[i]).addResourceLocations(resourcesLocation[i]);
        }
    }


    // 这里是将主页面设置输入IP:端口就可以直接访问
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        // 先检查文件是否存在
        File indexFile = new File("/home/web/resource/index.html");
        if (indexFile.exists()) {
            registry.addViewController("/")
                    .setViewName("redirect:/resource/index.html");
        }
    }
}

4.启动之后就可以直接通过127.0.0.1:端口,访问到主页面:/resource/index.html

    评论
    成就一亿技术人!
    拼手气红包6.0元
    还能输入1000个字符
     
    红包 添加红包
    表情包 插入表情
     条评论被折叠 查看
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

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

    抵扣说明:

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

    余额充值