Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC implementation of WebApplicationInitializer

Tags:

spring-mvc

I have already learned how to configure Spring MVC app with XML so I decided to go ahead.

I read documentation about WebApplicationInitializer and minimizing of XML in application configuration. But when I completed all preparations of the sample application I encountered with 404 page.

Further I put snippets of my code, please give me advices how to make @-based approach properly.

Config file:

package com.onet.init;

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.UrlBasedViewResolver;

@Configuration
@ComponentScan("com.onet")
@EnableWebMvc
public class BaseConfig {

    @Bean
    public UrlBasedViewResolver setupViewResolver() {
        UrlBasedViewResolver resolver = new UrlBasedViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
    }

}

Initializer:

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 Initializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(BaseConfig.class);

        Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
        servlet.addMapping("*.html");
        servlet.setLoadOnStartup(1);
    }

}

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/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>oneTest</groupId>
    <artifactId>oneTest</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <packaging>war</packaging>

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

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib-nodep</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>

</project>

Controller:

package com.onet.controller;

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

@Controller
public class HelloController {

    @RequestMapping(value="/hello")
    public ModelAndView goToHelloWorld() {
        return new ModelAndView("hello-world");
    }

}

index.jsp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Home page</title>
</head>
<body>
<h1>Home page</h1>
<p>This is a home page.</p>
<p><a href="hello.html">Say Hello</a></p>
</body>
</html>

So when I click on "Say Hello" link I get 404. Entire project you can download from my drop-box.

like image 604
Alex Fruzenshtein Avatar asked Dec 11 '25 07:12

Alex Fruzenshtein


1 Answers

i just checked the project in your dropbox. it seems to me, the structure of the project is wrong. you mixed maven-scructure with eclipse-structure. When you use maven, you put the webcontent in src/main/webapp... not in WebContent like you did. you can take a look here for more details on this topic.

short version:

move the files from WebContent to src/main/webapp and try again.

long version:

if you run mvn package and extract the resulting *.war from /target directory, you will see it lacks the files from WebContent directory. Maven expect those files to be in src/main/webapp. I assume you started by creating a "Dynamic Web Project" in eclipse. Eclipse expect the resources like *.jsp and co. to be located in WebContent, this is why calling index.jsp work. But when it comes to spring it failes, because hello-world.jsp is not located where it should be.

how to fix:

Start with moving the files from WebContent to src/main/webapp. then run mvn eclipse:eclipse -Dwtpversion=2.0. it will generate the configuration for eclipse (.classpath, .project, etc.). Refresh the project in Eclipse. Now it should work.

like image 50
Yevgeniy Avatar answered Dec 14 '25 00:12

Yevgeniy