2015-03-03 72 views
1

我试图在谷歌应用引擎上构建一个spring mvc网页。但我无法进行路由/映射工作。谷歌应用引擎中的Spring MVC路由

我有我的HelloController,这表明我希望映射到/hello

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

@Controller 
@RequestMapping("/hello") 
public class HelloController { 

    @RequestMapping(method = RequestMethod.GET) 
    public String Index() { 
     return "hello"; 
    } 
} 

和一个非常简单的hello.jsp文件

<html> 
    <head><title>Hello :: Spring Application</title></head> 
    <body> 
    <h1>Hello - Spring Application</h1> 
    <p>Greetings.</p> 
    </body> 
</html> 

然而,当我尝试访问/hello,我会得到404找不到。

这里是我的XML映射文件mvc-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
     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 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 

    <context:component-scan base-package="com.example.web" /> 
    <mvc:annotation-driven /> 

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

我缺少什么?如何解决它?

+0

你在web.xml中映射的servlet春天? – 2015-03-03 07:43:10

+0

你把hello.jsp放在哪里?我想你应该把它放在'InternalResourceViewResolver'配置中'prefix'指定的目录中 – hsluo 2015-03-03 09:41:06

回答

2

你有web.xml吗?

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/spring/root-context.xml</param-value> 
</context-param> 

<!-- Creates the Spring Container shared by all Servlets and Filters --> 
<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

<!-- Processes application requests --> 
<servlet> 
    <servlet-name>appServlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

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

ContextLoaderListener的关系ApplicationContext的生命周期 ServletContext的生命周期和自动化 的ApplicationContext的创建。 ApplicationContext是Spring beans 的地方,我们可以通过contextConfigLocation 上下文参数提供它的配置。 root-context.xml文件为WebApplicationContext提供配置 的详细信息。

她EIS教程http://www.journaldev.com/2433/spring-mvc-tutorial-for-beginners-with-spring-tool-suite