I am having problem using mvc:resources in spring 3.1 configuration.
Initially i was working on the project that integrates spring 3.0, JPA, on tomcat 6.
On tomcat 6 server, i used the following servlet-mapping in web.xml to access static contents(css,js and png etc) from my application.
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/static/*</url-pattern>
</servlet-mapping>
I am not sure if it is best practice but it worked fine on tomcat6 environment. It doesnt work on tomcat 7. So i switched to spring 3.1 to use the mvc:resources element in applicationContext. I changed xml namespaces for 3.1 and added the two lines in applicationContext.xml.
<mvc:annotation-driven/>
<mvc:resources location="/resources/" mapping="/static/**"/>
The whole application worked very well before I added the above two lines of configuration. But now all annotated controllers are not detected by the framework. I dont have index.jsp or any welcome files in application root and I mapped root requests “/” to annotated RootController.java. So the appliation home page is not even loaded.
When i check around the net in search of the solutions, i found out that mvc:annotation-driven replaces some default bean configuration that i think is implicitly defined by the framework. I found some solutions that works for some people in this forum which is to explicitly define some bean configuration by myself.
I tried adding the following two lines
<bean class ="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean class ="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
But unfortunately that doesn’t solve my problem. I am not sure if it is the real problem that i am facing now ,so here is my applicationContext.xml for you to check my current configuration.
<?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>
I am very new to spring and have no idea which configuration is missing in my applicationContext. Please someone help me if you have a little free time. If you need more information please let me know.
regards
Yewint
Your annotated controllers are not getting scanned by Spring is because of following line in your applicationContext.xml file.
Please remove that line and it will resolve your issue. Hope this helps you cheers.