Spring MVC - @Controller注释不会被自动检测

时间:2013-06-07 10:07:22

标签: spring spring-mvc web.xml spring-annotations

我春天很新,所以也许我的问题很愚蠢!我试图在Spring的Petclinic示例中添加一个控制器。但是,此控制器中的映射不起作用,我得到404错误。我添加了相同的映射到其他现有的控制器,它的工作原理。我想看看我做错了什么。这是我的控制器:

package org.springframework.samples.petclinic.web;

import java.util.Map;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.stereotype.Controller;

@Controller
public class TwitterController {

    @RequestMapping(value = "/twitter/searchtweets", method = RequestMethod.GET)
        public String initFindForm(Map<String, Object> model) {
        ...
        return "twitter/searchtweets";
    }
}

这是我的MVC配置文件中的组件扫描定义:

<context:component-scan
        base-package="org.springframework.samples.petclinic.web"/>

    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="contentNegotiationManager" ref="cnManager"/>

    <property name="viewResolvers">
        <list>
             <!-- Default viewClass: JSTL view (JSP with html output) -->
            <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <!-- Example: a logical view name of 'vets' is mapped to '/WEB-INF/jsp/vets.jsp' -->
                <property name="prefix" value="/WEB-INF/jsp/"/>
                <property name="suffix" value=".jsp"/>
            </bean>

            <!-- Used here for 'xml' and 'atom' views  -->
            <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
        </list>
    </property>
</bean>

<!-- Simple strategy: only path extension is taken into account -->
<bean id="cnManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
    <property name="favorPathExtension" value="true"/>
    <property name="ignoreAcceptHeader" value="true"/>
    <property name="defaultContentType" value="text/html"/>

    <property name="mediaTypes">
        <map>
            <entry key="html" value="text/html" />
            <entry key="xml" value="application/xml" />
            <entry key="atom" value="application/atom+xml" />
        </map>
    </property>
</bean>

地址:localhost:8081/petclinic/twitter/searchtweets.html返回404错误。值得一提的是这里的jsp文件:webapp/WEB_INF/jsp/twitter/searchtweets.jsp

我错过了什么吗?

0 个答案:

没有答案