Spring Controller没有得到扫描

时间:2018-06-07 07:01:48

标签: java spring spring-mvc spring-boot

控制器和项目结构:

我是Spring Development的新手。我正在开发一个Web应用程序。有一些jsp页面我需要显示一些图像,所以我在spring-servlet.xml中创建了一个资源条目,如下所示:

<mvc:resources mapping="/schedules/**" location="/schedules/" />

但是当我添加此条目之后,我的控制器类停止扫描,而不是我的动作检测到404.但是如果从xml中删除上面的条目比我的控制器检测到的正确。但删除图像后,网址未找到丢失的内容。

<context:component-scan base-package="com.spring" />

3 个答案:

答案 0 :(得分:0)

  1. 在Spring servlet-context xml配置中检查mvc XML名称空间是否为xmlns:mvc="http://www.springframework.org/schema/mvc"
  2. <resources>标记是在Spring 3.0.4中引入的,因此至少需要该版本的Spring和xsd。

答案 1 :(得分:0)

  1. 我的建议是使用最新的春季版本。
  2. 在配置文件中添加<mvc:annotation-driven />
  3. 检查资源文件夹行<mvc:resources mapping="/schedules/**" location="/schedules/" />是此文件夹下的所有内容。

答案 2 :(得分:0)

感谢所有人的支持。问题已解决。实际上,xml标签应该声明一个层次结构。

下面的代码适用于我

<context:component-scan base-package="com.spring" />
<mvc:resources mapping="/schedules/**" location="/schedules/" /> 

<mvc:default-servlet-handler />
<mvc:annotation-driven/>

现在我的xml看起来像这样

<?xml version="1.0" encoding="UTF-8"?>

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


    <context:component-scan base-package="com.spring" />
    <mvc:resources mapping="/schedules/**" location="/schedules/" /> 

    <mvc:default-servlet-handler />
    <mvc:annotation-driven/>

    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

    <!-- Register the welcome.properties -->
    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="ApplicationResources" />
    </bean>

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>



</beans>