【问题标题】:Spring Controller not getting Scan弹簧控制器没有得到扫描
【发布时间】:2018-06-07 07:01:48
【问题描述】:

控制器和项目结构:

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

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

但是当我添加这个条目之后,我的控制器类停止扫描并且我的动作没有检测到 404。但是如果我从 xml 中删除上述条目而不是我的控制器正确检测到。但在删除图片 url 后找不到缺少的内容。

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

【问题讨论】:

  • base-package 应该指向 yournot springs
  • 其实我的包名是spring
  • 显示你的控制器代码和项目结构
  • 你可以试试这个:

标签: java spring spring-mvc spring-boot


【解决方案1】:
  1. 我的建议是使用最新的 spring 版本。
  2. 在您的配置文件中添加&lt;mvc:annotation-driven /&gt;
  3. 检查资源文件夹行&lt;mvc:resources mapping="/schedules/**" location="/schedules/" /&gt; 是该文件夹下的所有内容。

【讨论】:

  • 我也试过这也是 。但没有产生任何差异。让我也将 spring 切换到最新版本。并根据时间表出现了一些自动生成的图像。没有别的
  • 请分享你的web.xml,控制器类(当前图片没有给出关于类上使用的注释的任何线索),资源路径图片
【解决方案2】:
  1. 在 Spring servlet-context xml 配置中检查 mvc XML 命名空间是否位于 xmlns:mvc="http://www.springframework.org/schema/mvc"
  2. &lt;resources&gt; 标签是在 Spring 3.0.4 中引入的,因此您至少需要该版本的 Spring 和 xsd。

【讨论】:

  • 这是我的春季会议
【解决方案3】:

感谢大家的支持。问题得到解决。实际上有一个层次结构级别应该声明 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>  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-23
    • 1970-01-01
    • 2014-10-28
    • 2022-01-22
    • 1970-01-01
    • 2012-09-07
    • 2014-12-14
    相关资源
    最近更新 更多