Tuesday, August 30, 2011

Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping

ERROR: org.springframework.web.servlet.DispatcherServlet - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mySessionFactory' defined in ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.orm.hibernate3.LocalSessionFactoryBean]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration

Suggestions/Solutions While setting up environment for a project  I started getting the above error. I was using Springsourcetool and despite  all the JAR's in my projects's classpath I was getting this error.  I googled and there were different posts with different suggestions. Most of them suggested adding these JARS to WEB-INF/lib folder

spring-core.JAR(suitable version 3+ in my case)
spring-orm.JAR(suitable version 3+ in my case)
spring-mvc.JAR(suitable version 3+ in my case)

Allthough I was using Spring Source Tool and the abve JARs should not have mattered. So,None of the above worked for me, However carefully comparing the end of the error I found they were different from each other, In my case it says nested exception is java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration

looking at the error it was pretty obvious that i needed my hibernate3.jar . Allthough it was added in my projects class path it had to be added to WEB-INF/lib
I added the
hibernate3.jar to WEB-INF/lib folder and the error was resolved.

hope it would be helpful to someone, If you still get the same error after trying all the above mentioned JARs you need to look at the error carefully and see what might be missing


Thursday, August 11, 2011

Integrating Spring with Velocity

you need to download velocity jar and add it to ur project's classpath and WEB-INF/lib folder

you will need to add below mappings to ur spring servlet

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean name ="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
        <beans:property name="prefix" value="" />
        <beans:property name="suffix" value=".vm" />
   <!--     <beans:property name="exposeSpringMacroHelpers" value="true"/> -->    
    </beans:bean>

        <beans:bean name ="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
        <beans:property name="resourceLoaderPath" value="/WEB-INF/velocity/" />
    </beans:bean>

be careful with below 3 values. These should be according to your project setup.

<beans:property name="prefix" value="" />
        <beans:property name="suffix" value=".vm" />
<beans:property name="resourceLoaderPath" value="/WEB-INF/velocity/" />



you should have a velocity file in your velocity folder, i have a file named home.vm. set up ur web.xml and it should work.

If more detailed explanation is needed, let me know.