Spring MVC :resources makes annotated controllers not available

标签 spring model-view-controller tomcat spring-mvc

我在 spring 3.1 配置中使用 mvc:resources 时遇到问题。

最初我从事的是在 tomcat 6 上集成 spring 3.0、JPA 的项目。 在 tomcat 6 服务器上,我在 web.xml 中使用以下 servlet 映射从我的应用程序访问静态内容(css、js 和 png 等)。

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/static/*</url-pattern>
</servlet-mapping>

我不确定这是否是最佳实践,但它在 tomcat6 环境中运行良好。它在 tomcat 7 上不起作用。所以我切换到 spring 3.1 以在 applicationContext 中使用 mvc:resources 元素。我更改了 3.1 的 xml 命名空间,并在 applicationContext.xml 中添加了这两行。

<mvc:annotation-driven/>
<mvc:resources location="/resources/" mapping="/static/**"/>

在我添加以上两行配置之前,整个应用程序运行良好。但是现在框架不会检测到所有带注释的 Controller 。我在应用程序根目录中没有 index.jsp 或任何欢迎文件,我将根请求“/”映射到带注释的 RootController.java。所以应用程序主页甚至没有加载。

当我在网上搜索解决方案时,我发现 mvc:annotation-driven 替换了一些我认为由框架隐式定义的默认 bean 配置。我在这个论坛中找到了一些适用于某些人的解决方案,这些解决方案是我自己明确定义一些 bean 配置。 我尝试添加以下两行

<bean class ="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> 
<bean class ="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

但不幸的是,这并没有解决我的问题。我不确定这是否是我现在面临的真正问题,所以这是我的 applicationContext.xml 供您检查我当前的配置。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <context:component-scan base-package="com.yewintko.uog.emailcampaignmanager">
        <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
    </context:component-scan>

    <tx:annotation-driven/>
    <mvc:annotation-driven/>

    <mvc:resources location="/resources/" mapping="/static/**"/>
<bean class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" /> 
<bean class = "org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>   

<context:property-placeholder location="classpath:database.properties"/>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${database.driver}"/>
        <property name="url" value="${database.url}"/>
        <property name="username" value="${database.username}"/>
        <property name="password" value="${database.password}"/>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

    <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="EmailCampaignManager"/>
        <property name="dataSource" ref="dataSource"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="databasePlatform" value="${database.platform}"/>
                <property name="showSql" value="${database.showSql}"/>
                <property name="generateDdl" value="${database.generateDdl}"/>
            </bean>
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
            </props>
        </property>
    </bean>
</beans>

我是 spring 的新手,不知道我的 applicationContext 中缺少哪个配置。如果您有一点空闲时间,请有人帮助我。如果您需要更多信息,请告诉我。

问候 Yewint

最佳答案

Spring 未扫描您带注释的 Controller 是因为您的 applicationContext.xml 文件中有以下行。

<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>

请删除该行,它将解决您的问题。希望这能帮助你欢呼。

关于 Spring MVC :resources makes annotated controllers not available,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11013935/

相关文章:

php - 尝试理解 MVC - 我已经在这么做了吗?

asp.net-mvc-3 - 返回 CSV 文件的 MVC 操作

java - JRebel 无法在外部 tomcat 7 上工作

java - Tomcat 前面的 Apache HTTPD 时 WebSocket 连接中断

tomcat - 带有 Websphere 8.5-java.lang.IllegalStateException 的 Grails 2.X 没有为此应用程序配置工厂

java - 我的 spring boot 应用程序的新版本无法连接到 mysql

java - 使用@Conditional时如何强制Spring首先运行BeanFactoryPostProcessor

java - com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException : Unknown database 'xyz' . hbm2ddl.auto=更新不工作

java - 如何将 Spring bean 注入(inject) DynamoDBMarshaller<T> 的实现中?

javascript - 如何在 ajax 成功调用 location.reload() 后显示隐藏字段值