how to specify welcome-file-list in WebApplicationInitializer.onStartup()
Asked Answered
D

4

19

Currently I have a web application where we are using web.xml to configure the application. The web.xml has welcome-file-list.

<web-app>  
   ...
   <welcome-file-list>  
     <welcome-file>home.html</welcome-file>  
   </welcome-file-list>  
</web-app>  

We are planning to use spring framework and use java class for application configuration.

class MyApplication extends WebApplicationInitializer {
    public void onStartUp(ServletContext context){
        ...
    }
}

How do I specify welcome-file-list in this java class?

Deserted answered 22/6, 2015 at 5:33 Comment(1)
I don't believe you can.Alluvial
N
20

While developing Spring MVC application with pure Java Based Configuration, we can set the home page by making our application configuration class extending the WebMvcConfigurerAdapter class and override the addViewControllers method where we can set the default home page as described below.

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

  @Bean
  public InternalResourceViewResolver getViewResolver() {
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setPrefix("/WEB-INF/view/");
    viewResolver.setSuffix(".jsp");
    return viewResolver;
  }

  @Override
  public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/").setViewName("home");
  }

}

It returns home.jsp view which can be served as home page. No need to create a custom controller logic to return the home page view.

The JavaDoc for addViewControllers method says -

Configure simple automated controllers pre-configured with the response status code and/or a view to render the response body. This is useful in cases where there is no need for custom controller logic -- e.g. render a home page, perform simple site URL redirects, return a 404 status with HTML content, a 204 with no content, and more.

2nd way - For static HTML file home page we can use the code below in our configuration class. It will return index.html as a home page -

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

3rd way - The request mapping "/" below will also return home view which can be served as a home page for an app. But the above ways are recommended.

@Controller
public class UserController {
    @RequestMapping(value = { "/" })
    public String homePage() {
        return "home";
    }
}
Nuclease answered 30/11, 2015 at 9:52 Comment(0)
M
9

You can't

As specified in Java Doc

public interface WebApplicationInitializer

Interface to be implemented in Servlet 3.0+ environments in order to configure the ServletContext programmatically -- as opposed to (or possibly in conjunction with) the traditional web.xml-based approach.

but you still need minimal configuration in web.xml , such as for

<welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
Mcclimans answered 22/6, 2015 at 5:45 Comment(1)
Although this answer is correct (you cannot :) ). You might want to add that this isn't actually related to Spring but the fact that isn't implemented in the ``ServletContext` i.e. java based configuration approach for servlet containers. Even when not using Spring and using a plain ServletContainerInitializer it will not be possible, at least not in a servlet container agnostic way.Gasser
A
5
@EnableWebMvc
@Configuration
@ComponentScan("com.springapp.mvc")
public class MvcConfig extends WebMvcConfigurerAdapter {
...
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/*.html").addResourceLocations("/WEB-INF/pages/");
}

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

This might help.

Attalie answered 22/6, 2015 at 5:48 Comment(0)
R
0

this works for me...

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/").setViewName("forward:/index.html");
}
Rehm answered 9/7, 2020 at 20:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.