How To Set Up Spring MVC webapp to use DispatcherServlet AND have a default page of index.html

1.2k Views Asked by At

OK, this is getting ridiculous.

I asked basically this question a couple of days ago and got nowhere with it. I've tried all sorts of suggestions from all sorts of web pages and none gets me where I want to be. I've been all over the execrable Spring documentation which makes complicated things simple and simple things complicated.

Now I've learned a little more and maybe I can take a second chance at expressing myself, hopefully more clearly.

I want the following: A Spring MVC 4.0 web (not SpringBoot) application using the DispatcherServlet that ALSO displays the default index.html page whenever the basic URL http://myserver.mycompany.com:8080/myapp is entered into the browser.

It is a requirement that one should not have to type http://myserver.mycompany.com:8080/myapp/index.html to get to this page.

The DispatcherServlet will map URLs such as http://myserver.mycompany.com:8080/myapp/dothis and http://myserver.mycompany.com:8080/myapp/do/that to appropriately configured controllers.

I know this question has been asked over and over on the web and the answers all get lost in Spring minutia and none of them seem to work in my situation.

In other words I want the functionality of a welcome page as in a non-Spring webapp and the Spring-MVC dispatcher functionality in the same application. It does not have to be configured in web.xml. As long as it works as above.

Oh, and preferably I would like solutions that use Spring Annotations.

Simple question. I hope someone can provide me with a simple answer.

UPDATE: Answers to questions asked below: @JB_Nizet: Your suggestion, just use the code recommended in the spring doc and define the welcome page in web.xml is what I have been trying, and it doesn't work. And this makes sense. The Spring document approach ALLOWS static html documents to be served from /. But it still prevents the display of the welcome page from the root url. You can get to index.html by providing it in the url, but the mapping from / to /index.html does not take place.

@Aeseir: Yes, I would prefer the Java Config approach too which I referred to above as "Spring Annotations".

@rhinds: I'll answer your "stupid question" with one of my own: I have tried different methods of adding a controller that "just maps to "/", but I don't understand what this controller would have to DO. How do I code a controller that does nothing but serve index.html?

2

There are 2 best solutions below

0
On

If your index.html file is served as a static resource, then you can configure this with:

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

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

}
0
On

The answer was that there were some security filters in web.xml. I thought I had turned all that off by commenting out the security inclusions in the spring xml files, but I guess I had to turn the filters off. I didn't realize this until I had taken the time to remove all the XML from the application, in response to @JB_Nizet's fine example. It took awhile to find all the equivalent Java Config annotations but once I did that and it still didn't work, it was pretty easy to spot the filter as the culprit and comment it out. We will put it back when we're ready to bolt security on.

I have to say that I find it easier to locate configuration issues in one or two java classes than in several xml files located all over the place.