0

I've been checking several tutorials and other helps here in stackoverflow, but I'm unable to solve this annoying problem: I created a Spring MVC project in Eclipse following this tutorial from step to step, however I still get a Resource not found error from the Tomcat server.

WEB-INF/web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>onlab</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>onlab</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

WEB-INF/onlab-servlet.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    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="controller" />

    <mvc:annotation-driven />

</beans>

src/controller/HomeControllerJava:

@Controller
public class HomeController {

    @RequestMapping(value = "/")
    public String home() {
        System.out.println("HomeController: Passing through...");
        return "home";
    }
}

WEB-INF/views/home.jsp:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
    <head>
        <title>Home</title>
    </head>
    <body>
        <h1>Hello world!</h1>
    </body>
</html>

and finally src/controller/Configurer.java:

@Configuration
public class Configurer {
    @Bean
    ViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
    }

Last time this same tutorial worked well for me, but now I just keep getting Resource not found from the Apache.

7
  • What URL are you sending the request to? Commented Apr 23, 2014 at 15:49
  • Is this a mistake : or did you call your folder WEB-ING/ ? it may solve your issue to rename it WEB-INF Commented Apr 23, 2014 at 15:56
  • The whole stack trace would be nice Commented Apr 23, 2014 at 16:05
  • I am trying "localhost:8080/onlab" and that is WEB-INF, sorry. Commented Apr 23, 2014 at 16:40
  • Your example works for me. How did you deploy your app? Commented Apr 23, 2014 at 16:57

2 Answers 2

1

Create a java class MyWebAppInitializer.java and in this file add your Configurer.java Then Test

public class MyWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class<?>[] getServletConfigClasses() {
    return new Class[] { Configurer.class };
}

@Override
protected String[] getServletMappings() {
    return new String[] { "/" };
}

@Override
protected Class<?>[] getRootConfigClasses() {
    return null;
}

}

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

Comments

0

Since i can't add comment I'm going to go for the answer :).

Not knowing your Tomcat/IDE setup this is my assumption and how i derived the final conclusion: -Your code is not returning errors -The IDE is building and deploying the files in correct location -You have Tomcat setup properly

Since you are running in Eclipse (if you using that you might as well go with Spring Tools Suite since its specifically designed for Spring) you should the configuration of the Tomcat server. Previous experience for me was that mapping in Tomcat (which is also in http.conf file) was using different prefix then what i expect due to other conflicting projects.

In this case while you are assuming its localhost:8080/onlab/ it may be configured to be looking for the same file at localhost:8080/ or even localhost:8080//onlab/.

Check out your tomcat config and make sure its pointing to right location.

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.