5

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.

Ghassen Hamrouni
  • 3,138
  • 2
  • 20
  • 31
Saifeddine M
  • 483
  • 1
  • 5
  • 14
  • 3
    You cannot... This is also not really Spring related but an oversight (or not) in the java based servlet configuration which is defined by the Servlet Spec and not Spring. You could set the `/` to map to `index.html`. However this will only work for the root not for sub directories (as the welcome-file normally does). – M. Deinum Jan 21 '16 at 14:06
  • 2
    Refer to solutions regarding the same question (Servlet API 3.0 specific): http://stackoverflow.com/questions/13450044/how-to-define-welcome-file-list-and-error-page-in-servlet-3-0s-web-xml-less – Dennis Hunziker Jan 21 '16 at 14:08

3 Answers3

5

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.

OPK
  • 4,120
  • 6
  • 36
  • 66
2

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.

Community
  • 1
  • 1
Omkar Puttagunta
  • 4,036
  • 3
  • 22
  • 35
0

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