Java Spring Download: how to map application root ("/") to index.html?

I am new to Java and Spring. How can I map the application root http://localhost:8080/ to static index.html ? If I go to http://localhost:8080/index.html , it works fine.

My application structure:

dirs

My config\WebConfig.java looks like this:

 @Configuration @EnableWebMvc @ComponentScan public class WebConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**").addResourceLocations("/"); } } 

I tried to add registry.addResourceHandler("/").addResourceLocations("/index.html"); but it does not work.

+118
java spring spring-boot
Dec 09 '14 at 2:49
source share
7 answers

It would be helpful if you did not use the @EnableWebMvc annotation. When you do this, you will disable everything that Spring Boot does for you in WebMvcAutoConfiguration . You can delete this annotation or add back the view controller that you disabled:

 @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("forward:/index.html"); } 
+137
Dec 09 '14 at 16:08
source share

Dave Sier’s answer example:

 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"); } }; } } 
+43
Jul 05 '15 at 23:27
source share

if this is a Spring boot application.

Spring Download automatically detects index.html in the public / static / web folder. If you wrote any @Requestmapping("/") controller, it will override the default function and will not show index.html unless you type localhost:8080/index.html

+17
Jul 20 '17 at 19:51
source share
 @Configuration @EnableWebMvc public class WebAppConfig extends WebMvcConfigurerAdapter { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addRedirectViewController("/", "index.html"); } } 
+6
Aug 16 '17 at 4:00
source share

Update: January-2019

First create a shared folder under the resources and create the index.html file. Use WebMvcConfigurer instead of 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"); } } 
+4
Jan 18 '19 at 19:46
source share

Inside Spring Boot I always put web pages in a folder such as public or webapps or views and place it in the src/main/resources directory as you can see in application.properties .

Spring_Boot-Project-Explorer-View

and this is my 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 

as soon as you put a URL, for example servername:15800 and this request received by the Spring Boot manager will look for index.html exactly and this name will be in the case when spring.mvc.view.suffix will be html, jsp, htm etc.

Hope this helps a lot.

+3
Apr 16 '18 at 14:38
source share
  1. The index.html file should be located in the following location - src / resources / public / index.html OR src / resources / static / index.html if both definitions defined then in which index.html is called first are called from this directory .
  2. The source code looks like this:

     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); } } 
+2
Mar 16 '18 at 17:32
source share



All Articles