2012-06-13 22 views
0

我使用MVC有問題的控制器:春季3.1配置資源。Spring MVC的:資源使得註釋不可

最初我正在研究在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/**"/> 

在我添加上述兩行配置之前,整個應用程序運行良好。但是現在所有註釋的控制器都沒有被框架檢測到。我沒有在應用程序根目錄的index.jsp或歡迎任何文件,我映射根請求「/」來註釋RootController.java。所以應用程序主頁甚至沒有加載。

當我到處找解決方案的淨檢查,我發現MVC:註解驅動取代了一些默認的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

回答

0

註釋的控制器都沒有得到由Spring掃描是因爲在你的applicationContext.xml文件以下行的。

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

請刪除該行,它會解決您的問題。希望這可以幫助你歡呼。

+0

當然。我很確定這將解決您的問題。 –

+0

嗨Japs 對不起,我意外刪除上面的評論。它的工作原理和非常感謝。順便說一句你能解釋一下爲什麼解決這個問題? – Max

+0

因爲它是爲了排除某些包或註釋類的過濾器而不想從春季掃描中排除。這就是爲什麼刪除它可以解決你的問題。 –

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

把你的控制器在http://localhost:8080/{warname}/static/{controller} 但是你映射資源的servlet被映射到相同的URL。 您可以通過更改任一映射來解決此問題。 因爲我以爲我想你的資源的servlet指向/靜態/ *路徑,你應該做的是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:資源用作控制器,並使用/ static/** 來表示方法,它將捕獲您嘗試對您的服務執行的所有請求。

在生產環境中的性能的原因,最好使用常規的Web服務器提供靜態內容。

+0

嗨G-Man 感謝您的回覆。 Ur答案是訪問靜態內容的另一種方式。但是我有使用3.0.4中引入的新特性mvc:資源的問題。再次感謝 – Max