• Basic Spring MVC Project By Maven

    Posted on December 26, 2015 by Hamidul Islam in Spring MVC.

    basic-spring-mvc-maven


    aim

    Any one who are in java world know more or less about Spring MVC. In this article we will create very basic Spring MVC project by Maven. This is just a quick start guide for those who want to starts Spring MVC. However learning Maven is not the scope of this article.

    Prerequisites

    To follow this article you should have basic knowledge of Java Web Application and Maven



    Step 1: Open your command prompt and execute the below command

    All Should Go in Single Line

    mvn archetype:generate
    -DgroupId=com.proliferay
    -DartifactId=HelloWorldSpring
    -DarchetypeArtifactId=maven-archetype-webapp
    -DinteractiveMode=false

    Note 1: Once the above command is executed it will create below project structure

    hello-world-spring-project-structure

    Note 2: The above project is a simple java web application and its not a spring project. Below steps will make it Spring MVC project

    Step 2: Add Spring Dependency in pom.xml

    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.proliferay</groupId>
    <artifactId>HelloWorldSpring</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>HelloWorldSpring Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
    <spring.version>4.0.1.RELEASE</spring.version>
    </properties>
    <dependencies>
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>3.8.1</version>
    <scope>test</scope>
    </dependency>
    <!-- Spring dependencies -->
    <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>
    </dependencies>
    <build>
    <finalName>HelloWorldSpring</finalName>
    </build>
    </project>

    Step 3: Configure dispatcher servlet in web.xml

    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_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>Spring Hello World Example</display-name>

    <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>

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

    Note 3: Every Spring MVC Project must have DispatcherServlet. Its a front controller in Spring Framework. Every requests goes through this front controller.

    Note 4: Look back Step 3. We have given name of the dispatcher servlet as dispatcher. So by default it will try to load dispatcher-servlet.xml file under WEB-INF directory. Similarly if the name of the dispatcher servlet is foo then it will try to load foo-servlet.xml and so on.

    Step 4: Create dispatcher-servlet.xml in WEB-INF directory

    dispatcher-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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    <context:component-scan base-package="com.proliferay.controller" />

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

    </beans>

    Note 5: In Spring controller we generally return logical name of the views. For example if a controller return a string "hello" then spring will try to resolve this logical view to physical file. On the basis of the above configuration in Step 4 it will look for hello.jsp under /WEB-INF/views. Similarly if the controller returns a string "helloworld" then it will look for helloworld.jsp /WEB-INF/views and so on.

    Step 5: Create views folder under WEB-INF. The views folder contains helloworld.jsp

    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>HelloWorld Page</title>
    </head>
    <body>
    <center>
    <h2>Hello World Page</h2>
    </center>
    </body>
    </html>

    Step 6: Add  index page under webapp

    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>Index Page</title>
    </head>
    <body>
    <center>
    <h2>Hello World</h2>
    <h3>
    <a href="hello">Show Hello World Page</a>
    </h3>
    </center>
    </body>
    </html>

    Step 7: Add the controller

    HelloWorldController.java

    package com.proliferay.controller;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    /**
    *
    * @author Hamidul Islam
    *
    */

    @Controller

    public class HelloWorldController {

    @RequestMapping("/hello")
    public String showHelloPage() {
    System.out.println("#############showHelloPage method#############");
    /**
    * Spring will display helloworld.jsp under /WEB-INF/views
    */

    return "helloworld";
    }
    }

    Running the example:

    Download the source code from the below link and create the war file of the project by below command

    mvn clean package

    Download Source Code

    Post Tagged with
Comments are closed.
Top
%d bloggers like this:

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close