You can develop Spring MVC application with Spring Boot in two ways.
1. Develop as FAT JAR file.
2. Develop as WAR file
FAT Jar is self executable application which contains Application Code +
Embedded Container
You can execute the FAT jar as follows.
Java –jar [Link]
WAR file is deployable application which can be deployed in any External
Container to run.
Lab16: Spring MVC with Boot (with JSP)
Working Steps:
A) Setup the Project
1) Create the maven Project.
a) Open the Eclipse required Workspace
b) Select New -> Maven Project.
c) Check the checkbox for create Simple Project(skip the Archetype selection) and
Click on Next Button.
d) Provide the following
Group Id: [Link] Artifact Id :
Lab16
Version : 1.0
packaging : jar
Name : MyLab16and Click On finish button.
2) Remove the following two Source Folders.
a. src/test/java
b. src/test/resources
3) Create [Link] under src/main/resources
4) Update [Link] as follows
[Link]=12345
5) Create the Source Folder called src/main/webapp
6) Create the Folder called WEB-INF under src/main/webapp
7) Create the Folder called myjsps under WEB-INF
[Link] 91 Spring Boot 2
8) Update [Link] by adding the following dependencies.
<parent>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
</parent>
<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
9) You can see the Error in Maven Project.
Fix the Error by updating the Maven Project.
Select the Project , right click and then select Maven -> Update Project.
10) Create the Package called [Link] under src/main/java
[Link] 92 Spring Boot 2
11) Write Bean Configuration class – [Link]
12) Configure View Resolver in [Link]
@SpringBootApplication
public class JTCWebConfig implements WebMvcConfigurer{
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
[Link]("/WEB-INF/myjsps/");
[Link](".jsp");
[Link]([Link]);
return resolver;
}
}
13) Write Boot Main Class – [Link]
@SpringBootApplication(scanBasePackages = { "[Link]" })
public class StartMyBootApp extends SpringBootServletInitializer {
public static void main(String[] args)
{ [Link]([Link],
args);
}
14) Run the Boot Application
You will see – Whitelabel Error Page means DS is Ready.
B) Develop the Usecase : show index page
[Link] => You are sending the request for /
15) Write [Link] under WEB-INF/myjsps folder.
<!DOCTYPE html>
<html>
<body>
<h1 class="text-center"> Welcome to Jtcindia !!!</h1>
</body>
</html>
[Link] 93 Spring Boot 2
16) Write [Link]
@Controller
public class IndexController { @GetMapping("/")
public String showIndexPage()
{ [Link]("IndexController - showIndexPage()");
return "index";
}
}
17) Run the Boot Application
18) Open the browser and hit [Link] If every thing fine you will see
[Link]
C) Enable BootStarp:
19) Specify the Dependencies for Jquery and Bootstarp
<dependency>
<groupId>[Link]</groupId>
<artifactId>bootstrap</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>jquery</artifactId>
<version>3.4.1</version>
</dependency>
20) Observe the following
a) [Link]
META-INF=> resources => webjars => bootstarp => 4.3.1 => css => [Link]
META-INF=> resources => webjars => bootstarp => 4.3.1 => js => [Link]
b) [Link] =>
META-INF=> resources => webjars => jquery => 3.4.1 => [Link]
[Link] 94 Spring Boot 2
21) Add the resource handler bean (to make your resources available in class path)
in JTCWebConfig
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry)
{ [Link]("/webjars/**").addResourceLocations("classpath:/META
-INF/resources/webjars/");
}
22) Include the css and js files in [Link].
<link href="webjars/bootstrap/4.3.1/css/[Link]" rel="stylesheet">
<script src="webjars/jquery/3.4.1/[Link]"></script>
<script src="webjars/bootstrap/4.3.1/js/[Link]"></script>
23) Use Some BootStrap Classes in [Link]
<h1 class="text-center"> Welcome to Jtcindia !!!</h1>
24) Run the Boot Application and Test
25) Open the browser and hit [Link]
If every thing fine you will see Bootstrap Css styles applied in [Link]
D) Develop the Usecase : show books
[Link]
26) Add hyperlink in [Link]
<h2 class="text-center"> <a href="showBooks"> Show Books </a></h2>
27) Write [Link] under [Link]
28) Write [Link] under [Link]
29) Write [Link] under WEB-INF/myjsps
30) Run the Boot Application and Test
Open the browser and hit [Link]
[Link] 95 Spring Boot 2
Lab16: Files Required
1. [Link] 2. [Link]
3. [Link] 4. [Link]
5. [Link] 6. [Link]
7. [Link] 8. [Link]
9. [Link]
1. [Link]
<!DOCTYPE html>
<html>
<head>
<title>JTC Bookstore</title>
<link href="webjars/bootstrap/4.3.1/css/[Link]" rel="stylesheet">
</head>
<body>
<h1 class="text-center"> Welcome to Jtcindia !!!</h1>
<br/>
<h2 class="text-center"> <a href="showBooks"> Show Books </a></h2>
<script src="webjars/jquery/3.4.1/[Link]"></script>
<script src="webjars/bootstrap/4.3.1/js/[Link]"></script>
</body>
</html>
[Link] 96 Spring Boot 2
2. [Link]
<%@ taglib uri="[Link] prefix="c" %>
<html> <head>
<title>JTC Bookstore</title>
<link href="webjars/bootstrap/4.3.1/css/[Link]" rel="stylesheet">
</head>
<body>
<div class="container">
<h1 class="text-center"> Welcome to Jtcindia !!!</h1>
<div class="alert alert-primary" role="alert">
<h2 class="text-center"> Latest Books from Som Prakash Rai </h2>
</div>
<br/>
<table class="table table-striped table-bordered table-hover" style="font-size:20px;">
<thead class="thead-light ">
<tr> <th> Book Id </th>
<th> Book Name </th>
<th> Author</th>
<th> Price </th>
<th> Publications</th>
<th> </th>
<th> </th> </tr>
</thead>
<tbody>
<c:forEach var="mybook" items="${MyBooks}">
<tr>
<td> <a href="#"> ${[Link]} </a> </td>
<td> ${[Link]} </td>
<td> ${[Link]} </td>
<td> ${[Link]} </td>
<td> ${[Link]}</td>
<td> <button type="button" class="btn btn-success btn-lg">View Book</button> </td>
<td> <button type="button" class="btn btn-primary btn-lg">Edit Book</button> </td>
</tr>
</c:forEach>
<tbody>
</table>
</div>
<script src="webjars/jquery/3.4.1/[Link]"></script>
<script src="webjars/bootstrap/4.3.1/js/[Link]"></script>
</body>
</html>
[Link] 97 Spring Boot 2
3. [Link]
spackage [Link];
import [Link];
/*
* @Author : Som Prakash Rai
* @Company : JTCINDIA
* @Website : [Link]
* */
public class Book
{ private Integer bid;
private String bname;
private String author;
private BigDecimal price;
private String pub;
public Book() {}
//Constructors
//Setters and Getters
}
4. [Link]
package [Link];
import [Link];
import [Link];
/*
* @Author : Som Prakash Rai
* @Company : JTCINDIA
* @Website : [Link]
* */
@Controller
public class IndexController {
@GetMapping("/")
public String showIndexPage()
{ [Link]("IndexController - showIndexPage()");
return "index";
}
}
[Link] 98 Spring Boot 2
5. [Link]
package [Link];
import [Link];
import [Link].*;
import [Link];
import [Link];
import [Link];
/*
* @Author : Som Prakash Rai
* @Company : JTCINDIA
* @Website : [Link]
* */
@Controller
public class BooksController {
@GetMapping("/showBooks")
public String getBooks(HttpSession session)
{ [Link](" BooksController - getBooks()");
List<Book> blist=new ArrayList<>();
Book mybook1=new Book(101,"Master Spring Boot","Som Prakash Rai
",[Link](10000.0),"JTC");
Book mybook2=new Book(102,"Master MicroServices","Som Prakash
Rai",[Link](10000.0),"JTC");
Book mybook3=new Book(103,"Master Angular","Som Prakash
Rai",[Link](10000.0),"JTC");
Book mybook4=new Book(104,"Master React ","Som Prakash
Rai",[Link](10000.0),"JTC");
Book mybook5=new Book(105,"Master Java Full Stack","Som Prakash
Rai",[Link](10000.0),"JTC");
[Link](mybook1); [Link](mybook2); [Link](mybook3);
[Link](mybook4); [Link](mybook5);
[Link]("MyBooks", blist);
return "booksList";
}
}
[Link] 99 Spring Boot 2
6. [Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
/*
* @Author : Som Prakash Rai
* @Company : JTCINDIA
* @Website : [Link]
* */
@SpringBootApplication
public class JTCWebConfig implements
WebMvcConfigurer{ @Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
[Link]("/WEB-INF/myjsps/");
[Link](".jsp");
[Link]([Link]);
return resolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry)
{ [Link]("/webjars/**").addResourceLocations("classpath:/META-
INF/resources/webjars/");
}
}
7. [Link]
package [Link];
import [Link];
import [Link];
import [Link];
/*
* @Author : Som Prakash Rai
* @Company : JTCINDIA
* @Website : [Link]
* */
[Link] 100 Spring Boot 2
@SpringBootApplication(scanBasePackages = { "[Link]" }) public
class StartMyBootApp extends SpringBootServletInitializer {
public static void main(String[] args)
{ [Link]([Link],
args);
}
}
8. [Link]
[Link]=12345
9. [Link]
<project…>
<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>Lab16</artifactId>
<version>1.0</version>
<name>MyLab16</name>
<url>[Link]
<parent>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
</parent>
<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
[Link] 101 Spring Boot 2
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>bootstrap</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>jquery</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
<build>
<finalName>MyLab16</finalName>
<plugins>
<plugin>
<groupId>[Link]</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
[Link] 102 Spring Boot 2
Lab17: Spring MVC with Boot (using ThymeLeaf)
Working Steps:
1) Copy Lab16 as Lab17
2) Remove the WEB-INF under src/main/webapp ( So that all the JSP’s will be
deleted)
3) Remove ViewResolver from [Link]
4) Create templates folder under src/main/resources
5) Place [Link] and [Link] under templates
a)[Link] (new in Lab17)
b)[Link] (new in Lab17)
6) Add the following property in [Link]
[Link]=54321
[Link]=false
7) Remove the following dependencies from [Link]
<dependency>
<groupId>[Link]</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
8) Add the dependency for ThymeLeaf in [Link]
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
[Link] 103 Spring Boot 2
9) Make sure that all the Files as given below are updated correctly.
10) Run the Boot Application
11) Open the browser and hit [Link]
Lab17: Files Required
1. [Link] New in Lab17
2. [Link] New in Lab17
3. [Link] Same as Lab16
4. [Link] Same as Lab16
5. [Link] Same as Lab16
6. [Link] Updated in Lab17
7. [Link] Same as Lab16
8. [Link] Updated in Lab17
9. [Link] Updated in Lab17
1. [Link]
<!DOCTYPE html>
<html xmlns:th="[Link]
<head>
<title>JTC Bookstore</title>
<link href="webjars/bootstrap/4.3.1/css/[Link]" rel="stylesheet">
</head>
<body>
[Link] 104 Spring Boot 2
<h1 class="text-center"> Welcome to Jtcindia !!!</h1>
<br/>
<h2 class="text-center"> <a th:href="@{/showBooks}">Show Books</a> </h2>
<script src="webjars/jquery/3.4.1/[Link]"></script>
<script src="webjars/bootstrap/4.3.1/js/[Link]"></script>
</body> </html>
2. [Link]
<!DOCTYPE html>
<html xmlns:th="[Link]
<head>
<title>JTC Bookstore</title>
<link href="webjars/bootstrap/4.3.1/css/[Link]" rel="stylesheet">
</head>
<body>
<div class="container">
<h1 class="text-center"> Welcome to Jtcindia !!!</h1>
<div class="alert alert-primary" role="alert">
<h2 class="text-center"> Latest Books from Som Prakash Rai </h2>
</div>
<br/>
<table class="table table-striped table-bordered table-hover" style="font-size:20px;">
<thead class="thead-light ">
<tr> <th> Book Id </th>
<th> Book Name </th>
<th> Author</th>
<th> Price </th>
<th> Publications</th>
<th> </th> <th> </th>
</tr> </thead>
<tbody >
<tr th:each="mybook : ${[Link]}">
<td th:text="${[Link]}"> </td>
<td th:text="${[Link]}"> </td>
<td th:text="${[Link]}"></td>
<td th:text="${[Link]}"> </td>
<td th:text="${[Link]}"> </td>
<td> <button type="button" class="btn btn-success btn-lg">View Book</button> </td>
<td> <button type="button" class="btn btn-danger btn-lg">Edit Book</button> </td>
</tr>
<tbody>
</table>
</div>
</body> </html>
[Link] 105 Spring Boot 2
3. [Link]
4. [Link]
5. [Link]
6. [Link]
package [Link];
import [Link];
import [Link];
import [Link];
/*
* @Author : Som Prakash Rai
* @Company : JTCINDIA
* @Website : [Link]
* */
@SpringBootApplication
public class JTCWebConfig implements
WebMvcConfigurer{ @Override
public void addResourceHandlers(ResourceHandlerRegistry registry)
{ [Link]("/webjars/**").addResourceLocations("classpath:/META-
INF/resources/webjars/");
}
}
7. [Link]
8. [Link]
[Link]=54321
[Link]=false
9. [Link]
<project…>
<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>Lab17</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>Lab17-jar</name>
<url>[Link]
<parent>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
</parent>
[Link] 106 Spring Boot 2
<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>bootstrap</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>jquery</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
<build>
<finalName>MyLab17</finalName>
<plugins>
<plugin>
<groupId>[Link]</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
[Link] 107 Spring Boot 2