Spring 3和Google App引擎的问题

时间:2011-03-05 05:31:22

标签: java spring google-app-engine

这是我的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>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

这是我的dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    <!-- JSR-303 support will be detected on classpath and enabled automatically -->
    <mvc:annotation-driven/>
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

我有一个带注释的Controller类:

@Controller
public class MainController {
    @RequestMapping("/test")
    public ModelAndView testHandler(){
          ModelAndView mav = new ModelAndView();
            mav.setViewName("testView");
            mav.addObject("message", "test");
            return mav;
    }
}

项目编译没有错误,但是当我运行它并点击http://.aggengine.com//test我得到404.我的映射是错误的还是我的URI?

1 个答案:

答案 0 :(得分:3)

我想你忘记为你的控制器创建一个<bean>

<mvc:annotation-driven/>

只需启用支持,即在创建bean时读取bean的注释。

E.g。要注册您的控制器:

<bean id="mainController" class="my.package.MainController"/>

或者您可以启用自动类路径扫描,但这会导致appengine的性能问题,因为它很慢(并且每次应用程序冷启动时都会发生)

要启用类路径扫描,请将上下文命名空间添加到<beans>并添加:

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