Spring AnnotationHandlerMapping无法正常工作

时间:2009-08-28 15:41:41

标签: spring spring-mvc

我是使用带注释控制器的弹簧控制器的新手。

这是我的配置

Bean定义

<bean
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

控制器

package learn.web.controller.annotation;

import javax.servlet.http.HttpServletRequest;

import learn.web.controller.BaseController;

import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class FirstController extends BaseController {

    @RequestMapping("/annotation/first.ftl")
    public ModelAndView first(HttpServletRequest request) {

        if(messageSource  instanceof ReloadableResourceBundleMessageSource){
            ReloadableResourceBundleMessageSource m = (ReloadableResourceBundleMessageSource) messageSource;
            m.clearCache();
        }

        messageSource.getMessage("learn.message.first", new Object[] {},
                localResolver.resolveLocale(request));

        return new ModelAndView("/annotation/first");
    }

}

当试图访问给定的URL时,Spring正在抛出警告org.springframework.web.servlet.PageNotFound - 在DispatcherServlet中找不到带有URI [/Learn/annotation/first.ftl]的HTTP请求的映射,名称为'springapp'

1 个答案:

答案 0 :(得分:0)

我认为您缺少的是组件扫描

<context:component-scan base-package="learn.web.controller" />

将此添加到您的配置中并尝试。

这将加载指定包中的所有带注释的组件

您的配置可能如下所示

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

    <context:component-scan base-package="learn.web.controller" />
    <bean
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

</beans>