Maven Multi-Module - Spring MVC - Web App Example!

Comes with Jetty!

Notes

  • Maven Multi Module Project!
  • Core has XML Configuration, no Component Scan!
  • Web has XML Configuration with Component Scan!
  • Spring makes sure that Root Context is parent of Dispatcher Servlets Context!
  • Spring Boot Process in Deployment Descriptor!

Directory Layout

(root) 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>biz.tugay</groupId>
    <artifactId>coreweb</artifactId>
 
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
 
    <properties>
        <spring.version>4.3.3.RELEASE</spring.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>
 
    <modules>
        <module>core</module>
        <module>web</module>
    </modules>
</project>

(core) 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>
 
    <artifactId>core</artifactId>
 
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
 
    <name>core Maven Webapp</name>
    <url>http://maven.apache.org</url>
 
    <parent>
        <groupId>biz.tugay</groupId>
        <artifactId>coreweb</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
 
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
 
    <build>
        <finalName>core</finalName>
    </build>
</project>

core-context.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd">
 
    <bean id="coreService" class="biz.tugay.core.CoreServiceImpl"/>
</beans>

CoreService.java
package biz.tugay.core;
 
/**
 * User: Koray Tugay (koray@tugay.biz)
 * Date: 11/8/2016
 * Time: 7:49 PM
 */
public interface CoreService {
 
    int randomNumber();
}

CoreServiceImpl.java
package biz.tugay.core;
 
import java.util.Random;
 
/**
 * User: Koray Tugay (koray@tugay.biz)
 * Date: 11/8/2016
 * Time: 7:49 PM
 */
public class CoreServiceImpl implements CoreService {
    public int randomNumber() {
        final Random random = new Random();
        final int randomNumber = random.nextInt();
        return randomNumber;
    }
}

(web) 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>
 
    <artifactId>web</artifactId>
 
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
 
    <name>web Maven Webapp</name>
    <url>http://maven.apache.org</url>
 
    <parent>
        <groupId>biz.tugay</groupId>
        <artifactId>coreweb</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
 
    <dependencies>
        <dependency>
            <groupId>biz.tugay</groupId>
            <artifactId>core</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
 
    <build>
        <finalName>web</finalName>
        <plugins>
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.2.1.v20140609</version>
                <configuration>
                    <scanIntervalSeconds>2</scanIntervalSeconds>
                    <webApp>
                        <contextPath>/</contextPath>
                    </webApp>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                             http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
 
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:core-context.xml</param-value>
    </context-param>
 
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
 
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
 
    <welcome-file-list>
        <welcome-file>/</welcome-file>
    </welcome-file-list>
</web-app>

springmvc-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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-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="biz.tugay.web"/>
    <context:annotation-config/>
 
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

HelloWorldController.java
package biz.tugay.web;
 
import biz.tugay.core.CoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
 
/**
 * User: Koray Tugay (koray@tugay.biz)
 * Date: 11/8/2016
 * Time: 7:59 PM
 */
 
@Controller
public class HelloWorldController {
 
    private CoreService coreService;
 
    @RequestMapping(value = "/")
    public ModelAndView indexPage() {
        ModelAndView mv = new ModelAndView();
        mv.addObject("message", Integer.toString(coreService.randomNumber()));
        mv.setViewName("helloworld");
        return mv;
    }
 
    @Autowired
    public void setCoreService(CoreService coreService) {
        this.coreService = coreService;
    }
}

helloworld.jsp
<html>
<body>
<h2>Hello World!</h2>
<div>
Here is your random number: ${message}!
</div>
</body>
</html>

In Action!