1

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?

Community
  • 1
  • 1
Steve Cohen
  • 4,679
  • 9
  • 51
  • 89
  • http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#mvc-default-servlet-handler – JB Nizet Jun 25 '15 at 22:41
  • Thanks. I've been to this page, though, and it still confuses me. I don't see where in this page it shows the thing that maps the default application url to index.html. It's missing some piece of information that my feeble spring-noob brain is not grokking. Do you have to write a custom servlet to serve index.html? – Steve Cohen Jun 25 '15 at 22:48
  • No. You just use the code (XML or Java-Config) that is in the documentation, and Spring will delegate to the default servlet for static resources. So it will serve static resources from the webapp just as if you didn't use Sring at all. Default pages are configured in the web.xml file, as the servlet specification tells. By default, index.html is one of the default pages. – JB Nizet Jun 25 '15 at 22:51
  • @SteveCohen what format you setting up your Spring MVC in XML or Java Config? I personally recommend Java Config as it becomes a lot simple to get up and going and gives greater amount of flexibility. – Aeseir Jun 26 '15 at 00:57
  • This is probably a stupid question, but assume you have your controller for the default home page that just maps to / ? – rhinds Jun 26 '15 at 09:31
  • It works as expected and documented here. If you want help, show your code. Here's the simplest project I can imagine: https://github.com/jnizet/springmvcdemo. Clone it, execute `./gradlew build` from the project directory, copy the build/libs/ROOT.war to the webapps directory of tomcat, and go to localhost:8080: you'll see the index.html page. Go to http://localhost:8080/api/hello, and you'll se JSON generated by a Spring rest controller. – JB Nizet Jun 26 '15 at 14:10
  • @JB_Nizet - thanks! Your sample is very clear and easy to understand. It should be more widely circulated.. Something like it should be included in Spring documentation. I haven't built or run it, but it's all quite understandable. My problem is that I inherited starter code from someone else that includes both annotations and Spring xml configuration and wires must be crossed somewhere. I probably need to straighten this out. Is there Spring documentation that indicates which annotation to use to replace different pieces of xml configuration? – Steve Cohen Jun 26 '15 at 17:31
  • I assume that your sample is relying on default behavior that makes index.html the welcome page since nothing in the code specifies that. – Steve Cohen Jun 26 '15 at 17:39
  • Yes. Tomcat (and all the other servers, AFAIK) defines default values for "default pages". index.html is one of them. If you want others, you would have to define them in web.xml. Regarding xml vs. java config and annotations, the reference documentation usually gives examples using both. So if you find your xml tag in the documentation, the equivalent java config is usually not far from it. – JB Nizet Jun 26 '15 at 17:54
  • Something is very whacked-out about your github repository or my setup. I tried to clone your repository (https://github.com/jnizet/springmvcdemo.git) and when I tried to import it as an Eclipse project I got something completely different than what I cloned from Git. How the #$%^&* can that happen? – Steve Cohen Jun 26 '15 at 19:30
  • @JB_Nizet - Put your demo as the answer and I'll be happy to accept it, now that I've learned to use Gradle with Eclipse. – Steve Cohen Jun 27 '15 at 20:44

2 Answers2

4

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

}
Brian Clozel
  • 56,583
  • 15
  • 167
  • 176
1

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.

Steve Cohen
  • 4,679
  • 9
  • 51
  • 89