Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the welcome page for Spring MVC 4 with Java configuration

Im trying to migrate from XML to full java class based config in Spring MVC 4. What I did so far is the creation of a simple WebAppInitializer class and a WebConfig class.

But, I can't find a way to config my welcome page, Here's an excerpt from my old Web.xml:

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

Any help would be appreciated.

like image 236
Saifeddine M Avatar asked Jan 21 '16 13:01

Saifeddine M


2 Answers

You do not need to do anything actually, spring automatically looks for index.html file under src/main/webapp, all you need to do is create a index.html file and put it under this root.

like image 106
OPK Avatar answered Oct 04 '22 22:10

OPK


You can do this by overriding the addViewControllers method of WebMvcConfigurerAdapter class.

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.myapp.controllers" })
public class ApplicationConfig extends WebMvcConfigurerAdapter {

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

See my answer for more information.

Using this configuration you can set any filename as a welcome/home page.

like image 45
Omkar Puttagunta Avatar answered Oct 04 '22 22:10

Omkar Puttagunta