Saifeddine M
Saifeddine M

Reputation: 493

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.

Upvotes: 5

Views: 11515

Answers (3)

Gopalakrishnan
Gopalakrishnan

Reputation: 1

In the root controller,you can redirect the path to your path which you want to show as a welcomefile,

@Controller
public class WelcomePageController {

  @RequestMapping("/")
  public String redirectPage() {
    return "redirect:Welcome";
  }


  @RequestMapping("/Welcome")
  public String showHomePage() {
    return "index";
  }
}

Upvotes: 0

Omkar Puttagunta
Omkar Puttagunta

Reputation: 4156

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.

Upvotes: 2

OPK
OPK

Reputation: 4180

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.

Upvotes: 5

Related Questions