1

I created a Maven web projectProject

added maven dependencies through porm.xml created servelet-config.xml in folder WEB-INF/config using Spring Bean configuration Xml file created a folder src\main\java and in that folder created a class HelloController. Now when I use @Controller annotation in my HelloController class, it can not be resolved. I dont know when I use in my servelet-config.xml it says:

Configures the annotation-driven Spring MVC Controller programming model. Note that this tag works in Web MVC only, not in Portlet MVC!

I am following....https://www.youtube.com/watch?v=9MdnvleI6-8

My created web.xml is:

`<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
     
  
  <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
 <servlet>
  <servlet-name>springDispatcherServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/config/servlet-config.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>

 <!-- Map all requests to the DispatcherServlet for handling -->
 <servlet-mapping>
  <servlet-name>springDispatcherServlet</servlet-name>
  <url-pattern>*jsp</url-pattern>
 </servlet-mapping>
 
</web-app>
`

my porm.xml is

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.sat</groupId>
  <artifactId>SpringMVC5</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>SpringMVC5 Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
     <groupId>springframework</groupId>
     <artifactId>spring-beans</artifactId>
     <version>1.2.6</version>
    </dependency>
    <dependency>
     <groupId>springframework</groupId>
     <artifactId>spring-webmvc</artifactId>
     <version>1.2.6</version>
    </dependency>
    <dependency>
     <groupId>springframework</groupId>
     <artifactId>spring-core</artifactId>
     <version>1.2.6</version>
    </dependency>
    <dependency>
     <groupId>springframework</groupId>
     <artifactId>spring-context</artifactId>
     <version>1.2.6</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>SpringMVC5</finalName>
  </build>
</project>

my servlet-config is:

<?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:jee="http://www.springframework.org/schema/jee"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
  http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
  http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">


<mvc:annotation-driven/>
<context:component-scan base-package="com.sat.controllers"></context:component-scan>



</beans>

my controller class is:

package com.sat.controllers;

import javax.annotation.Resource;

import org.springframework.web.servlet.config.annotation.EnableWebMvc;



@EnableWebMvc
@Controller

public class HelloController {
 
 

}

What I am doing wrong? Please help...

javaz
  • 81
  • 2
  • 4
  • 14

2 Answers2

1

Your pom.xml is specifying Spring 1.2.x - that's an extremely old version, and certainly doesn't support any sort of annotations - where did you get that POM fragment?

Spring is up to 4.1.x, you should be using that if you want to learn.

David V
  • 11,531
  • 5
  • 42
  • 66
skaffman
  • 398,947
  • 96
  • 818
  • 769
  • 1
    Exactly, spring started annotation support from 2.5 on wards. – Bala Apr 30 '15 at 02:07
  • @skaffman, can you please answer to my post -- http://stackoverflow.com/questions/30357979/when-to-use-request-session-application-scope-in-spring – learner May 21 '15 at 16:19
0

Your spring version is very old. Explore the latest feature from Spring Framework - Maven BOM. It will import all dependecies (direct & transitive) into your project and save you the headache.

Ujjwal
  • 603
  • 12
  • 23