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

Sho*_*ham 121 java spring spring-boot

我是Java和Spring的新手.如何将我的应用程序根映射http://localhost:8080/到静态index.html?如果我导航到http://localhost:8080/index.html它的工作正常.

我的app结构是:

迪尔斯

config\WebConfig.java看起来像这样:

@Configuration
@EnableWebMvc
@ComponentScan
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("/");
        }
}
Run Code Online (Sandbox Code Playgroud)

我试图添加registry.addResourceHandler("/").addResourceLocations("/index.html");但它失败了.

Dav*_*yer 140

如果您没有使用@EnableWebMvc注释,它可以开箱即用.当你这样做时,你关掉Spring Boot为你做的所有事情WebMvcAutoConfiguration.您可以删除该注释,或者可以添加回您关闭的视图控制器:

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/").setViewName("forward:/index.html");
}
Run Code Online (Sandbox Code Playgroud)

  • 好吧我明白了.如果您还想将URL从`/`更改为`/ index.html`,请使用`"redirect:/index.html"`而不是forward. (10认同)
  • 来自[参考文档](http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-spring-mvc-auto-configuration):"如果你想保留Spring启动MVC功能,你只想添加额外的MVC配置(拦截器,格式化程序,视图控制器等),你可以添加你自己的@Bean类型`WebMvcConfigurerAdapte`r,但没有`@ EnableWebMvc`" (9认同)
  • 我没有使用@EnableWebMvc注释,我没有被重定向到index.html. (4认同)

jus*_*tin 43

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");
            }
        };
    }

}
Run Code Online (Sandbox Code Playgroud)

  • 在Spring 5中使用WebMvcConfigurer而不是弃用的WebMvcConfigurerAdapter. (8认同)

小智 18

如果它是一个Spring启动应用程序.

Spring Boot会自动检测public/static/webapp文件夹中的index.html.如果您已编写任何控制器@Requestmapping("/"),它将覆盖默认功能,index.html除非您键入,否则不会显示localhost:8080/index.html

  • 我创建了一个src/main/resources/public/index.html文件,它工作正常!谢谢 (3认同)

sam*_*hlk 12

更新:2019 年 1 月

首先在资源下创建公共文件夹并创建 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");
    }

}
Run Code Online (Sandbox Code Playgroud)


zap*_*pee 10

如果您使用spring-boot 2.1.6.RELEASE带有简单@RestController注释的最新版本,那么您无需执行任何操作,只需将您的index.html文件添加到resources/static文件夹下:

project
  ??? src
      ??? main
          ??? resources
              ??? static
                  ??? index.html
Run Code Online (Sandbox Code Playgroud)

然后点击 URL http://localhost:8080。希望对大家有所帮助。


Rod*_*iro 7

@Configuration  
@EnableWebMvc  
public class WebAppConfig extends WebMvcConfigurerAdapter {  

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addRedirectViewController("/", "index.html");
    }

}
Run Code Online (Sandbox Code Playgroud)


小智 7

您可以添加一个 RedirectViewController,例如:

@Configuration
public class WebConfiguration implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addRedirectViewController("/", "/index.html");
    }
}
Run Code Online (Sandbox Code Playgroud)


Ari*_*afa 5

在里面Spring Boot,我总是把网页放在像publicorwebapps或 or这样的文件夹中views,并将它放在src/main/resources目录中,你也可以看到application.properties

Spring_Boot-Project-Explorer-View

这是我的application.properties

server.port=15800
spring.mvc.view.prefix=/public/
spring.mvc.view.suffix=.html
spring.datasource.url=jdbc:mysql://localhost:3306/hibernatedb
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.format_sql = true

logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
Run Code Online (Sandbox Code Playgroud)

一旦您输入类似 url 的 urlservername:15800并且 Spring Boot 接收到的这个请求占用了 Servlet 调度程序,它就会精确搜索index.html并且这个名称将区分大小写,因为spring.mvc.view.suffix它是 html、jsp、htm 等。

希望它能帮助很多人。


小智 5

我有同样的问题。Spring Boot 知道静态 html 文件所在的位置。

  1. 将index.html添加到resources/static文件夹中
  2. 然后删除根路径的完整控制器方法,例如 @RequestMapping("/") 等
  3. 运行应用程序并检查http://localhost:8080(应该可以)