1

Im trying to create my first Spring MVC project but I keep getting error http status 404.

If i try to open an .jsp file outside of /views (So not using Spring MVC) it works fine

Here are my files:

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-tarefas</display-name>

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

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

</web-app>

spring-context.xml

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


    <context:component-scan base-package="br.com.caelum.tarefas" />   
    <mvc:annotation-driven />

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

</beans>

Controller

package br.com.caelum.tarefas.controller;

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

@Controller
public class OlaMundoController {

    @RequestMapping("/olaMundoSpring")
    public String execute(){
        System.out.println("Executando a lógica com Spring MVC");
        return "ok";
    }

}

Libs

aopalliance-1.0.jar
commons-logging-1.2.jar
javax.servlet.jsp.jstl-1.2.1.jar
javax.servlet.jsp.jstl-api-1.2.1-3.jar
jcl-over-slf4j-1.7.12.jar
joda-time-2.4.jar
log4j-over-slf4j-1.7.12.jar
slf4j-api-1.7.12.jar
slf4j-log4j12-1.7.12.jar
spring-aop-4.1.7.RELEASE.jar
spring-aspects-4.1.7.RELEASE.jar
spring-beans-4.1.7.RELEASE.jar
spring-context-4.1.7.RELEASE.jar
spring-context-support-4.1.7.RELEASE.jar
spring-core-4.1.7.RELEASE.jar
spring-expression-4.1.7.RELEASE.jar
spring-instrument-4.1.7.RELEASE.jar
spring-instrument-tomcat-4.1.7.RELEASE.jar
spring-jdbc-4.1.7.RELEASE.jar
spring-jms-4.1.7.RELEASE.jar
spring-messaging-4.1.7.RELEASE.jar
spring-orm-4.1.7.RELEASE.jar
spring-oxm-4.1.7.RELEASE.jar
spring-test-4.1.7.RELEASE.jar
spring-tx-4.1.7.RELEASE.jar
spring-web-4.1.7.RELEASE.jar
spring-webmvc-4.1.7.RELEASE.jar
spring-webmvc-portlet-4.1.7.RELEASE.jar
spring-websocket-4.1.7.RELEASE.jar
2
  • 1
    what context are you trying to execute /olaMundoSpring ? Do you have ok.jsp in your views catalog since you are returning ok in your execute method ? Commented Aug 11, 2015 at 22:45
  • Is "Executando a lógica com Spring MVC" printing in console Commented Aug 12, 2015 at 6:04

3 Answers 3

2

When your controller method returns a String, the String to be returned is the name of your view (jsp, template, etc).

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

According to the setup above (provided by you), Spring in intended to return a view located on /WEB-INF/views and the name of the view must has the suffix ".jsp".

Please, make sure the file "ok.jsp" is located inside this path, otherwise, Spring will not be able to load it.

In case "ok.jsp" is already located in this folder, please, provide us full stacktrace of the exception you are facing.

Sign up to request clarification or add additional context in comments.

Comments

0

Yes, the view "ok.jsp" is located on the corrected folder.

I ended up fixing the issue by deleting all the libs and adding the dependency using maven.

Comments

0

If you are using spring, you should us spring boot to get started with any new service. Takes care of your basic dep tree. Refer to the tutorial at - http://projects.spring.io/spring-boot/#quick-start

Pretty straight forward.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.