为了账号安全,请及时绑定邮箱和手机立即绑定

Java Spring Boot:如何将我的应用程序根目录(“ /”)映射到index.html?

Java Spring Boot:如何将我的应用程序根目录(“ /”)映射到index.html?

繁花不似锦 2019-11-08 14:21:18
我是Java和Spring的新手。如何将我的应用程序根目录映射http://localhost:8080/到静态目录index.html?如果我导航到http://localhost:8080/index.html它的作品很好。我的应用程序结构为:rs我的config\WebConfig.java样子是这样的:@Configuration@EnableWebMvc@ComponentScanpublic class WebConfig extends WebMvcConfigurerAdapter {    @Override    public void addResourceHandlers(ResourceHandlerRegistry registry) {        registry.addResourceHandler("/**").addResourceLocations("/");        }}我尝试添加,registry.addResourceHandler("/").addResourceLocations("/index.html");但是失败。
查看完整描述

3 回答

?
心有法竹

TA贡献1866条经验 获得超5个赞

Dave Syer的答案的示例:


import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;


@Configuration

public class MyWebMvcConfig {


    @Bean

    public WebMvcConfigurerAdapter forwardToIndex() {

        return new WebMvcConfigurerAdapter() {

            @Override

            public void addViewControllers(ViewControllerRegistry registry) {

                // forward requests to /admin and /user to their index.html

                registry.addViewController("/admin").setViewName(

                        "forward:/admin/index.html");

                registry.addViewController("/user").setViewName(

                        "forward:/user/index.html");

            }

        };

    }


}


查看完整回答
反对 回复 2019-11-08
?
慕村9548890

TA贡献1884条经验 获得超4个赞

首先在资源下创建公用文件夹,然后创建index.html文件。使用WebMvcConfigurer而不是WebMvcConfigurerAdapter。


import org.springframework.context.annotation.Configuration;

import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration

public class WebAppConfig implements WebMvcConfigurer {


    @Override

    public void addViewControllers(ViewControllerRegistry registry) {

        registry.addViewController("/").setViewName("forward:/index.html");

    }


}


查看完整回答
反对 回复 2019-11-08
  • 3 回答
  • 0 关注
  • 1171 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信