开发者社区> 问答> 正文

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>

项目结构:
screenshot

展开
收起
蛮大人123 2016-03-11 15:14:55 2768 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

    springMVC-servet.xml 文件中的 <context:annotation-config/> 换成 <mvc:annotation-driven/>

    2019-07-17 18:59:22
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载

相关实验场景

更多