1
votes

I'm working with Spring MVC on google app engine and even though I've gotten a basic hello world example working, I can't get my servlet to show up when I use the request mapping of "/". Even when I specify "/" as my request mapping in my controller, I keep getting the "Hello App Engine!" page with a link to my project. I've already pulled the welcome-file declaration out of my web xml.

Basically...

package my.package.for.spring.stuff.controllers;

import ....;

// It doesn't seem to make a difference if 
// I have this reqeustmapping or not...
@Controller
public class MainController {

  // If I change mapping to "/main" and then go to
  // localhost:8888/main then everything works as expected
  @RequestMapping("/")
  public String HelloWorld() {
    return "MyView";
  }
}

is still going to the "Hello App Engine!" page. Also, here is my web.xml...

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">


    <servlet>
        <servlet-name>SpringAppEngine</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringAppEngine</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

And then here is my spring xml...

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

        <context:component-scan base-package="my.package.for.spring.stuff" />

        <bean id="viewResolver"
                class="org.springframework.web.servlet.view.InternalResourceViewResolver"
                p:prefix="/WEB-INF/views/main/" p:suffix=".jsp" />

</beans>

Why is the app engine generated welcome file still showing up even though I'm declaring the root mapping in my controller? I know my setup should be right because when I change the requestmapping, everything works as expected.

3
does it work if you change your mapping to something else? can you show your spring xml? - Igor Artamonov
Yes, it does work if I change the mapping to anything else and then go to localhost:8888/anythingelse. Im not at a place where I can get my other xmls. Ill have to post those later this evening. - Dave
I've updated the question w/my xmls and the class pretty much exactly as I'm using now. - Dave
Where does this "hello" string comes from? It could just be a bug in App Engine. If the text is in index.html or index.jsp, have you tried just deleting this html/JSP? - JB Nizet

3 Answers

8
votes

The servlet 3.0 spec says:

A string containing only the ’/’ character indicates the "default" servlet of the application.

And it also says:

By default all applications will have index.htm(l) and index.jsp in the list of welcome-file-list. The descriptor may to be used to override these default settings.

So, I guess that the container considers that the implicit index.html welcome file is an exact match, which takes precedence over the default servlet mapped to /.

The solution is to delete the index.html file or, probably, to define an explicit empty welcome file list in the descriptor.

1
votes

Apparently pulling the index.html mapping out of the web.xml isn't enough, you actually have to delete the index.html. If someone can post an answer explaining why I'll still upvote and accept.

0
votes

This post helped me and I think I can expand on why removing the file fixes the problem. According the GAE, any file in the war directory (except JSPs and anything in WEB-INF) gets a mapping to that file name. This implicit mapping appears to supercede any servlet rules in web.xml:

https://developers.google.com/appengine/docs/java/gettingstarted/staticfiles

Once you remove or rename index.html, your servlet rule for "/" is used instead of the implicit mapping for the static file.