java - Spring MVC 配置了静态资源后 @Controller 扫描失效
怪我咯
怪我咯 2017-04-17 17:19:37
[Java讨论组]

Spring MVC 配置了静态资源后 @Controller 扫描失效

springMVC-servet.xml中加
<mvc:resources location="/img/" mapping="/img/**" cache-period="604800"/>后导致 @Controller失效,访问都是 404

springMVC-servet.xml代码:


    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
        <context:annotation-config/>
    
        <context:component-scan base-package="com.junbaor.oauth.controller">
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        </context:component-scan>
    
    <!--<mvc:resources location="/img/" mapping="/img/**" cache-period="604800"/> -->
    
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="contentType" value="text/html"/>
            <property name="prefix" value="/WEB-INF/views/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    
    </beans>

applicationContext-Beans.xml代码:


    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns="http://www.springframework.org/schema/beans" xmlns:util="http://www.springframework.org/schema/util"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
        
        <context:annotation-config/>
    
        <context:component-scan base-package="com.junbaor.oauth">
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        </context:component-scan>
    
        <util:properties id="config" location="classpath:config.properties"/>
    
    </beans>

项目结构:

怪我咯
怪我咯

走同样的路,发现不同的人生

全部回复(1)
天蓬老师

springMVC-servet.xml 文件中的 <context:annotation-config/> 换成 <mvc:annotation-driven/> 后解决问题。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号