Spring MVC Annotations

Question!

I've been over Spring's documentation a couple times, but I don't seem to be able to get @Controller, etc annotations to work.

I am loading the dependencies and repositories in my POM (... are my specific values):

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>...</groupId>
  <artifactId>...</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>...</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <org.springframework.version>3.0.5.RELEASE</org.springframework.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>org.springframework.context</artifactId>
      <version>${org.springframework.version}</version>
      <scope>runtime</scope>
   </dependency>
   <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>org.springframework.web.servlet</artifactId>
      <version>${org.springframework.version}</version>
      <scope>runtime</scope>
   </dependency>
  </dependencies>

  <repositories>
    <repository>
        <id>com.springsource.repository.bundles.release</id>
        <url>http://repository.springsource.com/maven/bundles/release/</url>
    </repository>
    <repository>
        <id>com.springsource.repository.bundles.external</id>
        <url>http://respoitory.springsource.com/maven/bundles/external</url>
    </repository>
  </repositories>
</project>

In my web.xml, I am setting up the dispatcher servlet:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
         version="2.4">

    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

</web-app>

And under WEB-INF I have servlet-context.xml (... is my controller package):

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc 
                        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
                        http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                        http://www.springframework.org/schema/context 
                        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="..." />

    <mvc:annotation-driven />

    <!-- Adds prefix and suffix to returned views -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

I think I must be missing or forgetting something simple. Eclipse doesn't recognize the annotations and maven fails to build the project. Can anyone help me out here?

EDIT

For further clarification, in eclipse, adding the annotation results in:

The import org.springframework cannot be resolved

And running mvn clean install results in:

package org.springframework.stereotype does not exist

And removing the runtime scope results in the following Maven error:

error reading ...\.m2\repository\org\aopalliance\com.springsource.org.aopalliance\1.0.0\com.springsource.org.aopalliance-1.9.9.jar; error in opening zip file


Answers

Be sure your eclipse cached version of the repo index is up to date, you can even manually update the index by using maven repo view and update it from there.

I always try the vanilla version (command line) of my maven builds, if I get an error from some IDE.

By : Loki


use below and try, annotation handler should be used as below in your application context .xml

By : kapil das


I found the answer to my question in a later post.

Basically, I needed to add the m2eclipse plugin to Eclipse. You can find instructions at http://m2eclipse.sonatype.org/installing-m2eclipse.html if needed. Once this is installed, you can right click on your project in Eclipse and choose Maven > Enable Dependency Management. Then eclipse will be able to recognize imports from your POM dependencies.

So in the end, I had the following. Eclipse recognized everything, Maven correct built and packaged the project, and Tomcat deployed the project successfully.

Here is my web.xml hooking up the dispatcher servlet:



This video can help you solving your question :)
By: admin