1

I am working on a Spring 4.0.1 mvc application and my browser cannot render any resource of my app.

I am configuring my application via annotations, rather than xml files. This is why I am asking this question and not reusing any already given answer. Searching for my problem only led me to people who don't use annotations. I did follow this tutorial. Maven is my build tool and Eclipse kepler my IDE.

Can anyone please help me? I would appreciate any tips and resources.

Here is my system setup:

  • I try to reach the webapp on http://localhost:8080/Spring4MVCHelloWorld/hello.
  • The tomcat catalina log does not show any error.
  • mvn clean package are my executed maven goals.
  • Here you can find the project on GitHub: https://github.com/Husterknupp/spring-octo-lana

In This is the tomcat error report:

HTTP Status 404 - /Spring4MVCHelloWorld/

type Status report
message /Spring4MVCHelloWorld/
description The requested resource is not available.

This is my pom.xml:

<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>de.bschandera.jeetutorials</groupId>
    <artifactId>Spring4MVCHelloWorld</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>Spring4MVCHelloWorld Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <properties>
        <spring.version>4.0.1.RELEASE</spring.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <!-- Spring dependencies START -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- Spring dependencies END -->

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
                    <scope>provided</scope> <!-- provided because servlet api jar file must not be embedded inside the 
            webapp since, obviously, the container already has these classes in its classpath: 
            it implements the interfaces contained in this jar -->
        </dependency>

    </dependencies>

    <build>
        <finalName>Spring4MVCHelloWorld</finalName>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Under src/main/java there are these three classes:

  1. Config.java:

    package de.bschandera.jeetutorials.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.view.JstlView;
    import org.springframework.web.servlet.view.UrlBasedViewResolver;
    
    @Configuration
    // Marks this class as configuration
    // Specifies which package to scan
    @ComponentScan("de.bschandera.jeetutorials.config")
    // Enables Spring's annotations
    @EnableWebMvc
    public class Config {
        @Bean
        public UrlBasedViewResolver setupViewResolver() {
        UrlBasedViewResolver resolver = new UrlBasedViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
        }
    }
    
  2. HelloWorldController.java:

    package de.bschandera.jeetutorials.config;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    
    @Controller
    public class HelloWorldController {
    
        @RequestMapping("/hello")
        public String hello(@RequestParam(value = "name", required = false, defaultValue = "World") String name, Model model) {
        model.addAttribute("name", name);
        return "helloworld";
        }
    
    }
    
  3. WebInitializer.java:

    package de.bschandera.jeetutorials.config;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRegistration.Dynamic;
    
    import org.springframework.web.WebApplicationInitializer;
    import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
    import org.springframework.web.servlet.DispatcherServlet;
    
    public class WebInitializer implements WebApplicationInitializer {
    
        public void onStartup(ServletContext servletContext) throws ServletException {
    
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(Config.class);
    
        ctx.setServletContext(servletContext);
    
        Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
        servlet.addMapping("/");
        servlet.setLoadOnStartup(1);
    
        }
    
    }
    

Under src/main/webapp/WEB-INF/views there is helloworld.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Spring4 MVC -HelloWorld</title>
  </head>
  <body>
    <h1>Hello : ${name}</h1>
  </body>
</html>

Under src/main/webapp/WEB-INF there is the index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Spring4 MVC -HelloWorld INDEX</title>
  </head>
  <body>
    <h1>Index page - welcome!</h1>
  </body>
</html>

After having the first three answers not solving the problem, I would imagine that the problem has to do with any tomcat configuration or anything else but the code itself. But I dont know where to look for asides the suggested solutions.

5
  • I just imported your project, added the Jetty plugin and ran it. It worked first time!! How are you deploying your app? Have you checked your tomcat manager page to see if its deployed?
    – TedTrippin
    May 2, 2014 at 9:56
  • Now, I have problems with the Jetty plugin ;-) mvn jetty:run stops with BUILD ERROR. After the maven log there are two additional logger infos: Shutdown hook executing and Shutdown hook complete. What should I do with it? I added the Jetty plugin by extending the pom.xml. I simply added a new plugin node without any configuration. Do I need to configure the Jetty plugin in the pom.xml? May 2, 2014 at 10:47
  • I just added the plugin and ran it (with jetty:run) from within eclipse. I didnt do anything else! <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.1.2.v20140210</version> </plugin>
    – TedTrippin
    May 2, 2014 at 12:51
  • @TedTrippin I finally run jetty:run from within eclipse, which led to this [ERROR] Failed to execute goal org.eclipse.jetty:jetty-maven-plugin:9.1.2.v20140210:run (default-cli) on project Spring4MVCHelloWorld: Execution default-cli of goal org.eclipse.jetty:jetty-maven-plugin:9.1.2.v20140210:run failed: Unable to load the mojo 'run' in the plugin 'org.eclipse.jetty:jetty-maven-plugin:9.1.2.v20140210' due to an API incompatibility: org.codehaus.plexus.component.repository.exception.ComponentLookupException: org/eclipse/jetty/maven/plugin/JettyRunMojo : Unsupported major.minor version 51.0 May 7, 2014 at 13:19
  • Sounds like a java version problem. I was using java 7.
    – TedTrippin
    May 7, 2014 at 19:34

3 Answers 3

0

Unless there's something you aren't showing us, you have no welcome-file and no handler for /.

Assuming /Spring4MVCHelloWorld is your context path, there is nothing in your configuration to handle a request to

http://localhost:8080/Spring4MVCHelloWorld/

Perhaps you meant to send the request to

http://localhost:8080/Spring4MVCHelloWorld/hello

which can be handled by your HelloWorldController.

8
  • I have no more files left to show. Only those in the target folder. But those are not relevant I think, as maven creates these files dependent on those shown above. I have no handler for /. Your assumption is right. I am only requesting http://localhost:8080/Spring4MVCHelloWorld/hello. Do I need one anyway? May 1, 2014 at 22:15
  • @Huster If you're going to send a request to that URL, of course you need something to handle it. May 1, 2014 at 22:23
  • I need a handler for / when I'm going to send a request to http://localhost:8080/Spring4MVCHelloWorld/ and one (like I have already) for /hello when I'm going to send a request to http://localhost:8080/Spring4MVCHelloWorld/hello. Did I get you right? May 1, 2014 at 22:33
  • @Huster You'll need a handler for every URL for which you want a response. Look into welcome files. May 1, 2014 at 22:35
  • @Husterknupp So? Is http://localhost:8080/Spring4MVCHelloWorld/hello handled properly? Did you add a handler for /? May 2, 2014 at 5:55
0

Your class should extend WebMvcConfigurerAdapter class, see how your class should look like now

@Configuration
// Marks this class as configuration
// Specifies which package to scan
@ComponentScan("de.bschandera.jeetutorials.config")
// Enables Spring's annotations
@EnableWebMvc
public class Config extends WebMvcConfigurerAdapter {
    @Bean
    public UrlBasedViewResolver setupViewResolver() {
    UrlBasedViewResolver resolver = new UrlBasedViewResolver();
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".jsp");
    resolver.setViewClass(JstlView.class);
    return resolver;
    }
}
1
  • Your suggestion doesn't solve my problem either :-/ But thanks for your answer. May 2, 2014 at 0:11
0

I checked out your code and deployed it to Tomcat 7.0.27 (using Java 7) with only one change, and everything worked correctly.

The change I made was to add the jstl dependency to the pom.

    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
2
  • I added the dependency, but still having the same problem. Probably the problem has something to do with my Tomcat configuration or anything else but the project itself.? But what else can I check? I will follow TedTrippins suggestion with the Jetty plugin for maven. May 2, 2014 at 10:21
  • I can't really think about what else you should check.
    – geoand
    May 2, 2014 at 10:29

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.