Example Spring MVC Hello World – Annotation Config

 

1. Project Structure

 

2. File 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>com.codewr</groupId>

    <artifactId>spring-mvc-maven-annotation-hello-world</artifactId>

    <packaging>war</packaging>

    <version>1.0-SNAPSHOT</version>

    <name>SpringMVC Hello World-Annotation</name>

 

    <properties>

        <jdk.version>1.6</jdk.version>

        <spring.version>3.2.13.RELEASE</spring.version>

        <jstl.version>1.2</jstl.version>

        <servletapi.version>3.1.0</servletapi.version>

    </properties>

 

    <dependencies>

 

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-webmvc</artifactId>

            <version>${spring.version}</version>

        </dependency>

 

        <dependency>

            <groupId>javax.servlet</groupId>

            <artifactId>jstl</artifactId>

            <version>${jstl.version}</version>

        </dependency>

 

        <!-- compile only, deployed container will provide this -->

        <dependency>

            <groupId>javax.servlet</groupId>

            <artifactId>javax.servlet-api</artifactId>

            <version>${servletapi.version}</version>

            <scope>provided</scope>

        </dependency>

 

    </dependencies>

 

    <build>

 

        <plugins>

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-compiler-plugin</artifactId>

                <version>3.3</version>

                <configuration>

                    <source>${jdk.version}</source>

                    <target>${jdk.version}</target>

                </configuration>

            </plugin>

 

            <plugin>

                <groupId>org.eclipse.jetty</groupId>

                <artifactId>jetty-maven-plugin</artifactId>

                <version>9.2.11.v20150529</version>

                <configuration>

                    <scanIntervalSeconds>10</scanIntervalSeconds>

                    <webApp>

                        <contextPath>/spring</contextPath>

                    </webApp>

                </configuration>

            </plugin>

 

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-eclipse-plugin</artifactId>

                <version>2.9</version>

                <configuration>

                    <downloadSources>true</downloadSources>

                    <downloadJavadocs>true</downloadJavadocs>

                    <wtpversion>2.0</wtpversion>

                    <wtpContextName>spring</wtpContextName>

                </configuration>

            </plugin>

 

        </plugins>

    </build>

 

</project>

 

3. HelloController

 

package com.codewr.web.controller;

 

import org.springframework.stereotype.Controller;

import org.springframework.ui.ModelMap;

import org.springframework.web.bind.annotation.PathVariable;

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

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.servlet.ModelAndView;

 

/**

 *

 * @author codewr

 */

@Controller

public class HelloController {

 

    @RequestMapping(value = "/", method = RequestMethod.GET)

    public String printWelcome(ModelMap model) {

 

        model.addAttribute("message", "Spring  MVC Hello World");

        return "hello";

 

    }

 

    @RequestMapping(value = "/hello/{name:.+}", method = RequestMethod.GET)

    public ModelAndView hello(@PathVariable("name") String name) {

 

        ModelAndView model = new ModelAndView();

        model.setViewName("hello");

        model.addObject("msg", name);

 

        return model;

 

    }

 

}

 

4. File Config annotation

- SpringWebConfig.class

 

package com.codewr.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.config.annotation.ResourceHandlerRegistry;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import org.springframework.web.servlet.view.InternalResourceViewResolver;

import org.springframework.web.servlet.view.JstlView;

 

/**

 *

 * @author codewr

 */

@EnableWebMvc

@Configuration

@ComponentScan({"com.codewr.web"})

public class SpringWebConfig extends WebMvcConfigurerAdapter {

 

    @Override

    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");

    }

 

    @Bean

    public InternalResourceViewResolver viewResolver() {

        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();

        viewResolver.setViewClass(JstlView.class);

        viewResolver.setPrefix("/WEB-INF/views/jsp/");

        viewResolver.setSuffix(".jsp");

        return viewResolver;

    }

 

}

 

- MyWebInitializer.class

 

package com.codewr.servlet3;

 

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

 

import com.codewr.config.SpringWebConfig;

 

/**

 *

 * @author codewr

 */

public class MyWebInitializer extends

        AbstractAnnotationConfigDispatcherServletInitializer {

 

    @Override

    protected Class<?>[] getServletConfigClasses() {

        return new Class[]{SpringWebConfig.class};

    }

 

    @Override

    protected String[] getServletMappings() {

        return new String[]{"/"};

    }

 

    @Override

    protected Class<?>[] getRootConfigClasses() {

        return null;

    }

 

}

 

5. File hello.jsp

 

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<!DOCTYPE html>

<html lang="en">

    <head>

        <title>Spring MVC</title>

 

        <spring:url value="/resources/core/css/hello.css" var="coreCss" />

        <spring:url value="/resources/core/css/bootstrap.min.css" var="bootstrapCss" />

        <link href="${bootstrapCss}" rel="stylesheet" />

        <link href="${coreCss}" rel="stylesheet" />

    </head>

 

    <nav class="navbar navbar-inverse navbar-fixed-top">

        <div class="container">

            <div class="navbar-header">

                <a class="navbar-brand" href="#">Spring  MVC Project @JavaConfig</a>

            </div>

        </div>

    </nav>

 

    <div class="jumbotron">

        <div class="container">

            <h1>${title}</h1>

            <p>

                <c:if test="${not empty name}">

                    Hello ${name}

                </c:if>

 

                <c:if test="${empty name}">

                    Welcome Welcome!

                </c:if>

            </p>

            <p>

                <a class="btn btn-primary btn-lg" href="#" role="button">Learn more</a>

            </p>

        </div>

    </div>

 

    <div class="container">

 

        <div class="row">

            <div class="col-md-4">

                <h2>Heading</h2>

                <p>ABC</p>

                <p>

                    <a class="btn btn-default" href="#" role="button">View details</a>

                </p>

            </div>

            <div class="col-md-4">

                <h2>Heading</h2>

                <p>ABC</p>

                <p>

                    <a class="btn btn-default" href="#" role="button">View details</a>

                </p>

            </div>

            <div class="col-md-4">

                <h2>Heading</h2>

                <p>ABC</p>

                <p>

                    <a class="btn btn-default" href="#" role="button">View details</a>

                </p>

            </div>

        </div>

 

 

        <hr>

        <footer>

            <p>&copy; Codewr.com 2018</p>

        </footer>

    </div>

 

    <spring:url value="/resources/core/css/hello.js" var="coreJs" />

    <spring:url value="/resources/core/css/bootstrap.min.js" var="bootstrapJs" />

 

    <script src="${coreJs}"></script>

    <script src="${bootstrapJs}"></script>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

 

</body>

</html>

 

6. Run web:

 

http://localhost:8080/spring-mvc-maven-annotation-hello-world/

http://localhost:8080/spring-mvc-maven-annotation-hello-world/hello/codewr

 

 

7. Link full code

https://github.com/sieurobo196/Spring-MVC/tree/master/SpringMVC-Helloworld-Annatation-Config