我使用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
當然。我很確定這將解決您的問題。 –
嗨Japs 對不起,我意外刪除上面的評論。它的工作原理和非常感謝。順便說一句你能解釋一下爲什麼解決這個問題? – Max
因爲它是爲了排除某些包或註釋類的過濾器而不想從春季掃描中排除。這就是爲什麼刪除它可以解決你的問題。 –