【问题标题】:spring mvc:resources makes annotated controllers not availablespring mvc:resources 使带注释的控制器不可用
【发布时间】:2012-06-16 08:31:16
【问题描述】:

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

最初我正在研究将 spring 3.0、JPA 集成到 tomcat 6 上的项目。 在 tomcat 6 服务器上,我在 web.xml 中使用了以下 servlet-mapping 从我的应用程序访问静态内容(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/**"/>

在我添加以上两行配置之前,整个应用程序运行良好。但是现在框架没有检测到所有带注释的控制器。我在应用程序根目录中没有 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 中缺少哪个配置。如果你有一点空闲时间,请有人帮助我。如果您需要更多信息,请告诉我。

问候 叶文特

【问题讨论】:

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


    【解决方案1】:

    由于 applicationContext.xml 文件中的以下行,Spring 未扫描您的带注释的控制器。

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

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

    【讨论】:

    • 嗨日本人对不起,我不小心删除了上面的评论。它有效,非常感谢你。顺便说一句,你能解释一下为什么删除那条线可以解决问题吗?
    • 因为它是为了排除一些你想从春季扫描中排除的包或注释类的过滤器。这就是为什么删除它可以解决您的问题。
    【解决方案2】:
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>/static/*</url-pattern>
    </servlet-mapping>
    

    把你的控制器放在http://localhost:8080/{warname}/static/{controller} 但是映射您的资源 servlet 映射到相同的 url。 您可以通过更改任一映射来解决此问题。 因为我假设我希望您的资源 servlet 指向 /static/* 路径,所以您要做的就是将 servlet 更改为 /

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

    那么你的控制器在http://localhost:8080/{warname}/{controller} 您的静态内容位于http://localhost:8080/{warname}/static/

    尝试将 mvc:resources 视为一个控制器,其方法使用 /static/** 注释 它将捕获您尝试对服务执行的所有请求。

    出于生产环境中的性能原因,最好使用常规网络服务器来提供静态内容。

    【讨论】:

    • 嗨,G-Man 感谢您的回复。 Ur answers 是访问静态内容的另一种方法。但是我在使用 3.0.4 中引入的新功能 mvc:resource 时遇到问题。再次感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-17
    • 2010-12-30
    • 2011-12-24
    • 2015-11-15
    • 1970-01-01
    • 2011-12-21
    相关资源
    最近更新 更多