Bright Java Tutorial

is brought to you by DoITBright LLC

Spring Boot

MVC and JSP Example

This lesson will show us a very simple example on how to create our first web application that implements the design pattern Model View Controller or MVC using Spring Boot framework. It is assumed that you have your JDK and Eclipse IDE setup on your local machine. Extract your Eclipse IDE and place it under the directory c:\>SpringBootStuff. I am using Windows OS and my directory looks like this...

Eclipse on your local directory


Look for the eclipse.exe file inside the 'eclipse' folder. Launch your Eclipse IDE.

Launch Eclipse IDE


Click on the tab 'File', then 'New', then 'Maven Project'. Make sure to select the checkbox 'Use default workspace location'. Click the 'Next' button.

Create Maven Project


Select the 'maven-archetype-webapp'. Click 'Next' button.

Create Maven Project


Fill through the next form by providing 'com.brightjavatutorial.spring.boot' as Group ID and 'SpringBootMvcJsp' as Artifact ID. Click the 'Finish' button. Your Maven project named as 'SpringBootMvcJsp' is created. Here is how it will look like...

Create Maven Project


Please take note that you have to delete the 'index.jsp' file generated by Eclipse IDE and provide the other files that are not yet present as of the moment. The above image is how your final file and directory structure will look like after this tutorial.

Open the 'pom.xml' file and copy and paste the code as shown below...

  <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.brightjavatutorial.spring.boot
    </groupId>
    <artifactId>
      SpringBootMvcJsp
    </artifactId>
    <packaging>
      war
    </packaging>
    <version>
      0.0.1-SNAPSHOT
    </version>
    <name>
      SpringBootMvcJsp Maven Webapp
    </name>
    <url>
      http://maven.apache.org
    </url>

    <parent>
      <groupId>
        org.springframework.boot
      </groupId>
      <artifactId>
        spring-boot-starter-parent
      </artifactId>
      <version>
        1.3.5.RELEASE
      </version>
    </parent>

    <properties>
      <java.version>
	    1.8
      </java.version>
    </properties>

    <dependencies>
      <dependency>
        <groupId>
          org.springframework.boot
        </groupId>
        <artifactId>
          spring-boot-starter-web
        </artifactId>
      </dependency>

      <dependency>
        <groupId>
          org.springframework.boot
        </groupId>
        <artifactId>
		  spring-boot-starter-tomcat
        </artifactId>
        <scope>
		  provided
        </scope>
      </dependency>

      <dependency>
        <groupId>
          org.apache.tomcat.embed
        </groupId>
        <artifactId>
          tomcat-embed-jasper
        </artifactId>
        <scope>
          provided
        </scope>
      </dependency>
    </dependencies>
    <build>
      <plugins>
        <plugin>
          <groupId>
            org.springframework.boot
          </groupId>
          <artifactId>
            spring-boot-maven-plugin
          </artifactId>
        </plugin>
      </plugins>
    </build>
  </project>
                           


Create a Java file under the package 'springboot.mvc'. Make sure that this package will be in 'C:\SpringBootStuff\SpringBootMvc\src\main\java\' directory. Name it as 'HomePageController.java'.

  package springboot.mvc;

  import javax.servlet.http.HttpServletRequest;
  import org.springframework.stereotype.Controller;
  import 
    org.springframework.web.bind.annotation.RequestMapping;
  import 
    org.springframework.web.bind.annotation.RequestMethod;

  /**
   * @author Rolan Liwanag
   */
  @Controller
  public class HomePageController {
	
    @RequestMapping(value = "/", 
      method = {RequestMethod.GET, RequestMethod.POST})
    public String homePage(HttpServletRequest request) {
      request.setAttribute("message",
        "Hello to the world of Spring Boot with JSP!");
      return "homePage";
    }
  }
                            


This class is marked with a '@Controller' annotation. This will serve as a controller. Look at the method declared inside the class, it is has an annotation of '@RequestMapping'. This means, this will map to your root URL.

Create another class in the same package. Name it as WebApplication.java.

  package springboot.mvc;

  import org.springframework.boot.SpringApplication;
  import 
    org.springframework.boot.autoconfigure
      .SpringBootApplication;
  import 
    org.springframework.boot.builder
      .SpringApplicationBuilder;
  import 
    org.springframework.boot.context.web
      .SpringBootServletInitializer;

  @SpringBootApplication
  public class WebApplication 
    extends SpringBootServletInitializer {
    
    @Override
    protected SpringApplicationBuilder configure(
      SpringApplicationBuilder application) {
      return application.sources(WebApplication.class);
    }

    public static void main(String[] args) 
      throws Exception {
      SpringApplication.run(WebApplication.class, args);
    }
  }
                            


The above class will be able to make your web application run without packaging it into a WAR file and without placing it into the Apache Tomcat 'webapps' directory if you even have one installed on your local machine. This is the beauty of Spring Boot framework. It has a built in tomcat. Do not try running the application yet. We still have few more things to do.

Inside the '\SpringBootMvcJsp\src\main\webapp' directory, create a JSP file. Name it as 'homePage.jsp'. The name of this JSP file should match the value of the String being returned in your 'HomePageController.java' inside the method 'homePage' without the '.jsp' file name extension.

  <!DOCTYPE html>
  <%
  String message 
    = (String) request.getAttribute("message");
  %>
  <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>Hello</title>
    </head>
    <body>
      <%= message %>
    </body>
  </html>
                            


The next thing we have to do is create a file named 'application.properties'. This file should be inside the '\SpringBootMvcJsp\src\main\resources\' directory.

  spring.mvc.view.prefix: /
  spring.mvc.view.suffix: .jsp
                            




You now have a web application that can be accessed through the URL 'http://localhost:8080'. Let us now run our application. Click on the Maven project 'SpringBootMvcJsp'. Click on the tab 'Run', then click on 'Run Configurations'. On the left side, look for 'Maven' and click on it. On top, look for 'New Launc Configuration'. It is an icon that looks like a file with a '+' on it. Fill through the form as shown below. Make sure your JVM points to your JDK directory. Click 'Apply' then 'Run'.

Run your Spring Boot application


Try accessing the URL 'http://localhost:8080'. If this is your first time creating and running a Spring Boot application, you will be amazed that you did not have to package your application to a WAR file, and place the WAR file in your '\tomcat\webapps\' directory. Your Spring Boot application has an Apache Tomcat web application server built in it. This is a feature of Spring Boot.

The next thing we have to do is try packaging the 'SpringBootMvcJsp' into a WAR file. Using your Eclipse, right click on the project and click on 'Export', then 'Web', then 'WAR'. The rest is easy to follow. Place the WAR file in your '\tomcat\webapps' directory. Then, start your Tomcat. Remember that when you packaged your application into a WAR file, by default, it named it as 'SpringBootMvcJsp.war'. This will be your application name. So, to access the application, you should invoke 'http://localhost:8080/SpringBootMvcJsp' in your browser.

Here is a good reference on how to deploy web applications in Tomcat.

Writing Your First JSP


Back    Next