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

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

我的应用程序结构是:

dirs

我的 config\WebConfig.java是这样的:

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


@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("/");
}
}

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

243634 次浏览

如果您没有使用 @EnableWebMvc注释,那么它本可以立即工作。当你这样做的时候,你关闭了所有的事情,春天启动为你在 WebMvcAutoConfiguration。您可以删除这个注释,或者您可以添加回您关闭的视图控制器:

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

戴夫•塞耶(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");
}
};
}


}

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

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

@Configuration
@EnableWebMvc
public class WebAppConfig extends WebMvcConfigurerAdapter {


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


}
  1. Html 文件应该位于下面的位置- Src/resources/public/index.html OR Src/resources/static/index.html 如果两个位置都定义了,那么首先出现的 index.html 将从该目录调用。
  2. 源代码看起来像-

    package com.bluestone.pms.app.boot;
    import org.springframework.boot.Banner;
    import org.springframework.boot.Banner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.boot.web.support.SpringBootServletInitializer;
    import org.springframework.context.annotation.ComponentScan;
    
    
    
    
    
    
    @SpringBootApplication
    @EnableAutoConfiguration
    @ComponentScan(basePackages = {"com.your.pkg"})
    public class BootApplication extends SpringBootServletInitializer {
    
    
    
    
    
    
    /**
    * @param args Arguments
    */
    public static void main(String[] args) {
    SpringApplication application = new SpringApplication(BootApplication.class);
    /* Setting Boot banner off default value is true */
    application.setBannerMode(Banner.Mode.OFF);
    application.run(args);
    }
    
    
    /**
    * @param builder a builder for the application context
    * @return the application builder
    * @see SpringApplicationBuilder
    */
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder
    builder) {
    return super.configure(builder);
    }
    }
    

Spring Boot中,我总是把网页放在像 publicwebapps或者 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

只要你放入像 servername:15800这样的 URL,Spring Boot 占用 Servlet 调度器接收到的这个请求,它就会精确地搜索 index.html,而且这个名字会像 spring.mvc.view.suffix一样区分大小写,比如 html、 jsp、 htm 等等。

希望能对大家有所帮助。

更新: 2019年1月

首先在资源下创建公用文件夹并创建 index.html 文件。 使用 WebMvcConfigrer 而不是 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");
}


}

我也有同样的问题。 Spring 启动知道静态 html 文件的位置。

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

如果您使用最新的 spring-boot 2.1.6.RELEASE和一个简单的 @RestController注释,那么您不需要做任何事情,只需将您的 index.html文件添加到 resources/static文件夹下:

project
├── src
   ├── main
       └── resources
           └── static
               └── index.html

然后点击应用程序 http://localhost:8080的根 URL。

上面的解决方案可以使用 Spring 和 Tomcat 进行开箱即用,您对根 /的 HTTP 请求将自动映射到 index.html文件。但是如果您使用 @EnableWebMvc注释,那么您可以关闭 Spring Boot 为您做的注释。在这种情况下,您有两个选择:

(1)删除该注释

(2) 或者您可以添加回您关闭的视图控制器

@Configuration
public class WebConfiguration implements WebMvcConfigurer {


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

}

希望对大家有所帮助。

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

@Configuration
public class WebConfiguration implements WebMvcConfigurer {


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