0

I'm having a lot of trouble trying to use Spring Framework and a don't know where is the error. I'm deploying the application using Tomcat 7.0.68.

When I try to use the URL (http://localhost:8080/fj21-spring/olaMundo) I'm getting a 404 error, but its should be mapped to call my Action method on my Controller class, as below:

package com.caelum.tarefas.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TarefasController {

  @RequestMapping("/olaMundo")
  public String execute(){
      return "ok";
  }
}

My pom.xml file is what follows:

<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>com.caelum.tarefas</groupId>
  <artifactId>fj21-spring</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>fj21-spring</name>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  
   <dependencies>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
   <version>4.2.5.RELEASE</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-aop</artifactId>
   <version>4.2.5.RELEASE</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>4.2.5.RELEASE</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-web</artifactId>
   <version>4.2.5.RELEASE</version>
  </dependency>
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
   <version>1.2</version>
  </dependency> 
  <dependency>
   <groupId>commons-logging</groupId>
   <artifactId>commons-logging</artifactId>
   <version>1.1.3</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-jdbc</artifactId>
   <version>4.2.5.RELEASE</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-aspects</artifactId>
   <version>4.2.5.RELEASE</version>
  </dependency>
  <dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-api</artifactId>
   <version>1.7.19</version>
  </dependency>   
  <dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-log4j12</artifactId>
   <version>1.7.19</version>
  </dependency> 
 </dependencies>
</project>

And my web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>fj21-spring</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
   <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
  
</web-app>

My Spring XML configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">
 
 <context:component-scan base-package="com.caelum.tarefas" />
  <mvc:annotation-driven></mvc:annotation-driven>
  
 <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
  <property name="prefix" value="/WEB-INF/views/" />
  <property name="suffix" value=".jsp" />
 </bean>
</beans>

I expect that someone could help me. I had tryed to recreated the Server in Eclipse, deleted the project and create another one, but until now nothing worked.

Thank you!

EDIT

After change my annotation on Controller class to @RequestMapping(value="/olaMundo"), the Tomcat write a stack trace on console, which follows (maybe I missed this stack trace before too):

GRAVE: Servlet [spring] in web application [/fj21-spring] threw load() exception
java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1858)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1701)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:504)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:486)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:113)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1146)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1085)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5318)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5610)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1572)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1562)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caio Ferreira
  • 304
  • 2
  • 9
  • Please don't post a link to the error, instead add the error content directly to your question. – pczeus Mar 27 '16 at 21:24
  • Maybe you should include information on how you are attempting to access the controller method. What is the full URL you are hitting? How have you deployed your application? It looks to be on Tomcat. Is this a war? Exploded war? what is your war name? – pczeus Mar 27 '16 at 21:26
  • Edit your question and copy the stack trace that your Tomcat is generating in the Eclipse's tab "Console". 404 error does not explain anything, you can remove the image. Just copy the URL and write down that it generates 404. – RubioRic Mar 27 '16 at 21:27
  • I've added the url that I'm trying to hit and yes, I'm using Tomcat 7.0.68. I don't have sure what is a war in Tomcat, but for what I can see in the publish folders of my Tomcat's installation, it's not. – Caio Ferreira Mar 27 '16 at 21:57
  • @RubioRic there isn't any stack trace on my console that doesn't regard the deploying and iniatilization of the Tomcat server. There isn't error message on console or other information about the 404 error. – Caio Ferreira Mar 27 '16 at 22:01
  • If you are using Eclipse, select your project, right-click, Run As, Run On Server, Select your server, etc ... That's how you deploy your app to the Tomcat server. You have to do that before trying the URL. When the Tomcat is started, it is gonna write in the console. – RubioRic Mar 27 '16 at 22:09
  • I try to deploy as you said but the result wasn't different. Have a look at the stack trace that I put above. – Caio Ferreira Mar 27 '16 at 22:27
  • @CaioFerreira You have obtained the exception. That's an improvement. Look for what may cause that specific exception. Maybe this helps http://stackoverflow.com/questions/11227395/java-lang-classnotfoundexception-org-springframework-web-servlet-dispatcherserv – RubioRic Mar 27 '16 at 23:51
  • 1
    UHUL! It worked!! When I found the exception I started to look for posts about that and find some, but none had worked until that! Thank you a lot man! I'm a big fan of MVC and DDD but I'm migrating for C# to Java and don't much yet! Thank all you guys! – Caio Ferreira Mar 28 '16 at 00:01

3 Answers3

1

It looks like the application your deployed can't find the spring libs enter link description here

may this question can help you

Community
  • 1
  • 1
eric zhao
  • 165
  • 3
  • 15
0

Is the jsp you are trying to display called "ok.jsp"? If not this is what your controller is looking for in your views folder when it returns "ok".

Try replacing your @RequestMapping("/olaMundo") with @RequestMapping(value="/olaMundo")

Ian
  • 30,182
  • 19
  • 69
  • 107
hamo
  • 474
  • 1
  • 6
  • 16
  • 1
    Yes, I have a "ok.jsp" file in my views folder. – Caio Ferreira Mar 27 '16 at 21:54
  • 1
    I don't downvoted you. If you see, someone downvoted my question too. I even try to "upvoted" you, but I'm new to StackOverflow and don't enough reputation to my votes made changes which others can see. – Caio Ferreira Mar 27 '16 at 23:00
0

I see ClassNotFoundException. check the jar file in lib dir and check the configuration like this. properties --> deployment assembly enter image description here

Jason Christ
  • 125
  • 7