Spring MVC: Creating a Project from Scratch using Maven

If you don’t want to use Spring STS to create your Spring MVC project, you can, of course, create it from the scratch. And here is what we will need to do:

  • Create a java web project using maven
  • Test it using Jetty
  • Transform it to a Spring MVC project

For the source code, you can download it here: https://github.com/nxhoaf/spring-mvc-from-scratch. And now, let’s start:

1. Create a java j2ee project using maven:

mvn archetype:generate
     -DgroupId=com.gmail.nxhoaf
     -DartifactId=spring-mvc-from-scratch
     -DarchetypeArtifactId=maven-archetype-webapp

When asked for the version, just enter to choose the default one (1.0-SNAPSHOT).  After that, we should have a project structure like below:

spring-mvc-from-scratch
    ├── pom.xml
    └── src
        └── main
            ├── resources
            └── webapp
                ├── index.jsp
                └── WEB-INF
                    └── web.xml

Next, we will convert it to eclipse project and import it to your eclipse ide. This step is optional, if you don’t want to use eclipse, feel free to go ahead.

mvn eclipse:eclipse

2. Test it using jetty server

– Add some configurations:

        <plugins>
            <!-- To enable jetty -->
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>maven-jetty-plugin</artifactId>
            </plugin>
        </plugins>

        <!-- Servlet and JSP -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.4</version>
            <!-- <scope>provided</scope> -->
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <!-- <scope>provided</scope> -->
        </dependency>

run it:

mvn clean install
mvn jetty:run

Then, go to http://localhost:8080/spring-mvc-from-scratch/, you should she the hello world message.

3. Transform the project to a Spring MVC project

– Add the FrontController and the listener in the xml file:

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Define Front Controller for Spring -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/masterApplicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

– Then, create an applicationContext.xml in WEB-INF with the following content:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="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
                http://www.springframework.org/schema/mvc
                http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    <!-- Handle annotation driven request mapping -->
    <mvc:annotation-driven />

    <context:component-scan base-package="com.gmail.nxhoaf" />
    
       <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

 

– Next, import the following Spring MVC dependencies to your pom.xml

    <!-- Define some properties -->
    <properties>
        <spring.version>3.0.2.RELEASE</spring.version>
    </properties>

        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <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>

– Then, we create the first Spring Hello World Controller:

package com.gmail.nxhoaf;

import java.util.Date;
import java.util.Locale;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HelloWorldController {
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home(Locale locale, Model model) {
        Date date = new Date();
        model.addAttribute("serverTime", date );
        return "helloWorld";
    }
}

And finally, create the helloWorld.jsp in WEB-INF/views

<%@ page isELIgnored="false" %>

<html>
<body>
<h2>Hello World at: ${serverTime}</h2>
</body>
</html>

Now, you can test your work at: http://localhost:8080/spring-mvc-from-scratch/

 

Advertisements
This entry was posted in Java. Bookmark the permalink.

One Response to Spring MVC: Creating a Project from Scratch using Maven

  1. Pingback: Spring MVC: Return a JSON object | nxhoaf

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s