这是我的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?

最佳答案

我认为您忘记为控制器创建<bean>了。

<mvc:annotation-driven/>

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

例如。要注册您的控制器:
<bean id="mainController" class="my.package.MainController"/>

另外,您可以启用自动类路径扫描,但是由于速度慢(可能会在您的应用每次冷启动时发生),因此会导致appengine出现性能问题。

要启用类路径扫描,请将上下文名称空间添加到<beans>并添加:
 <context:component-scan base-package="my.package"/>

09-20 19:38