3

Hope you can help because as far as I can see, this is set up correctly (but please prove me wrong).

I have my spring 3 mvc project configured as follows:

web.xml

<servlet>
    <servlet-name>myServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>myServlet</servlet-name>
    <url-pattern>/frontPage</url-pattern>
</servlet-mapping>

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/myServlet-service.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

And myServlet-servlet.xml

<mvc:annotation-driven />
<context:component-scan base-package="my.path.to.controllers" />

<bean
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
</bean>

I have an index.jsp within WEB-INF (not WEB-INF/views) that does nothing more than say "Hello" (I was originally trying to get it to forward to /frontPage).

Now, if I type in the url for the controllers (localhost:8080/myServlet/frontPage), the controller works and the view is displayed, however when I first start up I get a 404 instead of the index.jsp page. I've tried adding a leading slash to index.jsp but that makes no difference.

I must have made a schoolboy error somewhere, but I can't for the life of me see where. Can anyone point it out for me?

Spring MVC 3.2 Tomcat 6 running in STS 2.9.1 servlet 2.5

Many thanks.

NilsH
  • 13,705
  • 4
  • 41
  • 59
Scribe74
  • 45
  • 1
  • 1
  • 3
  • btw It is wrong to put leading forward (or trailing) slash `/` before (or after) welcome file list element. See this answer for more info about defining welcome file list: http://stackoverflow.com/a/15533844/814702 – informatik01 Apr 05 '13 at 22:51

3 Answers3

12

The WEB-INF folder is not accessible publicly. So you have to put your index.jsp somewhere reachable, for instance in the web application root folder.

/mywebapp
    /WEB-INF/
    /index.jsp
NilsH
  • 13,705
  • 4
  • 41
  • 59
  • Oh my word! I can't believe I was that stupid! Told you it was a schoolboy error (there's a couple of hours of my life I won't get back). I was going so cross eyed I didn't notice where it was. I tried to up-vote your answer, but I don't have enough points (sorry). Many thanks. – Scribe74 Apr 05 '13 at 11:39
0

In case of java configuration you can override two methods in class that extends WebMvcConfigurerAdapter

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

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    configurer.enable();
}

If you wanna serve index.html explicitly, turn it into a resource override a method in the same class as below:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/index.html").addResourceLocations("/WEB-INF/views/index.html");
}

Of course addResourceLocations must follows the folder choosen to hold your views.

See these samples

Moesio
  • 3,100
  • 1
  • 27
  • 36
-1

This thing happens when you just begin. Just put the index.html(or whatever that welcome file can be) under WEB-INF. This can be done easily with a simple drag and drop. When you expand the folders in STS it may look like the welcome files are listed under WEB-INF while they are actually not.enter image description here