2014-02-16 31 views
1

我正在遵循一個簡單的教程來看看我是否可以在GAE上使用Spring MVC。它調用我的控制器類創建一個視圖,但視圖JSP從未找到。Google App Engine簡單的Spring MVC無法找到JSP

這裏是我的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>HelloWeb</servlet-name> 
     <servlet-class> 
      org.springframework.web.servlet.DispatcherServlet 
     </servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>HelloWeb</servlet-name> 
     <url-pattern>*.jsp</url-pattern> 
    </servlet-mapping> 


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

這裏是我的HelloWeb-servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

    <context:component-scan base-package="com.tutorialspoint" /> 

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

</beans> 

,這裏是我的hello.jsp位於WEB-INF/JSP

<html> 
    <head> 
    <title>Hello Spring MVC</title> 
    </head> 
    <body> 
    <h2>${message}</h2> 
    </body> 
</html> 

我去網址

localhost:8888/hello.jsp 

我的控制器在返回視圖「hello」時停在斷點處。我期望它找到/WEB-INF/jsp/hello.jsp 我剛剛在瀏覽器中收到錯誤404。有沒有基本的東西我沒有正確配置?我很感激幫助。

回答

0

我有一個working application,演示Spring MVC在Google App Engine上工作。您可以通過this URL訪問該應用程序。

將我的應用程序與上面共享的數據進行比較,我發現Spring MVC調度程序servlet在我的應用程序中映射爲/,在您的應用程序中映射到*.jsp。通常的約定不是將調度程序servlet映射到文件擴展名。如果我更改我的應用程序配置以將servlet映射到*.jsp,然後請求http://localhost:8080/index.jsp我也會遇到404錯誤,就像您所面對的一樣。

我要說的是,你應該改變你的web.xml,以反映如下:

<servlet-mapping> 
    <servlet-name>HelloWeb</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

然後,接入http://localhost:8888/代替http://localhost:8888/home.jsp因爲MVC的地方,你沒有爲視圖直接問,但只有網址控制器映射到哪個。

相關問題