Spring Boot - Creating web application using Spring MVC



Spring Boot - Creating web application using Spring MVC

In this post, I will show you how to create and bootstrap a Spring MVC web application using the Spring Boot framework. We will use the JSP as view for our web application.

Tools and technologies used for this application are-

  • Spring Boot 1.5.4.RELEASE
  • Spring WebMVC 4.3.9.RELEASE
  • Tomcat Embedded 8.5
  • JavaSE 1.8
  • Maven 3.3.9
  • Eclipse Neon.3

 

Project Structure 

Review the spring boot project structure.

Spring Boot - Creating web application using Spring MVC | BORAJI.COM

Related - How to create a maven project in eclipse IDE.

 

Jar dependencies

To create and run a Spring MVC web application in spring boot, you need to add the spring-boot-starter dependency in your pom.xml file. 

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

Here is the complete pom.xml 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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.boraji.tutorial.springboot</groupId>
  <artifactId>spring-boot-web-application-example</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <properties>
    <java.version>1.8</java.version>
  </properties>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.4.RELEASE</version>
  </parent>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- JSTL tag lib -->
    <dependency>
      <groupId>javax.servlet.jsp.jstl</groupId>
      <artifactId>javax.servlet.jsp.jstl-api</artifactId>
      <version>1.2.1</version>
    </dependency>

    <dependency>
      <groupId>taglibs</groupId>
      <artifactId>standard</artifactId>
      <version>1.1.2</version>
    </dependency>

    <!-- Tomcat for JSP rendering -->
    <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>
  <packaging>war</packaging>
</project>

 

Controller class

Create a HelloController class under com.boraji.tutorial.springboot.controller package and write the following code in it.

HelloController.java

package com.boraji.tutorial.springboot.controller;

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

@Controller
public class HelloController {

   @RequestMapping("/")
   public String index() {
      return "index";
   }

   @PostMapping("/hello")
   public String sayHello(@RequestParam("name") String name, Model model) {
      model.addAttribute("name", name);
      return "hello";
   }
}

 

JSP Views

Create index.jsp and hello.jsp  files under src/main/webapp/WEB-INF/views folder as shown in the project structure.

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<!-- Static content -->
<link rel="stylesheet" href="/resources/css/style.css">
<script type="text/javascript" src="/resources/js/app.js"></script>

<title>Spring Boot</title>
</head>
<body>
  <h1>Spring Boot - MVC web application example</h1>
  <hr>

  <div class="form">
    <form action="hello" method="post" onsubmit="return validate()">
      <table>
        <tr>
          <td>Enter Your name</td>
          <td><input id="name" name="name"></td>
          <td><input type="submit" value="Submit"></td>
        </tr>
      </table>
    </form>
  </div>

</body>
</html>

hello.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring Boot</title>
</head>
<body>
  <h1>Spring Boot - MVC web application example</h1>
  <hr>

  <h2>Your name is ${name}</h2>

</body>
</html>

 

Static web resources

By default Spring Boot serves the static web content from the /static or /public or /resources or /META-INF/resources folder in a classpath. In this example, we will put all static resources such as  stylesheet, JavaScript files under /static folder.

Create a stylesheet file under src/main/resources/static/css folder and write the following code in it.

style.css

.form {
	background-color: #efefef;
	width: 400px;
	height: 50px;
	border-radius: 7px;
	padding: 20px;
}

Now, create a javascript file under src/main/resources/static/js folder and write the following code in it.

app.js

function validate() {
	var name = document.getElementById("name").value;
	if (name == '') {
		alert('Please enter a valid name.');
		return false;
	} else {
		return true;
	}
}

 

Application properties

Spring boot load the properties from application.properties and add them to the Spring Environment. You can set the properties related to the Spring MVC or static web content in application.properties file.

Create an application.properties file under src/main/resources source folder and write the following properties in it.

application.properties

spring.mvc.view.prefix = /WEB-INF/views/
spring.mvc.view.suffix = .jsp
spring.mvc.static-path-pattern=/resources/**

Note - spring.mvc.static-path-pattern=/resources/** will map the classpath:/static/css/style.css to /resources/css/style.css. Similarly, the classpath:/static/js/app.js  will be mapped to /resources/js/app.js. You can use these static resources in jsp as follows.

<link rel="stylesheet" href="/resources/css/style.css">
<script type="text/javascript" src="/resources/js/app.js"></script>

 

Main class

To bootstrap spring MVC we application using Spring Boot, create a main class annotated with @SpringBootApplication annotation as follows.

MainApp.java

package com.boraji.tutorial.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MainApp {
   public static void main(String[] args) {
      SpringApplication.run(MainApp.class, args);
   }
}

 

Run application

Run the MainApp.java class as Java application i.e. go to Run→ Run as → Java Application

You can use the mvn spring-boot:run command to run the spring boot application.

After executing the main class, the console output will look like as follow.

Spring Boot - Creating web application using Spring MVC | BORAJI.COM

Now, enter the http://localhost:8080 in browser's address and enter the your name in input field.

Spring Boot - Creating web application using Spring MVC | BORAJI.COM

Click on submit button and see the result page.

Spring Boot - Creating web application using Spring MVC | BORAJI.COM

 

Packaging Spring Boot application as an executable war

You need to package the Spring boot application as war instead of jar, if you are using an embedded servlet container. There are some limitations in the JSP support. Read more..

You can use the following maven command to package your spring boot application as war.

mvn clean package

 

Running executable war from command line

Use the following the following command to run the war from the CMD.

>java -jar spring-boot-web-application-example-0.0.1-SNAPSHOT.war