【问题标题】:Spring AnnotationHandlerMapping not workingSpring AnnotationHandlerMapping 不起作用
【发布时间】:2010-11-23 18:51:30
【问题描述】:

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

这是我的配置

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”

【问题讨论】:

    标签: spring spring-mvc


    【解决方案1】:

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

    <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>
    

    【讨论】:

      猜你喜欢
      • 2012-01-02
      • 2017-04-22
      • 2013-11-22
      • 2012-01-07
      • 2012-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多